<?php

namespace App\Http\Controllers\Administrator\Construction;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\ConstructionModel\SubcontractModel;
use Auth;
use DataTables;
class SubcontractController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("administrator.construction.subcontract.index");
    }

    public function get_data_subcontract(Request $request)
    {
        $subcontract = SubcontractModel::get();
        return  Datatables::of($subcontract)
            ->addIndexColumn()
            ->addColumn('action', function($subcontract) {
                return "<a title='Edit' href='".route('construction-subcontract.edit',encrypt($subcontract->id))."' class='btn btn-sm btn-primary' ><i class='fa fa-edit'></i></a>";
            })      
            ->make(true);
    }

    static public function Subcontract_Model_Save($objSubcontractModel,$input)
    {
        $objSubcontractModel->name = $input->name;
        $objSubcontractModel->phone = $input->phone;
        $objSubcontractModel->id_card = $input->id_card;
        $objSubcontractModel->skill = $input->skill;
        $objSubcontractModel->address = $input->address;
        $objSubcontractModel->description = $input->description;
        $objSubcontractModel->user_id = Auth::user()->id;
        $objSubcontractModel->save();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('administrator.construction.subcontract.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'name'=>'required',
            'phone'=>'required|unique:nso007_subcontract,phone',
        ]);
        DB::beginTransaction();
        try {
            $subcontract = new SubcontractModel;
            $this::Subcontract_Model_Save($subcontract,$request);
            DB::commit();
            \LogActivity::addToLog('Subcontract Created successfully!');
            return redirect()->route('construction-subcontract.index')->with("success","Create Successfully!");
        } catch (Exception $e) {
             DB::rollback();
             return \back()->with('warning',$e);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $id = decrypt($id);
        $subcontract = SubcontractModel::find($id);
        return view('administrator.construction.subcontract.edit',compact('subcontract'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name'=>'required',
            'phone'=>'required|unique:nso007_subcontract,phone,'.$id,
        ]);
        DB::beginTransaction();
        try {
            $subcontract = SubcontractModel::find($id);
            $this::Subcontract_Model_Save($subcontract,$request);
            DB::commit();
            \LogActivity::addToLog('Subcontract Update successfully!');
            return redirect()->route('construction-subcontract.index')->with("success","Update Successfully!");
        } catch (Exception $e) {
             DB::rollback();
             return \back()->with('warning',$e);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
