<?php
namespace wstmart\admin\controller;
use wstmart\admin\model\Shops as M;
use wstmart\common\model\Table as TM;
/**
 * ============================================================================
 * 店铺控制器
 */
class Shops extends Base{
    public function index(){
        $this->assign("areaList",model('areas')->listQuery(0));
        $this->assign("catList",model('GoodsCats')->listQuery(0));
        return $this->fetch("list");
        
    }

    public function stopIndex(){
        $this->assign("areaList",model('areas')->listQuery(0));
    	return $this->fetch("list_stop");
    }
    /**
     * 获取分页
     */
    public function pageQuery(){
    	$m = new M();
    	return WSTGrid($m->pageQuery());
    }
    /**
     * 停用店铺列表
     */
    public function pageStopQuery(){
    	$m = new M();
    	return WSTGrid($m->pageQuery(-1));
    }
    // 商家登录
    public function detail(){
        $this->assign("startDate",date('Y-m-d',strtotime("-1month")));
        $this->assign("endDate",date('Y-m-d'));
        return $this->fetch('detail');
    }
    public  function detailByPage(){
        $m=new M();
        $result=$m->detailByPage();
        return WSTGrid($result);
    }
    /**
     * 获取菜单
     */
    public function get(){
    	$m = new M();
    	return $m->get((int)Input("post.id"));
    }
    /**
     * 跳去编辑页面
     */
    public function toEdit(){
    	$m = new M();
    	$id = (int)Input("get.id");
    	if($id>0){
    	    $object = $m->getById((int)Input("get.id"));
            $object['auxiliary']=db('shop_auxiliary')->where('shopId',$object['shopId'])->select();
            $data['object']=$object;
    	}else{
    		$object = $m->getEModel('shops');
    		$object['catshops'] = [];
    		$object['accreds'] = [];
    		$object['loginName'] = '';
    		$data['object']=$object;
    	}
    	$data['goodsCatList'] = model('goodsCats')->listQuery(0);
    	$data['accredList'] = model('accreds')->listQuery(0);
    	$data['bankList'] = model('banks')->listQuery();
    	$data['areaList'] = model('areas')->listQuery(0);
        if($id>0){
        	return $this->fetch("edit",$data);
        }else{
            return $this->fetch("add",$data);
        }
    }
    
    /**
     * 新增菜单
     */
    public function add(){
    	$m = new M();
    	return $m->add();
    }
    /**
     * 编辑菜单
     */
    public function edit(){
    	$m = new M();
    	return $m->edit();
    }
    /**
     * 删除菜单
     */
    public function del(){
    	$m = new M();
    	return $m->del();
    }
    
    /**
     * 检测店铺编号是否存在
     */
    public function checkShopSn(){
    	$m = new M();
    	$isChk = $m->checkShopSn(input('post.shopSn'),input('shopId/d'));
        if(!$isChk){
    		return ['ok'=>'该店铺编号可用'];
    	}else{
    		return ['error'=>'对不起,该店铺编号已存在'];
    	}
    }
    
    /**
     * 自营店铺后台
     */
    public function inself(){
    	$staffId=session("WST_STAFF");
    	if(!empty($staffId)){
    		$id=1;
    		$s = new M();
    		$r = $s->selfLogin($id);
    		if($r['status']==1){
    			header("Location: ".Url('home/shops/index'));
    			exit();
    		}
    	}
    	header("Location: ".Url('home/shops/selfShop'));
    	exit();
    }

    /**
     * 跳去店铺申请列表
     */
    public function apply(){
        $this->assign("areaList",model('Position')->listQuery());
        //$this->assign("areaList",model('areas')->listQuery(0));
        return $this->fetch("list_apply");
    }
    /**
     * 获取分页
     */
    public function pageQueryByApply(){
        $m = new M();
        return WSTGrid($m->pageQueryByApply());
    }
    /**
     * 获取店铺申请列表
     */
    public function shopApplyList(){
        $m = new M();
        return WSTGrid($m->shopApplyList());
    }
    
