<?php

namespace App\Http\Controllers\In_Charge;

use App\Http\Controllers\Controller;
use App\Models\Common;
use App\Models\Department;
use App\Models\Service;
use App\Models\Sub_In_Charge;
use Illuminate\Http\Request;
use Exception;

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

    public function index(Request $request)
    {
        try {

            $params['data'] = [];
            $incharge = InCharge_Data();
            $params['service'] = Service::where('department_id', $incharge['department_id'])->orderby('name', 'asc')->get();

            if ($request->ajax()) {

                $input_search = $request['input_search'];

                $query = Sub_In_Charge::where('department_id', $incharge['department_id'])->where('service_id', '=', "");
                if (!empty($input_search)) {
                    $query->where(function ($q) use ($input_search) {
                        $q->where('user_name', 'LIKE', "%{$input_search}%")->orWhere('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 . '" >' . __('label.transfer') . '</a>';                       
                    })
                    ->rawColumns(['transfer'])
                    ->make(true);
            }
            return view('incharge.inactive_sub_incharge.index', $params);
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
}
