<?php

namespace App\Http\Controllers\Administrator\setup;

use App\Exports\MemberTypeExport;
use App\Fun\Fun;
use App\Imports\MemberPackageImport;
use App\Imports\MemberTypeImport;
use App\Model\product\ProductUnitModel;
use App\Model\product\UnitModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\setup\MemberTypeModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Yajra\DataTables\Facades\DataTables;

class MemberTypeController extends Controller
{
    function __construct()
    {
//        $this->middleware('permission:Package System List', ['only' => ['index']]);
//        $this->middleware('permission:Package System Create', ['only' => ['create', 'store']]);
//        $this->middleware('permission:Package System Edit', ['only' => ['edit', 'update']]);
        Fun::lang();
    }

    public function index(Request $request)
    {
        if ($request->ajax()) {
            $search = $request->input('search.value');
            $data = MemberTypeModel::latest();
            $count_filter = $data->count();
            return DataTables::of($data->take(20))
                ->with([
                    "recordsTotal"      => $count_filter,
                    "recordsFiltered"   => $count_filter,
                ])
                ->addColumn('action', function ($action) {
                        return '<button type="button" data-href = "' . route("member-type.edit", $action->id) . '"  class="button_warning getHtmlForm" title="Edit"><i class="fa fa-pencil"></i></button>';
                })
                ->editColumn('created_at', function ($action) {
                    return myDate($action->created_at);
                })
                ->addIndexColumn()
                ->rawColumns(['action'])
                ->make(true);
        }

        return view('administrator.setup.member-type.index');
    }

    public function create()
    {
        return view('administrator.setup.member-type.create');
    }

    public function store(Request $request)
    {
       return $this->update($request,null);
    }

    public function edit($id)
    {
        $master = MemberTypeModel::find($id);
        return view('administrator.setup.member-type.edit', compact('master'));
    }

    public function update(Request $request,$id)
    {
        $this->validate($request,[
           'name'
        ]);
        MemberTypeModel::updateOrCreate(['id'=>$id],$request->all());
        return response()->json(['status' => 'success']);
    }

    public function import_excel(Request $request)
    {
        if ($request->export_sample) {
            return Excel::download(new MemberTypeExport(), 'MemberType.xlsx');
        }
        if ($request->method() === 'POST') {
            $this->validate($request, [
                'file' => 'required|file|max:10000|mimes:xlsx,csv',
            ]);

            try {
                if (request()->hasFile('file')) {
                    $path = $request->file('file');
                    Excel::import(new MemberTypeImport(), $path);
                    return response()->json(['status' => 'success', 'reload' => 1]);
                }
            } catch (\Exception $er) {
                throw ValidationException::withMessages($er->getMessage());
            }
        }
        return view('component.import-exel',['route'=>'member-type-import-excel']);
    }

}
