<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\SupplierExaminationService;
use App\Http\Transformers\SupplierLogTransformer;
use App\Http\Validators\SupplierExaminationValidator;
use App\Model\LogModel;
use App\Model\SupplierLogModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

//供应商检测
class SupplierExaminationApiController extends Controller
{
    public function Entrance(Request $request, $id)
    {
        $this->$id($request, $id);
    }

    //获取供应商信息变更记录
    public function GetSupplierExaminationList($request)
    {
        $model = new SupplierExaminationService();
        $list = $model->getSupplierExaminationList($request);
        $this->response(0, 'ok', $list['data'], $list['total']);
    }

    //添加
    public function AddSupplierExamination($request)
    {
        $params = $request->only([
            'order_sn',
            'purchase_sn',
            'examine_time',
            'sales_name',
            'purchase_name',
            'ticket_type',
            'supplier_name',
            'sku_name',
            'brand_name',
            'amount',
            'batch',
            'producing_area',
            'stock_in_date',
            'income_sn',
            'delivery_sn',
            'tally_request',
            'examine_request',
            'unhealthy_amount',
            'abnormal_level',
            'unhealthy_content',
            'examine_result',
            'remark',
        ]);
        $validator = new SupplierExaminationValidator();
        $validateResult = $validator->checkSave($params);
        if ($validateResult) {
            $this->response(-1, $validateResult);
        }
        $result = (new SupplierExaminationService())->saveSupplierExamination($params);
        if ($result) {
            $this->response(0, '添加成功');
        }
        $this->response(-1, '添加失败', $result);
    }

    //修改
    //添加
    public function UpdateSupplierExamination($request)
    {
        $params = $request->only([
            'id',
            'order_sn',
            'purchase_sn',
            'examine_time',
            'sales_name',
            'purchase_name',
            'ticket_type',
            'supplier_name',
            'sku_name',
            'brand_name',
            'amount',
            'batch',
            'producing_area',
            'stock_in_date',
            'income_sn',
            'delivery_sn',
            'tally_request',
            'examine_request',
            'unhealthy_amount',
            'abnormal_level',
            'unhealthy_content',
            'examine_result',
            'remark',
        ]);
        $validator = new SupplierExaminationValidator();
        $validateResult = $validator->checkSave($params);
        if ($validateResult) {
            $this->response(-1, $validateResult);
        }
        $result = (new SupplierExaminationService())->saveSupplierExamination($params);
        if ($result) {
            $this->response(0, '修改成功');
        }
        $this->response(-1, '修改失败', $result);
    }

    //删除
    public function DeleteSupplierExaminations($request)
    {
        $ids = $request->get('ids');
        $ids = explode(',', trim($ids));
        if (empty($ids)) {
            $this->response(-1, '请选择需要删除的数据');
        }
        $result = (new SupplierExaminationService())->deleteSupplierExaminations($ids);
        if ($result) {
            $this->response(0, '删除成功');
        }
        $this->response(-1, '删除失败', $result);
    }

    public function BatchUpdateSupplierExamination($request)
    {
        $params = $request->only([
            'ids',
            'examine_result'
        ]);
        if (empty($params['ids'])) {
            $this->response(-1, '请选择需要批量修改的检测数据');
        }
        $result = (new SupplierExaminationService())->batchUpdateSupplierExamination($params);
        if ($result) {
            $this->response(0, '修改成功');
        }
        $this->response(-1, '修改失败', $result);
    }

    //上传IQC检测记录
    private function ImportSupplierExamination($request)
    {
        $file = $request->file('file');
        if ($file->isValid()) {
            $ext = $file->getClientOriginalExtension();
            $realPath = $file->getRealPath();
            if ($ext != 'xlsx') {
                $this->response(-1, '上传格式错误');
            }
            // 上传文件
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            $bool = Storage::put($filename, file_get_contents($realPath));
            if (!$bool) {
                $this->response(-1, '文件处理失败');
            }
            $filePath = storage_path('app/') . $filename;

            $result = (new SupplierExaminationService())->ImportSupplierExamination($filePath);
            if ($result !== true) {
                $this->response(-1, $result);
            }
            $this->response(0, '上传成功');
        }
    }
}