<?php
namespace app\admin\controller\medical;

use app\admin\controller\BaseController;
use app\admin\server\medical\SalesmanServer;

class Salesman extends BaseController
{

    /**
     * Notes:获取预约服务业务员列表
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/27
     */
    public function getSalesmanList()
    {
        [$page,$limit] = $this->getPage();
        $keywords = $this->request->get('keywords');
        $result         = SalesmanServer::getSalesmanList($page,$limit,$keywords);
        return json_success($result, '成功');
    }

    /**
     * Notes:修改业务员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/27
     */
    public function updateSalesman()
    {
        $salesman_id      = $this->request->post('salesman_id');
        $salesman_shop_id    = $this->request->post('salesman_shop_id','');
        $salesman_name     = $this->request->post('salesman_name','');
        $salesman_pwd    = $this->request->post('salesman_pwd','');
        $again_salesman_pwd    = $this->request->post('again_salesman_pwd','');
        $salesman_phone   = $this->request->post('salesman_phone','');
        $salesman_resale_name  = $this->request->post('salesman_resale_name','');
        $salesman_status    = $this->request->post('salesman_status','');
        if (!empty($salesman_pwd) && !empty($again_salesman_pwd)){
            $salf = $this->passwordSalf(6);
            $salesman_pwd = md5(sha1($salesman_pwd.$salf));
        }elseif (!empty($salesman_pwd) && empty($again_salesman_pwd)){
            throw new \Exception('请输入确认密码！');
        }elseif (empty($salesman_pwd) && !empty($again_salesman_pwd)){
            throw new \Exception('请输入密码！');
        }
        $admin_id = $this->request->admin_id;
        $this->validateCheck('medical\SalesmanValidate', ['salesman_id'=>$salesman_id], 'update');
        $result         = SalesmanServer::updateSalesman($salesman_id, $salesman_shop_id, $salesman_name, $salesman_pwd, $salesman_phone, $salesman_resale_name, $salesman_status, $salf, $admin_id);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:删除业务员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/27
     */
    public function delSalesman()
    {
        $salesman_id = $this->request->get('salesman_id');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('medical\SalesmanValidate', ['salesman_id' => $salesman_id], 'info');
        $result         = SalesmanServer::delSalesman($salesman_id, $admin_id);
        if ($result){
            return json_success($result, '删除成功');
        }else{
            throw new \Exception('删除失败！');
        }

    }

    /**
     * Notes:添加业务员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/27
     */
    public function addSalesman()
    {
        $salesman_shop_id    = $this->request->post('salesman_shop_id');
        $salesman_name     = $this->request->post('salesman_name');
        $salesman_pwd    = $this->request->post('salesman_pwd');
        $again_salesman_pwd    = $this->request->post('again_salesman_pwd','');
        $salesman_phone   = $this->request->post('salesman_phone');
        $salesman_resale_name  = $this->request->post('salesman_resale_name');
        $salesman_status    = $this->request->post('salesman_status');
        $salf = $this->passwordSalf(6);
        $admin_id = $this->request->admin_id;
        $this->validateCheck('medical\SalesmanValidate', ['salesman_shop_id' => $salesman_shop_id, 'salesman_name' => $salesman_name, 'salesman_pwd' => $salesman_pwd, 'again_salesman_pwd' => $again_salesman_pwd, 'salesman_phone' => $salesman_phone, 'salesman_resale_name' => $salesman_resale_name, 'salesman_status' => $salesman_status], 'create');
        if ($salesman_pwd !== $again_salesman_pwd){
            throw new \Exception('两次密码输入不一致！');
        }
        $salesman_pwd = md5(sha1($salesman_pwd.$salf));
        $result         = SalesmanServer::insertSalesman($salesman_shop_id, $salesman_name, $salesman_pwd, $salesman_phone, $salesman_resale_name, $salesman_status, $salf, $admin_id);
        return json_success($result, '添加成功');
    }

    /**
     * Notes:查询业务员详情
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/27
     */
    public function salesmanInfo()
    {
        $salesman_id = $this->request->get('salesman_id');
        $this->validateCheck('medical\SalesmanValidate', ['salesman_id' => $salesman_id], 'info');
        $result         = SalesmanServer::salesmanInfo($salesman_id);
        if (!empty($result))
        return json_success($result, '成功');
    }

    /**
     * Notes:修改业务员状态
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/14
     */
    public function updateStatus()
    {
        $salesman_id      = $this->request->get('salesman_id');
        $salesman_status = $this->request->get('salesman_status');
        $admin_id = $this->request->admin_id;
        $this->validateCheck('medical\SalesmanValidate', ['salesman_id'=>$salesman_id], 'info');
        $result         = SalesmanServer::updateStatus($salesman_id, $salesman_status, $admin_id);
        return json_success($result, '修改成功');

    }

}
