<?php

namespace app\enterprise\controller;

use app\admin\model\Enterprise;
use app\common\api\DictApi;
use app\common\model\TalentChecklog;
use app\enterprise\model\EnterpriseRecord;
use app\enterprise\common\EnterpriseController;
use app\Request;
use think\exception\ValidateException;

class Api extends EnterpriseController {

    public function findEnterpriseChangeByPage()
    {
        $order = trim($this->request->param("order")) ?: "desc";
        $offset = trim($this->request->param("offset")) ?: 0;
        $limit = trim($this->request->param("limit")) ?: 10;

        $list = EnterpriseRecord::where('mainId',session("user")["uid"])->limit($offset, $limit)->order('createTime ' . $order)->select()->toArray();
        $count = count($list);

        return json(["total" => $count, "rows" => $list]);
    }

    public function findUnfinishedChangeRecord(){
        return EnterpriseRecord::where('mainId',session("user")["uid"])->where('checkState',4)->select()->toArray();
    }

    public function toAdd()
    {
        $ep = Enterprise::where('id',session("user")["uid"])->find();
        $ecr = [
            'enterprise_id' => $ep['id'],
            'newName' => $ep['name'],
            'newIdCard' => $ep['idCard'],
            'newStreet' => $ep['street'],
            'newAddress' => $ep['address'],
            'newLegal' => $ep['legal'],
            'newEphone' => $ep['ephone'],
            'newAgentName' => $ep['agentName'],
            'newAgentEmail' => $ep['agentEmail'],
            'newAgentPhone' => $ep['agentPhone'],
            'type' => $ep['type'],
            'newEnterpriseTag' => $ep['enterpriseTag'],
            'newIndustryFieldNew' => $ep['industryFieldNew'],
            'newIndustryFieldOld' => $ep['industryFieldOld']
        ];



        switch ($ep['checkState']){
            case 1:
                $ecr['checkStateName'] = '保存未提交审核';
                break;
            case 2:
                $ecr['checkStateName'] = '待审核';
                break;
            case 3:
                $ecr['checkStateName'] = '审核驳回';
                break;
            case 4:
                $ecr['checkStateName'] = '审核通过';
                break;
            case 5:
                $ecr['checkStateName'] = '重新提交';
                break;
            default:
                $ecr['checkStateName'] = '';
        }

        return view("", ['ecr' => $ecr]);
    }

    public function upsert(){
        $data = [
            'id' => \StrUtil::getRequestDecodeParam($this->request,'enterprise_id'),
            'name' => \StrUtil::getRequestDecodeParam($this->request,'newName'),//单位名称
            'idCard' => \StrUtil::getRequestDecodeParam($this->request,'newIdCard'),//社会信用代码
            'agentName' => \StrUtil::getRequestDecodeParam($this->request,'newAgentName'),//经办人
            'agentPhone' => \StrUtil::getRequestDecodeParam($this->request,'newAgentPhone'),//经办人电话
            'legal' => \StrUtil::getRequestDecodeParam($this->request,'newLegal'),//法人
            'street' => \StrUtil::getRequestDecodeParam($this->request,'newStreet'),//镇街
            'address' => \StrUtil::getRequestDecodeParam($this->request,'newAddress'),//地址
            'type' => intval($this->request['type']),
            'enterpriseTag' => \StrUtil::getRequestDecodeParam($this->request,'newEnterpriseTag'),//单位标签
            'agentEmail' => \StrUtil::getRequestDecodeParam($this->request,'newAgentEmail'),//邮箱
            'ephone' => \StrUtil::getRequestDecodeParam($this->request,'newEphone'),//单位电话
            'industryFieldNew' => \StrUtil::getRequestDecodeParam($this->request,'newIndustryFieldNew'),//产业领域
            'industryFieldOld' => \StrUtil::getRequestDecodeParam($this->request,'newIndustryFieldOld'),//行业领域
            'bankCard' => \StrUtil::getRequestDecodeParam($this->request,'newBankCard'),//银行
            'bank' => \StrUtil::getRequestDecodeParam($this->request,'newBank'),//开户行
            'bankNetwork' => \StrUtil::getRequestDecodeParam($this->request,'newBankNetwork')//网点
        ];

        try {
            validate(\app\common\validate\Enterprise::class)->batch(true)->scene('change')->check($data);
            $ep = Enterprise::where('id',session("user")["uid"])->find();
            $record_id = \StrUtil::getRequestDecodeParam($this->request,'id');
            if($record_id){
                $record_data = [
                    'mainId' => $data['id'],
                    'type' => $data['type'],
                    'oldName' => $ep['name'],
                    'oldIdCard' => $ep['idCard'],
                    'oldIndustryFieldNew' => $ep['industryFieldNew'],
                    'oldIndustryFieldOld' => $ep['industryFieldOld'],
                    'oldStreet' => $ep['street'],
                    'oldAddress' => $ep['address'],
                    'oldLegal' => $ep['legal'],
                    'oldEphone' => $ep['ephone'],
                    'oldAgentName' => $ep['agentName'],
                    'oldAgentEmail' => $ep['agentEmail'],
                    'oldAgentPhone' => $ep['agentPhone'],
                    'oldEnterpriseTag' => $ep['enterpriseTag'],
                    'oldBankCard' => $ep['bankCard'],
                    'oldBank' => $ep['bank'],
                    'oldBankNetwork' => $ep['bankNetwork'],
                    'newName' => htmlspecialchars($data['name']),
                    'newIdCard' => htmlspecialchars($data['idCard']),
                    'newIndustryFieldNew' => $data['industryFieldNew'],
                    'newIndustryFieldOld' => $data['industryFieldOld'],
                    'newStreet' => $data['street'],
                    'newAddress' => $data['address'],
                    'newLegal' => $data['legal'],
                    'newEphone' =>  $data['ephone'],
                    'newAgentName' => $data['agentName'],
                    'newAgentEmail' => $data['agentEmail'],
                    'newAgentPhone' => $data['agentPhone'],
                    'newEnterpriseTag' => $data['enterpriseTag'],
                    'newBankCard' => $data['bankCard'],
                    'newBank' => $data['bank'],
                    'newBankNetwork' => $data['bankNetwork'],
                    'checkState' => 1,
                    'createTime' => date("y-m-d H:i:s",time()),
                    'createUser' => session("user")["uid"]
                ];
                EnterpriseRecord::create($record_data);
            }

            $log = [
                'type' => 10,
                'active' => 1,
                'state' => 1,
                'step' => 100,
                'stateChange' => '保存未提交',
                'createTime' => date("y-m-d H:i:s",time()),
                'createUser' => '用户'
            ];



        } catch (ValidateException $e){
            $error = $e->getError();
            return json(["msg" => array_pop($error)],500);
        }
        dd($this->request->post());

    }
}