<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\LogService;
use App\Http\Services\SupplierAttachmentService;
use App\Http\Transformers\SupplierAttachmentTransformer;
use App\Http\Validators\SupplierAttachmentValidator;
use App\Model\LogModel;
use App\Model\SupplierAttachmentsModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierAttachmentModel;
use Illuminate\Http\Request;

class SupplierAttachmentApiController extends Controller
{
    public function Entrance(Request $request, $id)
    {
        $this->$id($request, $id);
    }

    //获取供应商收款信息
    public function GetSupplierAttachmentList($request)
    {
        $supplierId = $request->get('supplier_id');
        $limit = $request->get('limit', 10);
        $model = new SupplierAttachmentsModel();
        $model->where('supplier_id', $supplierId)->paginate();
        $list = $model->where('supplier_id', $supplierId)->orderBy('attachment_id', 'desc')
            ->paginate($limit)->toArray();
        $transformer = new SupplierAttachmentTransformer();
        $list['data'] = $transformer->transformList($list['data']);
        $this->response(0, 'ok', $list['data'], $list['total']);
    }

    //获取供应商信息变更记录
    public function SaveSupplierAttachment($request)
    {
        //先去表单验证
        $validator = new SupplierAttachmentValidator();
        $validateResult = $validator->checkSave($request);
        if ($validateResult) {
            $this->response(-1, $validateResult);
        }
        $attachment = $request->only([
            'field_name',
            'validity_type',
            'validity_period',
            'description',
            'file_name',
            'file_url',
            'supplier_id',
            'attachment_id',
        ]);
        $attachmentService = new SupplierAttachmentService();
        $result = $attachmentService->saveAttachment($attachment);
        if (!$result) {
            $this->response(-1, '操作失败');
        } else {
            $logService = new LogService();
            $content = !empty($attachment['attachment_id']) ? '修改附件信息' : '添加附件信息';
            $remark = json_encode($attachment);
            $logService->AddLog($attachment['supplier_id'], LogModel::UPDATE_OPERATE, '修改供应商基本资料', $content, $remark);
            $this->response();
        }
    }

    //判断是否要进入审核中状态,因为部分字段修改是不需要走审核的
    private function checkNeedAudit($oldAttachment, $newAttachment)
    {
        $notNeedAuditField = [
            'remark',
        ];
        $diff = array_diff($oldAttachment, $newAttachment);
        unset($diff['update_time']);
        $changeField = array_keys($diff);
        foreach ($changeField as $filed) {
            //只要有一个不存在于不需要审核的字段,就返回需要审核
            if (!in_array($filed, $notNeedAuditField)) {
                return true;
            }
        }
        return false;
    }

    //删除
    public function DeleteSupplierAttachment($request)
    {
        $attachmentId = $request->get('attachment_id');
        $model = new SupplierAttachmentsModel();
        $attachment = $model->where('attachment_id', $attachmentId)->first()->toArray();
        $result = $model->where('attachment_id', $attachmentId)->delete();
        if ($result) {
            $logService = new LogService();
            $content = "删除附件信息 : " . $attachment['type_name'];
            $remark = json_encode($attachment);
            $logService->AddLog($attachment['supplier_id'], LogModel::UPDATE_OPERATE, '删除附件信息', $content, $remark);
            $this->response(0, '操作成功');
        }
        $this->response(-1, '操作失败');
    }
}