<?php

namespace App\Http\Controllers\Admin;

use App\Exports\SupplierExport;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\China;
use App\Models\Supplier;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;

class SupplierController extends Controller
{
    use ResponseJson;

    public function getArea(Request $request){
        $pid = $request->get('pid');
        if(empty($pid)){
            $pid = 0;
        }
        $address = China::where('id','<>',0)->where('pid',$pid)->orderBy('id', 'asc')->get()->toArray();
        if(strlen($pid)){
            foreach ($address as $key => $value){
                $address[$key]['city'] = China::where('pid',$value['id'])->orderBy('id', 'asc')->get()->toArray();
            }
        }
        return $this->success('ok',$address);
    }

    public function addData(Request $request){
        try {
            $data = $request->all();
            $validated = Validator::make($data, [
                'name' => 'required|unique:supplier',
//                'main_category' => 'required',
//                'main_product' => 'required',
//                'product_ids' => 'required',
            ], [
                'name.required' => '请填写名称',
//                'main_category.required' => '请填写主营大类',
//                'name.unique' => '供应商名不可重复',
//                'product_ids.required' => '请选择产品',
            ]);

            if ($validated->fails()) {
                $errors = $validated->errors()->all();
                $error = count($errors)?$errors[0]:null;
                return $this->failure($error);
            }
            $data['product_ids'] = json_encode($data['product_ids']);
            Supplier::create($data);

            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function getlist(Request $request){
        try {
            $keyword = $request->get('keyword');
            $no_page = $request->get('no_page');
            $is_export = $request->get('is_export');
            if($is_export){
                $no_page = true;
            }
            $query = Supplier::query()->when($keyword,function ($query) use ($keyword){
                $query->where('name','like',"%{$keyword}%");
            })->orderByDesc('id');

            if($no_page){
                $list = $query->get();
            }else{
                $list = $query->paginate();
            }

            if($is_export){
                return Excel::download(new SupplierExport($list), '供应商.xlsx');
            }

            return $this->success('ok',$list);
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function deleteData($id){
        try {
            Supplier::where('id',$id)->delete();
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function updateData(Request $request,$id){
        try {
            $data = $request->all();
            $validated = Validator::make($data, [
                'name' => 'required',
//                'main_category' => 'required',
//                'main_product' => 'required',
//                'product_ids' => 'required',
            ], [
                'name.required' => '请填写名称',
//                'main_category.required' => '请填写主营大类',
//                'name.unique' => '供应商名不可重复',
//                'product_ids.required' => '请选择产品',
            ]);

            if ($validated->fails()) {
                $errors = $validated->errors()->all();
                $error = count($errors)?$errors[0]:null;
                return $this->failure($error);
            }
            $data['product_ids'] = json_encode($data['product_ids']);
            Supplier::where('id',$id)->update($data);
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function getDetail(Request $request,$id){
        try {
            $detail = Supplier::find($id);
            return $this->success('ok',$detail);
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }
}
