<?php

namespace app\workerapi\controller;

use app\common\logic\MasterWorkerExamineLogic;
use app\common\model\dict\DictData;
use app\workerapi\logic\LoginLogic;
use app\workerapi\logic\MasterWorkerInfoLogic;
use app\workerapi\logic\MasterWorkerLogic;
use app\workerapi\validate\BankAccountValidate;
use app\workerapi\validate\MasterWokerInfoValidate;
use app\workerapi\validate\MasterWokerValidate;

class MasterWorkerController extends BaseApiController
{
    public array $notNeedLogin = ['customerPhone'];

    /**
     * @notes 修改密码
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/20 19:16
     */
    public function changePassword()
    {
        $params = (new MasterWokerValidate())->post()->goCheck('changePassword');
        $result = MasterWorkerLogic::changePassword($params, $this->userId);
        if (true === $result) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(MasterWorkerLogic::getError());
    }

    /**
     * 更改手机号
     * @return \think\response\Json
     * @author 林海涛
     * @date 2024/7/10 下午2:23
     */
    public function changeMobile()
    {
        $params = (new MasterWokerValidate())->post()->goCheck('changeMobile');
        $result = MasterWorkerLogic::changeMobile($params, $this->userId);
        if (true === $result) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(MasterWorkerLogic::getError());
    }

    /**
     * 更改身份证信息
     * @return \think\response\Json
     * @author 林海涛
     * @date 2024/7/10 下午4:46
     */
    public function changeIdCard()
    {
        $params =  (new MasterWokerInfoValidate())->post()->goCheck('changeIdCard');
        $result = MasterWorkerInfoLogic::changeIdCard($params, $this->userId);
        if (true === $result) {
            \app\common\logic\MasterWorkerExamineLogic::updateEngineerInformation($this->userId);
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(MasterWorkerInfoLogic::getError());
    }

    public function getIdCard()
    {
        $result = MasterWorkerInfoLogic::getIdCard($this->userId);
        return $this->data($result);
    }

    public function approvalStopWork()
    {
        $result = MasterWorkerLogic::stopWork($this->userId);
        if (true === $result) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(MasterWorkerLogic::getError());
    }


    /**
     * 注销账号
     * @return \think\response\Json
     * @author 林海涛
     * @date 2024/7/10 下午4:46
     */
    public function logOff()
    {
        $result = MasterWorkerLogic::logOff($this->userId);
        if (true === $result) {
            LoginLogic::logout($this->userInfo);
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(MasterWorkerInfoLogic::getError());
    }

    /**
     * 绑定银行卡
     * @return \think\response\Json
     * @author 林海涛
     * @date 2024/7/11 下午4:27
     */
    public function bindBankAccount()
    {
        $params =  (new BankAccountValidate())->post()->goCheck('bindBankAccount');
        $result = MasterWorkerInfoLogic::bindBankAccount($params, $this->userId);
        if (true === $result) {
            \app\common\logic\MasterWorkerExamineLogic::updateEngineerInformation($this->userId);
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(MasterWorkerInfoLogic::getError());
    }

    public function myInfo()
    {
        $result = MasterWorkerLogic::detail($this->userId);
        return $this->data($result);
    }


    public function bankAccountInfo()
    {
        $result = MasterWorkerInfoLogic::bankAccountInfo($this->userId);
        return $this->data($result);
    }

    /**
     * 客服电话
     * @return \think\response\Json
     */
    public function customerPhone()
    {
        $result = DictData::where(['type_value' => 'customer_support'])->column('value', 'name');
        return $this->data($result);
    }

    public function setInfo()
    {
        $params = (new MasterWokerValidate())->post()->goCheck('setInfo');

        $result = MasterWorkerLogic::setInfo($this->userId, $params);
        if (false === $result) {
            return $this->fail(MasterWorkerLogic::getError());
        }
        return $this->success('操作成功', $result, 1, 1);
    }
}