     /**
     * 去处理开店申请
     */
    public function applyEdit(){
        $data = [];
        $id = input('get.id/d');
       // dump($id);
        $m = new TM();
        $m->setTable('shops');
        $data['object'] = $m->getInfo(['shopId'=>$id],'*');
        $data['object']['applyTime'] = date('Y-m-d H:i:s',$data['object']['createTime']);
        //改成用户表
        $m->setTable('users');
        $data['object']['user'] = $m->getInfo(['userId'=>$data['object']['userId']],'loginName,trueName');
        $areas=['province','city','county','town','village'];
        $data['object']['area'] = '';
        foreach ($areas as $v) {
            $m->setTable('position_'.$v);
            $data['object']['area'] .=$m->getField([$v.'_id'=>$data['object'][$v.'Id']],$v.'_name').'-';
        }


        // //改成省表
        // $m->setTable('position_province');
        // $data['object']['area'] = $m->getField(['province_id'=>$data['object']['provinceId']],'province_name');
        // //改成市表
        // $m->setTable('position_city');
        // $data['object']['area'] .='-'.$m->getField(['city_id'=>$data['object']['cityId']],'city_name');
        // //改成区表
        // $m->setTable('position_county');
        // $data['object']['area'] .='-'.$m->getField(['county_id'=>$data['object']['countyId']],'county_name');
        // //改成乡镇表
        // $m->setTable('position_town');
        // $data['object']['area'] .='-'.$m->getField(['town_id'=>$data['object']['townId']],'town_name');
        // //改成村表
        // $m->setTable('position_village');
        // $data['object']['area'] .='-'.$m->getField(['village_id'=>$data['object']['villageId']],'village_name');
       // dump($data);die;
        return $this->fetch("apply_edit",$data);
    }
    /**
     * 去处理开店申请
     */
    public function toHandleApply(){
        $data = [];
        $data['shoplicense']=db('shop_license')
        ->where('shopId',(int)input("get.id"))
        ->find();
        $data['cashDeposit'] = db('shops_deposit')->where('shopId',(int)input("get.id"))->find();
        $data['object'] = model('shops')->getShopApply((int)input("get.id"));
        $data['goodsCatList'] = model('goodsCats')->listQuery(0);
        $data['accredList'] = model('accreds')->listQuery(0);
        $data['bankList'] = model('banks')->listQuery();
        $data['areaList'] = model('areas')->listQuery(0); 
        return $this->fetch("edit_apply",$data);
    }

    public function delApply(){
        $m = new M();
        return $m->delApply();
    }

    /**
     * 开店申请处理
     */
    public function handleApply(){
        $m = new M();
        return $m->handleApply();
    }
    /**
     * 导出订单
     */
    public function toExport(){
        $m = new M();
        $rs = $m->toExport();
        $this->assign('rs',$rs);
    }
    /**
     * 导出商家登录订单
     */
    public function toExports(){
        $m = new M();
        $rs = $m->toExports();
        $this->assign('rs',$rs);
    }

    /**
     * 管理质保金页面
     */
    public function toDeposit(){
        $m = new M();
        return $this->fetch('deposit');
    }
    /**
     * 质保金充值或扣除
     */
    public function topDeposit(){
        $m = new M();
        $result = $m->topDeposit();
        exit(json_encode($result));
    }

    /**
     * 获取店铺名称
     */
    public function upShopName(){
        $m = new M();
        $res = $m->upShopName();
        // dump($res);die;
        exit(json_encode($res));
    }

    /**
     * 修改佣金扣点
     */
    public function commission(){
        $m = new M();
        $obj = $m->commission((int)Input("post.shopId"));
        exit(json_encode($obj));
    }

    /**
     * 设置店铺佣金扣点,业务员提成
     */
    public function upCommission(){
        $m = new M();
        $result = $m->upCommission();
        exit(json_encode($result));
    }
    /**
     * 查询业务员信息
     */
    public function staffs(){
        $m = new M();
        $data = $m->staffs();
        exit(json_encode($data));
    }
}