<?php

namespace App\Http\Controllers\Backend;

use App\Components\Functions;
use App\Models\District;
use App\Models\Province;
use App\Models\Ward;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LocationController extends AdminController
{
    //
    public function index()
    {
        return view('admin.location.index');
    }

    public function updateIsSearch(Request $request)
    {
        $id = $request->input('id');
        $type = $request->input('type');

        if ($type == 'province') {
            $item = Province::query()->where('provinceid', $id)->first();
        } else if ($type == 'district') {
            $item = District::query()->where('districtid', $id)->first();
        } else {
            $item = Ward::query()->where('wardid', $id)->first();
        }
        if ($item->is_search == true) {
            $item->is_search = false;
        } else {
            $item->is_search = true;
        }
        $item->save();
        return response([
            'status' => 1,
        ]);
    }

    public function edit(Request $request)
    {
        $id = $request->input('id');
        $type = $request->input('type');

        if ($type == 'province') {
            $item = Province::query()->where('provinceid', $id)->first();
            $idName='provinceid';
        } else if ($type == 'district') {
            $item = District::query()->where('districtid', $id)->first();
            $idName='districtid';
        } else {
            $item = Ward::query()->where('wardid', $id)->first();
            $idName='wardid';
        }
        return response([
            'status' => 1,
            'data' => view('admin.location.edit', compact('item', 'type', 'idName'))->render()
        ]);
    }

    public function update(Request $request)
    {
        $type = $request->input('type');
        $id = $request->input('id');

        if ($type == 'province') {
            $item = Province::query()->where('provinceid', $id)->first();
        } else if ($type == 'district') {
            $item = District::query()->where('districtid', $id)->first();
        } else {
            $item = Ward::query()->where('wardid', $id)->first();
        }

        if ($request->file('image') && $request->file('image')->isValid()) {
            $item->image = Functions::uploadImage($request->file('image'));
        }
        $item->save();

        return response([
            'status' => 1,
        ]);
    }

    public function getLocationByAttribute(Request $request)
    {
        $parentId = $request->input('parent_id');
        $parentType = $request->input('parent_type');

        if ($parentType == 'province') {
            $items = District::query();
            $type = 'district';
            $id = 'districtid';
        } else if ($parentType == 'district') {
            $items = Ward::query();
            $type = 'ward';
            $id = 'wardid';
        } else {
            $items = Province::query();
            $type = 'province';
            $id = 'provinceid';
        }

        $items = $items->when(!empty($parentId), function ($q) use ($parentType, $parentId) {
            $q->where($parentType . 'id', $parentId);
        });

        return datatables()->of($items)
            ->editColumn('is_search', function ($item) use ($type, $id) {
                $isCheck = '';
                if ($item->is_search) {
                    $isCheck = 'checked';
                }

                return '<input type="checkbox" data-type="'.$type.'" data-id="' . $item->$id . '" ' . $isCheck . ' class="make-switch btn-is-search" data-on-text="Có" data-off-text="Không" >';
            })
            ->editColumn('name', function ($item) use ($type, $id) {
               return '<a href="/admin/location?parent_id='.$item->$id.'&parent_type='.$type.'">'.$item->name.'</a>';
            })
            ->addColumn('action', function($item) use ($type, $id) {
               return '<a href="#edit" data-type="'.$type.'" data-id="' .  $item->$id . '" data-toggle="modal" class="btn btn-sm yellow btn-outline btn-edit"> Sửa</a>';
            })
            ->rawColumns([
                'action',
                'is_search',
                'name'
            ])
            ->make(true);
    }
}
