<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\DepartmentService;
use App\Http\Services\SupplierShareApplyService;
use App\Model\SupplierChannelModel;
use Illuminate\Http\Request;

//供应商共享申请
class SupplierShareApplyApiController extends Controller
{
    public function Entrance(Request $request, $id)
    {
        $this->$id($request, $id);
    }

    public function GetSupplierShareApplyList($request)
    {
        $userId = $request->user->userId;
        $applyService = new SupplierShareApplyService();
        $list = $applyService->getSupplierShareApplyList($userId);
        $this->response(0, 'ok', $list['data'], $list['total']);
    }

    //校验申请的供应商,并且还要返回所属部门列表
    public function CheckApplySupplierShare($request)
    {
        $supplierName = trim($request->get('supplier_name'));
        if (empty($supplierName)) {
            $this->response(-1, '供应商名称不能为空');
        }
        $supplierModel = new SupplierChannelModel();
        //下面的处理是为了兼容有问题的数据
        if (!empty($supplier = $supplierModel->where('supplier_name', $supplierName)->first())) {
//            $supplier = $supplierModel->where('supplier_name', 'like', "%$supplierName%")->whereNotIn('status',
//                [SupplierChannelModel::STATUS_DISABLE, SupplierChannelModel::STATUS_REJECT])->first();
        }
        if (empty($supplier)) {
            $this->response(-1, '该供应商名称不存在,请输入正确供应商名称');
        }
        $userId = $request->user->userId;
        $codeId = $request->user->codeId;
        if (empty($codeId)) {
            $this->response(-1, '你还没有绑定内部编码,无法申请共用');
        }
        //要判断自己是不是有这个供应商了,有的话没必要申请
        $isOwn = $supplierModel->where('supplier_name', $supplierName)->where(function ($q) use ($userId, $codeId) {
            $q->where('create_uid', $userId);
            if (!empty($codeId)) {
                $q->orWhere('purchase_uid', $codeId)
                    ->orwhere('channel_uid', 'like', "%$codeId%");
            }
        })->where('is_type', 0)->first();
        if ($isOwn) {
            $this->response(-1, '你已经可以管理该供应商,无需申请共用');
        }
        if (empty($supplier['create_uid']) && empty($supplier['channel_uid']) && empty($supplier['purchase_uid'])) {
            $this->response(-1, '该供应商没有相关的所属人信息,请联系管理员');
        }
        $departmentService = new DepartmentService();
        $department = $departmentService->getUpperDepartmentByUserIdForShareApply($request->user->userId);
        $applyService = new SupplierShareApplyService();
        $departments = $applyService->getApplyCanUseDepartments($supplier);
        //获取当前人的部门id,如果部门列表里面包含自己的部门,则提示该供应商属于自己部门,请找主管分配
        if (in_array($department, $departments)) {
            $this->response(-1, '该供应商属于自己部门,请找主管分配');
        }
        if (empty($departments)) {
            $this->response(-1, '该供应商不存在相关联的部门');
        }
        $this->response(0, '匹配到供应商数据,可以下拉选择你需要申请的部门', $departments, $supplier['supplier_id']);
    }

    //保存共用申请
    public function SaveSupplierShareApply($request)
    {
        $map = $request->only([
            'apply_department_id',
            'supplier_id',
        ]);
        if (empty($map['apply_department_id']) || empty($map['supplier_id'])) {
            $this->response(-1, '缺少参数');
        }
        if (empty($request->user->codeId)) {
            $this->response(-1, '你还没有绑定内部编码,无法申请共用');
        }
        $map['apply_code_id'] = $request->user->codeId;
        $applyService = new SupplierShareApplyService();
        $hasNoFinish = $applyService->checkHasNoFinishApply($map);
        if ($hasNoFinish) {
            $this->response(-1, '你存在对该供应商的申请,正在审核流程中,请等待对应的审核流程走完方可针对该供应商进行新的共用申请');
        }
        $result = $applyService->saveSupplierShareApply($map);
        if (!$result) {
            $this->response(-1, '申请失败,系统错误');
        }
        return $this->response(0, '申请成功');
    }

    //获取共用审核列表
    //1.审核分初审和复审
    //2.有审核权限都可以看到这个列表
    public function GetAuditSupplierShareApplyList($request)
    {
        $applyService = new SupplierShareApplyService();
        $userId = $request->user->userId;
        $list = $applyService->getAuditSupplierShareApplyList($userId);
        $this->response(0, 'ok', $list['data'], $list['total']);
    }

    //审核
    public function AuditSupplierShareApply($request)
    {
        $statusName = $request->get('status');
        $applyService = new SupplierShareApplyService();
        $userId = $request->user->userId;
        $id = $request->get('id');
        $result = $applyService->auditSupplierShareApply($id, $userId, $statusName);
        if (!$result) {
            $this->response(-1, '审核操作失败');
        }
        return $this->response(0, '审核操作成功');
    }

}