<?php
namespace app\modules\manage\v1\controllers;

use app\modules\common\Helper;
use app\modules\logic\model\RegionModel;
use app\modules\logic\model\SysProductSetModel;
use app\modules\logic\model\SysAreaSetModel;
use app\modules\logic\model\SysCarmodelSetModel;
use app\modules\logic\model\MerchTypeModel;
use app\modules\logic\services\UploadService;
use app\modules\logic\model\UsersModel;
use Yii;
use yii\web\Controller;

class CommonController extends MiddleController {
	/**
	 * 返回
	 * @param $code
	 * @param string $info
	 * @param string $controller
	 * @param string $total
	 */
	protected function response($code, $info = '', $total = '', $controller = 'common') {
		return parent::response($code, $info, $total, $controller);
	}
	/**
	 * [actionGetregion 获取地区]
	 * @apiDoc
	 * @api     v1/common/getregion
	 * @group   common
	 * @name    获取地区
	 * @desc    获取全部地区或下级市/县
	 * @method  POST
	 * @param type string 获取类型【all:全部,next:单独】 noreq
	 * @param parent_code string 上级code【获取全部省传0】 reg
	 * @param is_open string 是否开通【0，否，1，是】 req
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
			"code": "000000",
			"msg": "操作成功",
			"total": 35,
			"info": [
			{
			    "id": "4",
			    "name": "山西省",
			    "class": "1",//1:省;2:市;3:区
			    "code": "140000"
			},
			{
			    "id": "5",
			    "name": "内蒙古自治区",
			    "class": "1",
			    "code": "150000"
			},
			{
			    "id": "6",
			    "name": "辽宁省",
			    "class": "1",
			    "code": "210000"
			}
			]
		}
	 */
	public function actionGetregion(){
		if (empty($this->para['type']) || !in_array($this->para['type'], ['all','next'])) {
            return $this->response('para_miss', '请选择输出类型');
        }
        $Region = new RegionModel;
        $res =[];
        switch ($this->para['type']) {
        	case 'all':
        		$res = $Region->getWidgetRows(['cols'=>['id','name','class','code']]);
        		break;
        	case 'next':
				if (!isset($this->para['parent_code'])) {
					return $this->response('para_miss', '请选择对应类型');
				}
				$param = ['cols'=>['id','name','class','code'],'parent_code'=>$this->para['parent_code']];
				if(isset($this->para['is_open'])){
					$param['is_open'] = $this->para['is_open'];
				}
				$res = $Region->getWidgetRows($param);
        	default:
        }
        return $this->response('search_succ', $res,count($res));

	}
	/**
	 * [actionGetopenarea 获取开通城市]
	 * @apiDoc
	 * @api    v1/common/getopenarea
	 * @group  common
	 * @name   获取开通地区
	 * @desc   获取开通地区
	 * @method POST
	 * @return json
		{
		"code": "000000",
		"msg": "操作成功",
		"info": [
		{
		    "id": "3",
		    "name": "河北省",
		    "class": "1",
		    "code": "130000",
		    "city_list": [
		        {
		            "id": "39",
		            "name": "石家庄市",
		            "class": "2",
		            "code": "130100",
		            "area_list": []
		        },
		        {
		            "id": "41",
		            "name": "秦皇岛市",
		            "class": "2",
		            "code": "130300",
		            "area_list": [
		                {
		                    "id": "457",
		                    "name": "山海关区",
		                    "class": "3",
		                    "code": "130303"
		                }
		            ]
		        }
		    ]
		},
		{
		    "id": "23",
		    "name": "四川省",
		    "class": "1",
		    "code": "510000",
		    "city_list": [
		        {
		            "id": "275",
		            "name": "成都市",
		            "class": "2",
		            "code": "510100",
		            "area_list": [
		                {
		                    "id": "2641",
		                    "name": "武侯区",
		                    "class": "3",
		                    "code": "510107"
		                },
		                {
		                    "id": "2648",
		                    "name": "双流县",
		                    "class": "3",
		                    "code": "510122"
		                }
		            ]
		        },
		        {
		            "id": "278",
		            "name": "泸州市",
		            "class": "2",
		            "code": "510500",
		            "area_list": []
		        }
		    ]
		}
		]
		}
	 */
	public function actionGetopenarea(){
		$SysAreaSet = new SysAreaSetModel();
		$Region = new RegionModel;
		$rows = $SysAreaSet->getWidgetRows(['cols'=>['prov_id','city_id','area_id'],'status'=>'allow']);
		if(!empty($rows)){
			$prov_arr = array_unique(array_column($rows, 'prov_id'));
			$city_arr = array_unique(array_column($rows, 'city_id'));
			$area_arr = array_unique(array_column($rows, 'area_id'));
			$list_rows = $Region->getWidgetRows(['cols'=>['id','name','class','code'],'in'=>['id'=>$prov_arr]]);
			if(!empty($list_rows) && !empty($city_arr)){
				foreach ($list_rows as &$value) {
					$value['city_list'] = $Region->getWidgetRows(['cols'=>['id','name','class','code'],'in'=>['id'=>$city_arr],'parent_code'=>$value['code']]);
					if(!empty($value['city_list'])){
						foreach ($value['city_list'] as &$v) {
							$v['area_list']=$Region->getWidgetRows(['cols'=>['id','name','class','code'],'in'=>['id'=>$area_arr],'parent_code'=>$v['code']]);
						}
					}
				}
			}
		}
		return $this->response('deal_succ', $list_rows);
		
	}
	/**
	 * [actionOrdertype 获取订单类型]
	 * @apiDoc
	 * @api     v1/common/ordertype
	 * @group   common
	 * @name    获取订单类型
	 * @desc    获取订单类型公用接口
	 * @method  POST
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
	{
		"code": "000000",
		"msg": "操作成功",
		"info": [
		{
		    "id": 1,
		    "name": "实时单"
		},
		{
		    "id": 2,
		    "name": "预约单"
		}
		]
	}
	 */
	public function actionOrdertype(){
		$ret = Helper::getOrderType(1);
		return $this->response('deal_succ', $ret);
	}
	/**
	 * [actionOpenregion 开通地区]
	 * @apiDoc
	 * @api     v1/common/openregion
	 * @group   common
	 * @name    开通地区
	 * @desc    基础维护-价格维护-新建省/市/区
	 * @method  POST
	 * @param id int ID reg
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
			"code": "000000",
			"msg": "操作成功",
			"info": "开通成功"
		}
	 */
	public function actionOpenregion(){
		if (empty($this->para['id']) || !is_numeric($this->para['id'])) {
            return $this->response('para_miss', '操作ID不能为空');
        }
        $Region = new RegionModel();
        $res = $Region->saveUs(['is_open'=>1],(int) $this->para['id']);
        if($res){
			return $this->response('deal_succ', '开通成功');
		}else{
			return $this->response('deal_false', '已开通');
		}
	}
	/**
	 * [actionGetpro 获取项目/产品]
	 * @apiDoc
	 * @api     v1/common/getpro
	 * @group   common
	 * @name    获取业务
	 * @desc    获取业务/产品-场景为新增价格维护添加
	 * @method  POST
	 * @param parent_id int 父ID noreq
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"total": 1,
		"info": [
		{
		    "id": "1",
		    "name": "美容",
		    "parent_id": "0"
		}
		]
		}
	 */
	public function actionGetpro(){
		$SysProductSet = new SysProductSetModel();
		$param = [
			'cols'=>['id','name','parent_id'],
			'parent_id'=>isset($this->para['parent_id'])?$this->para['parent_id']:0,
			'status'=>'allow'
		];
		$res =  $SysProductSet->getWidgetRows($param);
		$res = !empty($res)?$res:[];
		return $this->response('search_succ', $res,count($res));
	}
	/**
	 * [actionGetcarmodel 获取车型列表]
	 * @apiDoc
	 * @api     v1/common/getcarmodel
	 * @group   common
	 * @name    获取车型配置
	 * @desc    获取车型动态配置
	 * @method  POST
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"total": 1,
		"info": [
		{
		    "id": "1",
		    "car_model": "轿车"
		}
		]
		}
	 */
	public function actionGetcarmodel(){
		$SysCarmodelSet = new SysCarmodelSetModel();
		$param = [
			'cols'=>['id','car_model'],
			'status'=>'allow'
		];
		$res =  $SysCarmodelSet->getWidgetRows($param);
		$res = !empty($res)?$res:[];
		return $this->response('search_succ', $res,count($res));
	}
	/**
	 * [actionGetmerch description]
	 * @apiDoc
	 * @api     v1/common/getmerch
	 * @group   common
	 * @name    获取商户性质
	 * @desc    获取所有有效商户性质
	 * @method  POST
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"total": 2,
		"info": [
		{
		    "id": "1",
		    "name": "二手车整备"
		},
		{
		    "id": "2",
		    "name": "综合社区店"
		}
		]
		}
	 */
	public function actionGetmerch(){
		$MerchType = new MerchTypeModel();
		$param = [
			'cols'=>['id','name'],
			'status'=>'allow'
		];
		$res =  $MerchType->getWidgetRows($param);
		$res = !empty($res)?$res:[];
		return $this->response('search_succ', $res,count($res));
	}
	/**
	 * [actionGetmarry 获取婚姻下拉]
	 * @apiDoc
	 * @api     v1/common/getmarry
	 * @group   common
	 * @name    获取婚姻下拉
	 * @desc    公用配置-获取婚姻下拉
	 * @method  POST
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"total": 2,
		"info": {
		"1": "已婚",
		"2": "未婚"
		}
		}
	 */
	public function actionGetmarry(){
		$res = Helper::getMarryCnf(0,true);
		return $this->response('search_succ', $res,count($res));
	}
	/**
	 * [actionGetcashsta 提现状态menu]
	 * @apiDoc
	 * @api    v1/common/getcashsta
	 * @group  common
	 * @name   提现状态下拉
	 * @desc   公用接口
	 * @method POST
	 * @return json
		{
			"code": "000000",
			"msg": "操作成功",
			"total": 4,
			"info": {
				"0": "处理中",
				"1": "提现成功",
				"2": "提现失败",
				"all": "全部"
			}
		}
	 */
	public function actionGetcashsta(){
		$res = Helper::getCashStatusByKey(0,true);
		return $this->response('search_succ', $res,count($res));
	}
	/**
	 * [getEduca 获取学历下拉]
	 * @apiDoc
	 * @api     v1/common/geteduca
	 * @group   common
	 * @name    获取学历下拉
	 * @desc    公用配置-获取学历下拉
	 * @method  POST
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"total": 3,
		"info": {
		"1": "本科",
		"2": "大专",
		"3": "高中"
		}
		}
	 */
	public function actionGeteduca(){
		$res = Helper::getEducationCnf(0,true);
		return $this->response('search_succ', $res,count($res));
	}
	/**
	 * [actionPermission 获取权限配置列表]
	 * @apiDoc
	 * @api     v1/common/permission
	 * @group   common
	 * @name    获取权限列表
	 * @desc    获取所有权限列表
	 * @method  POST
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
	 {
    "code": "000000",
    "msg": "操作成功",
    "info": [
        {
            "permission_id": "1",
            "function_code": "permissiongroup",
            "function_name": "系统管理",
            "permission_name": "权限管理-查询",
            "path_info": "permissiongroup/search/"
        },
        {
            "permission_id": "2",
            "function_code": "permissiongroup",
            "function_name": "系统管理",
            "permission_name": "权限管理-新增",
            "path_info": "permissiongroup/newrc/"
        }
    ]
}
	 */
	public function actionPermission()
    {
        $conn = Yii::$app->db;
        $_where = " where is_used = 1 order by show_order asc";

        $sql = "select `permission_id`, `function_code`, `function_name`, `permission_name`, `path_info` from {{%permission}} " . $_where;
        $cmd = $conn->createCommand($sql);
        $rec = $cmd->queryAll();
        $conn->close();
        return $this->response('deal_succ', $rec);
    }
    
