<?php

use App\Base\Base;
use App\Services\marketing\DistributorService;
use App\Exception\custom\DistributorException;
use Api\PhpUtils\Validate\Validate;
use \Validate\CheckIsDistributorValidate;


class DistributorController extends Base
{

    public function get_distributorAction()
    {
        $params = $this->params;
        $distributionList = DistributorService::getDistributorList($params);
        $this->success(['result' => $distributionList]);
    }

    public function importAction()
    {
       $distributionList = DistributorService::import();

       print_r($distributionList);
    }
    /**
     * 二级团长列表
     *
     */
    public function second_distributor_listAction()
    {
        $params = $this->params;
        $distributionList = DistributorService::secondDistributor($params);
        $this->success($distributionList);
    }
    
    /**
     * 申请团长
     *
     */
    public function apply_distributorAction()
    {
        $params = $this->params;
        $distributionList = DistributorService::applyDistributor($params);
        $this->success();
    }


    /**
     * 申请二级团长
     *
     */
    public function apply_second_distributorAction()
    {
        $params = $this->params;
        $distributionList = DistributorService::applySecondDistributor($params);
        $this->success();
    }
    
    /**
     * 申请发送短信
     *
     */
    public function send_sms_codeAction()
    {
        $params = $this->params;

        $mobile = !empty($params['mobile']) ? $params['mobile'] : '';
        if (empty($mobile)) {
            throw new DistributorException(['cus'=>6]);
        }

        $is_mobile =  (new Validate)->isMobile($mobile);
        if (empty($is_mobile)) {
            throw new DistributorException(['cus'=>7]);
        }
       
        $params = [
            'mobile' => !empty($mobile) ? $mobile : '',
        ];

        $send_data = DistributorService::sendSmsCode($params);
        if (!empty($send_data) && $send_data['status'] == 'success' && $send_data['code'] == 0 ) {
            $res  = [
                'reason' => 'ok',
            ];
            $this->success($res);
        }else{
            $this->failed($send_data['code'],$send_data['reason'],'failed');
            die();
        }
    }

    /**
     * 获取是否是
     *
     */
    public function distributorAction()
    {
        $params = $this->params;
        $info = DistributorService::distributor($params);
        $this->success(['result' => $info]);
    }
    
    /**
     * 获取分享二维码
     *
     */
    public function distributor_code_urlAction()
    {
        $params = $this->params;
        $info = DistributorService::distributorCodeUrl($params);
        $this->success(['result' => $info]);
    }

    /**
     * 获取待审核的团长列表
     * 后台管理
     *
     */
    public function distributor_listAction()
    {
        $params = $this->params;
        $distributionList = DistributorService::distributorList($params);
        $this->success($distributionList);
    }
    
    /**
     * 获取团长列表
     * 后台管理
     *
     */
    public function get_distributor_listAction()
    {
        $params = $this->params;
        $distributionList = DistributorService::getDistributorList($params);
        $this->success($distributionList);
    }
    
    /**
     * 获取详情数据
     * 后台管理
     *
     */
    public function distributor_infoAction()
    {
        $params = $this->params;
        $distributorInfo = DistributorService::distributorInfo($params);
        $this->success(['result' => $distributorInfo]);
    }
    
    /**
     * 更新数据
     * 后台管理
     *
     */
    public function update_distributorAction()
    {
        $params = $this->params;

        $where['distributor_id'] = $params['distributor_id'];
        
        if (!empty($params['audit_status'])) {
            $param['audit_status'] = $params['audit_status'];
        }

        if (!empty($params['audit_user_name'])) {
            $param['audit_user_name'] = $params['audit_user_name'];
        }
         
        $srt = DistributorService::updateDistributor($param,$where);

        if (!empty($srt)) {
            $this->success();
        }else{
            throw new DistributorException(['cus'=>1]);
        }
    }
    
    /**
     * 添加团长
     * 后台管理
     *
     */
    public function add_distributorAction()
    {
         
    }


    /**
     * 判断是否是团长
     * @throws Exception
     */
    public function is_distributorAction()
    {
        (new CheckIsDistributorValidate())->validate();

        $params = $this->params;
        $distributorList = DistributorService::getDistributorInfo([$params["user_id"]]);
        if (!empty($distributorList[$params["user_id"]])) {
            $this->success(["is_distributor"=>true]);
        }
        $this->success(["is_distributor"=>false]);
    }



}