<?php

namespace App\Http\Controllers\Supplier;

use App\Http\Controllers\Controller;
use App\Models\Log;
use App\Models\Log as LogModel;
use App\Models\Suppliers;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Validator;

class SuppliersController extends Controller
{
    public function __construct(Request $request)
    {
        $this->log = new LogModel([
            'module' => 'supplier',
            'action' => $request->getMethod(),
            'target_type' => 'supplier',
        ]);
    }

    public function index(Request $request)
    {
        $suppliers = Suppliers::query()->paginate($request->get('per_page'));
        return JsonResource::collection($suppliers);
    }

    public function store(Request $request)
    {
        //获取所有参数
        $allParams = $request->all();
        //进行校验验证
        $validator = Validator::make($allParams, [
            'supplier_name' => 'required|string',
            'company_name' => 'required|string',
            'address' => 'sometimes|string',
            'link_tel' => 'sometimes|string',
            'payment_account' => 'sometimes|string',
            'supply_type' => 'sometimes|string',
            'agent_id' => 'sometimes|integer',
            'agent_name' => 'sometimes|string',
        ]);
        if ($validator->fails()) {
            //校验失败返回异常
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
            return response($this->res, $this->res['httpCode']);
        }
        //保存数据
        $suppliers = new Suppliers();
        $suppliers->supplier_name = $request->supplier_name;
        $suppliers->company_name = $request->company_name ?? '';
        $suppliers->link_tel = $request->link_tel ?? '';
        $suppliers->address = $request->address ?? '';
        $suppliers->payment_account = $request->payment_account ?? '';
        $suppliers->supply_type = $request->supply_type ?? '';
        $suppliers->agent_id = $request->agent_id ?? 0;
        $suppliers->agent_name = $request->agent_name ?? '';
        $suppliers->save();

        return response($this->res, $this->res['httpCode']);
    }


    public function update($id, Request $request)
    {
        //获取所有参数
        $allParams = $request->all();
        //进行校验验证
        $validator = Validator::make($allParams, [
            'supplier_name' => 'required|string',
            'company_name' => 'required|string',
            'address' => 'sometimes|string',
            'link_tel' => 'sometimes|string',
            'payment_account' => 'sometimes|string',
            'supply_type' => 'sometimes|string',
            'agent_id' => 'sometimes|integer',
            'agent_name' => 'sometimes|string',
        ]);
        if ($validator->fails()) {
            //校验失败返回异常
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());
            return response($this->res, $this->res['httpCode']);
        }
        //保存数据
        $suppliers = Suppliers::query()->find($id);
        $suppliers->supplier_name = $request->supplier_name;
        $suppliers->company_name = $request->company_name ?? '';
        $suppliers->link_tel = $request->link_tel ?? '';
        $suppliers->address = $request->address ?? '';
        $suppliers->payment_account = $request->payment_account ?? '';
        $suppliers->supply_type = $request->supply_type ?? '';
        $suppliers->agent_id = $request->agent_id ?? 0;
        $suppliers->agent_name = $request->agent_name ?? '';
        $suppliers->save();

        return response($this->res, $this->res['httpCode']);
    }

    public function destroy($id)
    {
        $goodsType = Suppliers::query()->find($id);
        $goodsType->delete();
        $this->addLog($id, 'status');

        return response($this->res, $this->res['httpCode']);
    }
}