    /**
     * [actionUploadimg 上传图片]
     * @apiDoc
     * @api     v1/common/uploadimg
     * @group   common
     * @name    上传图片【公用】
     * @desc    公用上传图片接口
     * @method  POST
     * @param   upload_file png 图片路径 req
     * @author JOHN.W
     * @version [1.0]
     * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"info": "4"
		}
     */
    public function actionUploadimg(){
    	$Upload = new UploadService();
		if(!isset($this->para['modelname'])) $this->para['modelname'] = 'tech';
		if(!isset($this->para['filename'])) $this->para['filename'] = 'upload_file';
    	$res  = $Upload->sendUpimg($this->para);
    	if(isset($res['code']) && $res['code']=='000000'){
    		return $this->response('deal_succ', $res['info']);
    	}else{
    		return $this->response('deal_false', $res['msg']);
    	}
    }

    /**
	 * [actionUptech 启用禁用技师/门店账号]
	 * @apiDoc
	 * @api     v1/common/uptech
	 * @group   common
	 * @name    禁用/启用技师-门店账号
	 * @desc    后台管理-技师管理或门店管理-启用/禁用
	 * @method  POST
	 * @param id int 用户ID req
	 * @param status int 操作类型【0:启用;1:禁用 】 req
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
		"code": "000000",
		"msg": "操作成功",
		"info": true
		}
	 */
	public function actionUptech(){
        if (empty($this->para['user_id']) || !is_numeric($this->para['user_id'])) {
            return $this->response('para_miss', '用户ID不能为空');
        }
        if (isset($this->para['status']) || !in_array($this->para['status'], ['0','1'])) {
            return $this->response('para_miss', '请选择正确的更新类型');
        }
        $Users = new UsersModel();
        $res = $Users->saveUs(['status'=>$this->para['status']],(int)$this->para['user_id']);
        if($res){
			return $this->response('deal_succ', $res);
		}else{
			return $this->response('deal_false', $res);
		}
	}
	/**
	 * [actionGetusearea 获取城市启用地区]
	 * @apiDoc
	 * @api    v1/common/getusearea
	 * @group  common
	 * @name   获取开通城市地区
	 * @desc   管理后台获取开通城市地区
	 * @method POST
	 * @param prov_id int 省ID noreq
	 * @param city_id int 区ID noreq
	 * @return json
		 {
			"code": "000000",
			"msg": "操作成功",
			"info": [
			    {
			        "prov_id": "23",
			        "city_id": "275",
			        "area_id": "2639",
			        "prov_name": "四川省",
			        "city_name": "成都市",
			        "area_name": "青羊区"
			    },
			    {
			        "prov_id": "23",
			        "city_id": "275",
			        "area_id": "0",
			        "prov_name": "四川省",
			        "city_name": "成都市",
			        "area_name": "-"
			    }
			]
		}
	 */
	public function actionGetusearea(){
        $SysAreaSet = new SysAreaSetModel();
        $where = ['cols'=>['prov_id','city_id','area_id'],'status'=>'allow'];
        if(isset($this->para['prov_id']) && !empty($this->para['prov_id'])){
        	$where['prov_id'] =$this->para['prov_id'];
        }
        if(isset($this->para['city_id']) && !empty($this->para['city_id'])){
        	$where['city_id'] =$this->para['city_id'];
        }
        $rows = $SysAreaSet->getWidgetRows($where);
        foreach ($rows as &$value) {
			// "id": "5",
			// "name": "内蒙古自治区",
			// "class": "1",
			// "code": "150000"
			$value['prov_name'] = Helper::getRegion($value['prov_id']);
			$value['city_name'] = Helper::getRegion($value['city_id']);
			if(!empty($value['area_id'])){
				$value['area_name'] = Helper::getRegion($value['area_id']);
			}else{
				$value['area_name'] = '-';
			}
        }
        return $this->response('deal_succ', $rows);
	}
}