<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\controller\auth;

use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\auth\AdminLists;
use app\adminapi\validate\auth\AdminValidate;
use app\adminapi\logic\auth\AdminLogic;
use app\adminapi\validate\auth\editSelfValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
use app\api\logic\SmsLogic;
use app\api\controller\JunziqianController;

/**
 * 管理员控制器
 * Class AdminController
 * @package app\adminapi\controller\auth
 */
class AdminController extends BaseAdminController
{

    /**
     * @notes 查看管理员列表
     * @return \think\response\Json
     * @author 段誉
     * @date 2021/12/29 9:55
     */
    public function lists()
    {
        return $this->dataLists(new AdminLists());
    }


    /**
     * @notes 添加管理员
     * @return \think\response\Json
     * @author 段誉
     * @date 2021/12/29 10:21
     */
    public function add()
    {
        $params = Request::param();

        if (empty($params['party_a'])) {
            return $this->fail('签约方不能为空');
        }
        try {
            $params['phone'] = $params['account'];
            $params['password'] = 'm' . $params['account'];
            $params['password_confirm'] = 'm' . $params['account'];
            $params['root'] = 0;
            $params['qualification'] = json_encode($params['qualification']);
            $params['disable'] = 1;
            validate(AdminValidate::class)
                ->scene('add')->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->fail($e->getError());
        }
        unset($params['id']);
        $result = AdminLogic::add($params);
        if (true === $result) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(AdminLogic::getError());
    }


    /**
     * @notes 编辑管理员
     * @return \think\response\Json
     * @author 段誉
     * @date 2021/12/29 11:03
     */
    public function edit()
    {
        $params = (new AdminValidate())->post()->goCheck('edit');
        if (empty($params['party_a'])) {
            return $this->fail('签约方不能为空');
        }
        $params['disable'] = $params['disable'] ?? 0;
        $params['qualification'] = json_encode($params['qualification']);
        $result = AdminLogic::edit($params);
        if (true === $result) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(AdminLogic::getError());
    }


    /**
     * @notes 删除管理员
     * @return \think\response\Json
     * @author 段誉
     * @date 2021/12/29 11:03
     */
    public function delete()
    {
        $params = (new AdminValidate())->post()->goCheck('delete');
        $result = AdminLogic::delete($params);
        if (true === $result) {
            return $this->success('操作成功', [], 1, 1);
        }
        return $this->fail(AdminLogic::getError());
    }


    /**
     * @notes 查看管理员详情
     * @return \think\response\Json
     * @author 段誉
     * @date 2021/12/29 11:07
     */
    public function detail()
    {
        $params = (new AdminValidate())->goCheck('detail');
        $result = AdminLogic::detail($params);
        return $this->data($result);
    }


    /**
     * @notes 获取当前管理员信息
     * @return \think\response\Json
     * @author 段誉
     * @date 2021/12/31 10:53
     */
    public function mySelf()
    {
        $result = AdminLogic::detail(['id' => $this->adminId], 'auth');
        return $this->data($result);
    }


    /**
     * @notes 编辑超级管理员信息
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/4/8 17:54
     */
    public function editSelf()
    {
        $params = (new editSelfValidate())->post()->goCheck('', ['admin_id' => $this->adminId]);
        $result = AdminLogic::editSelf($params);
        return $this->success('操作成功', [], 1, 1);
    }
    // /**生成合同 */
    public function Draftingcontracts()
    {
        $params = Request::param();
        $result = AdminLogic::detail($params);
        if ($result && $result['contract'] && $result['contract']['file'] != '') {
            $data = [
                'name' => $result['name'] . '合同',
                'signatories' => [['fullName' => $result['name'], 'identityType' => 1, 'identityCard' => $result['id_card'], 'mobile' => $result['account'],  'noNeedVerify' => 1, 'signLevel' => 1]],
                'url' => $result['contract']['file']
            ];
            $res = app(JunziqianController::class)->Signing($data);
            if ($res->success == true) {
                Db::name('contract')->where('id', $result['contract']['id'])->update(['contract_no' => $res->data]);
                $data = array(
                    "applyNo" => $res->data, //TODO *
                    "fullName" => $result['name'], //TODO *
                    "identityCard" => $result['id_card'], //TODO *
                    "identityType" => 1, //TODO *
                );
                $res = app(JunziqianController::class)->SigningLink($data);
                if ($res->success == true) {
                    Db::name('contract')->where('id', $result['contract']['id'])->update(['url' => $res->data]);
                    //发送短信
                    $sms = [
                        'mobile' => $result['phone'],
                        'name' => $result['name'],
                        'type' => '《' . $result['contract']['contract_type_name'] . '》',
                        'code' => 'api/Hetong/url?id=' . $result['contract']['id'],
                        'scene' => 'WQ'
                    ];
                    $result = SmsLogic::contractUrl($sms);
                    if (true === $result) {
                        return $this->success('发送成功');
                    } else {
                        return $this->fail(SmsLogic::getError());
                    }
                } else {
                    return $this->fail($res->msg);
                }
                return $this->success('生成合同成功', [], 1, 1);
            } else {
                return $this->fail($res->msg);
            }
        } else {
            return $this->fail('生成合同成功失败,联系管理员');
        }
    }

    //**发送短信 */
    public function postsms()
    {
        $params = Request::param();
        $admin=Db::name('admin')->where('id',$params['id'])->find();
        $find = Db::name('contract')->where('party_b', $params['id'])
            ->withAttr('contract_type_name', function ($value, $data) {
                return Db::name('dict_data')->where('id', $data['contract_type'])->value('name');
            })
            ->find();
        if ($find && $find['url'] != '') {
            //发送短信
            $sms = [
                'mobile' => $admin['phone'],
                'name' => $admin['name'],
                'type' => '《' . $find['contract_type_name'] . '》',
                'code' => 'api/Hetong/url?id=' . $find['id'],
                'scene' => 'WQ'
            ];
            $result = SmsLogic::contractUrl($sms);
            if (true === $result) {
                return $this->success('发送成功');
            } else {
                return $this->fail(SmsLogic::getError());
            }
        }
    }
}