<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Validator;
use Auth;
use DataTables;

class ChecklistController extends Controller
{
    public function index()
    {
        return view("menu_v1.checklist");
    }
    public function table()
    {
        $table = DB::table("t_conf_checklists as tcc")
            ->leftJoin("t_clients as tc", "tc.id", "tcc.client_id")
            ->leftJoin("t_users as tu", "tu.id", "tcc.crt_by")
            ->select("tcc.*", "tu.first_name as crt_name", "tc.c_name as company_name")
            ->whereNull("tcc.dlt")
            ->where("tcc.client_id", Auth::user()->client_id);

        // dd($table);

        return DataTables::of($table)
            ->addIndexColumn()
            ->make();
    }
    public function create()
    {
        return view("menu_v1._addChecklist");
    }

    public function store(Request $request)
    {
        $now = time();
        $_dtPost = [
            "name" => $request->name,
            "client_id" => Auth::user()->client_id,
            "desc" => $request->desc,
            "status" => $request->status,
            "crt" => $now,
            "updt" => $now,
            "crt_by" => Auth::user()->id,
            "updt_by" => Auth::user()->id,
        ];

        $dtPost = DB::table("t_conf_checklists")->insert($_dtPost);
        if ($dtPost) {
            return redirect("checklist");
        }
    }
}
