<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Common;
use App\Models\Department;
use App\Models\In_Charge;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Exception;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;


class InActiveInChargeController extends Controller
{
    private $folder = "in_charge";
    public $common;
    public function __construct()
    {
        $this->common = new Common;
    }

    public function index(Request $request)
    {
        try {

            $params['data'] = [];
            $params['department'] = Department::where('status', 1)->orderby('name', 'asc')->get();

            if ($request->ajax()) {

                $input_search = $request['input_search'];

                $query = In_Charge::where('department_id', 0);
                if (!empty($input_search)) {
                    $query->where(function ($q) use ($input_search) {
                        $q->Where('full_name', 'LIKE', "%{$input_search}%")->orWhere('email', 'LIKE', "%{$input_search}%")->orWhere('mobile_number', 'LIKE', "%{$input_search}%");
                    });
                }
                $data = $query->latest()->get();

                $this->common->imageNameToUrl($data, 'image', $this->folder);

                return DataTables()::of($data)
                    ->addIndexColumn()
                    ->addColumn('transfer', function ($row) {
                        
                        return '<a class="edit-delete-btn in_charge_update" title="Edit" style= "font-weight:bold;" data-toggle="modal" href="#edit_incharge_model" data-id="' . $row->id . '" data-department_id="' . $row->department_id . '">' . __('label.transfer') . '</a>';
                    })
                    ->addColumn('register_date', function ($row) {
                        $date = date("Y-m-d", strtotime($row->created_at));
                        return $date;
                    })
                    ->rawColumns(['transfer'])
                    ->make(true);
            }
            return view('admin.inactive_in_charge.index', $params);
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
}
