<?php
namespace app\api\controller\shop;

use app\admin\logic\merchant\MerchantLogic;
use app\admin\logic\supplier\SupplierLogic;
use app\api\controller\BaseApiController;
use app\api\logic\shop\ShopLogic;
use app\common\model\dict\DictData;
use app\common\model\merchant\Merchant;
use app\common\model\supplier\Supplier;
use think\facade\Db;


class IndexController extends BaseApiController{
    public $notNeedLogin = ['apply','get_token','merchat_type','record','apply_detail'];

    /**
     * 获取token
     */
    public function get_token(){
        $token = trim($this->request->header('X-Token'));
        $jg_register_id=$this->request->get('jg_register_id');
        if (strpos($token, 'Bearer') === 0){
            $token = trim(substr($token, 6));
        }
        $res=ShopLogic::auth($token,$jg_register_id);
        if(ShopLogic::hasError()||$res==false){
            return $this->success('没有授权信息');
        }else{
            return $this->success('ok',['token'=>$res]);
        }
    }

    /**
     * 商户类型
     */
    public function merchat_type(){
        $data=DictData::where('type_value','merchat_type')->field('value id,name')->order('sort','desc')->select()?->toArray();
        return $this->success('ok',$data);
    }
    /**
     * 申请
     */
    public function apply(){
        $post=$this->request->post();
        $shop_user_id= $this->get_x_token();
        $post['shop_user_id']=$shop_user_id;
        $find=Db::name('user_auth_shop')->where('shop_uid',$post['shop_user_id'])->find();
		if(!empty($post['city_name']) && !empty($post['area_name'])){
			$city = Db::name('geo_city')->field('province_code,city_code')->where('city_name', 'like', '%' . $post['city_name'] . '%')->find();
			if(empty($city)){
				$post['province_id'] = '';
				$post['city_id'] = '';
				$post['area_id'] = '';
			}else{
				$post['province_id'] = $city['province_code'];
				$post['city_id'] = $city['city_code'];
				$area = Db::name('geo_area')->field('area_code')->where('city_code', $city['city_code'])->where('area_name', 'like', '%' . $post['area_name'] . '%')->find();
				$post['area_id'] = !empty($area) ? $area['area_code'] : '';
			}
		}else{
			$post['province_id'] = '';
			$post['city_id'] = '';
			$post['area_id'] = '';
		}

        if($post['is_merchant_type']==1){
            if($find){
				$post['mer_id'] = $find['pid'];
                $res=MerchantLogic::edit($post);
                Db::name('user_auth_shop')->where('id',$find['id'])->update(['apply_status'=>0,'mark'=>'']);
            }else{
                $res=MerchantLogic::add($post);
            }
            if(MerchantLogic::hasError()){
                return $this->fail(MerchantLogic::getError());
            }
        }else{
            if($find){
                $res=SupplierLogic::edit($post);
                Db::name('user_auth_shop')->where('id',$find['id'])->update(['apply_status'=>0,'mark'=>'']);
            }else{
                $res=SupplierLogic::add($post);
            }
            if(SupplierLogic::hasError()){
                return $this->fail(SupplierLogic::getError());
            }
        }

        return $this->success('ok');
    }
    /**
     * 申请记录
     */
    public function record(){
        $shop_user_id= $this->get_x_token();
        if($shop_user_id){
            $find=Db::name('user_auth_shop')->where('shop_uid',$shop_user_id)->find();
            if($find){
                if($find['type']==1){
                   $res= Merchant::where('mer_id',$find['pid'])->field('mer_name')->find();
                   $res['id']=$find['pid'];
                   $res['apply_status']=$find['apply_status'];
                   $res['create_time']=$find['create_time'];
                   $res['mark']=$find['mark'];
                   return $this->success('ok',$res?->toArray());
                }else{
                    $res= Supplier::where('id',$find['pid'])->field('mer_name')->find();
                    $res['id']=$find['pid'];
                    $res['apply_status']=$find['apply_status'];
                    $res['create_time']=$find['create_time'];
                    $res['mark']=$find['mark'];
                    return $this->success('ok',$res?->toArray());
                }
            }
        }
        return $this->success('没有授权信息');
    }
    /**
     * 申请详情
     */
    public function apply_detail(){
        $id=$this->request->get('id');
        $shop_user_id= $this->get_x_token();
        $post['shop_user_id']=$shop_user_id;
        $where=[
            'shop_uid'=>$shop_user_id,
            'pid'=>$id,
        ];
        $find=Db::name('user_auth_shop')->where($where)->find();

        if($find&&$find['type']==1){
            $res=Merchant::where('mer_id',$id)->find();
            if($res){
                $res['type_name']=DictData::where('type_value','merchat_type')->where('value',$res['type_id'])->value('name');
				$city = Db::name('geo_city')->where('city_code',$res['city_id'])->find();
	            $area = Db::name('geo_area')->where('area_code',$res['area_id'])->find();
				$res['city_name'] = !empty($city) ? $city['city_name'] : '';
				$res['area_name'] = !empty($area) ? $area['area_name'] : '';
            }
            return $this->success('ok',$res?->toArray());
        }
        if($find&&$find['type']==2){
            $res=Supplier::where('id',$id)->find();
            if($res){
                $res['type_name']=DictData::where('type_value','merchat_type')->where('value',$res['type_id'])->value('name');
	            $city = Db::name('geo_city')->where('city_code',$res['city_id'])->find();
	            $area = Db::name('geo_area')->where('area_code',$res['area_id'])->find();
	            $res['city_name'] = !empty($city) ? $city['city_name'] : '';
	            $res['area_name'] = !empty($area) ? $area['area_name'] : '';
            }
            return $this->success('ok',$res?->toArray());
        }
        return $this->success('ok');
    }
    /**
     * 获取商城用户id
     */
    private function get_x_token(){
        $token = trim($this->request->header('X-Token'));
        if (strpos($token, 'Bearer') === 0){
            $token = trim(substr($token, 6));
        }
        list(, $base64UrlPayload,) = explode('.', $token);
        $payload = base64UrlDecode($base64UrlPayload);
        $data = json_decode($payload, true);
        if($data['exp'] < time()){
            return false;
        }
        return $data['jti'][0];
    }

}