<?php

namespace app\admin\model;

use app\admin\model\school\SearchCity;
use app\common\model\dyqc\ManystoreShop;
use think\Model;
use think\Session;

class Admin extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $hidden = [
        'password',
        'salt'
    ];

    public static function init()
    {
        self::beforeWrite(function ($row) {
            $changed = $row->getChangedData();
            //如果修改了用户或或密码则需要重新登录
            if (isset($changed['username']) || isset($changed['password']) || isset($changed['salt'])) {
                $row->token = '';
            }
        });
    }

    public function getAddressCityList(){
        return SearchCity::column("address_city","id");
    }


    public static function getHaveShopId($adminId){
        $admin = self::get($adminId);
        if(!$admin)throw new \Exception("管理员不存在");
        $area_json = $admin->area_json;
        $shop_id = "*";
        if($area_json){
           $shop_id = [];
           $searchCity = SearchCity::where("id","in",$area_json)->select();
           foreach ($searchCity as $item){
               $province = $item->province;
               $city = $item->city;
               $district = $item->district;
               //查询在该区域的店铺id
              $manystoreShop =  new ManystoreShop;
              if($province)$manystoreShop = $manystoreShop->where("province",$province);
              if($city)$manystoreShop = $manystoreShop->where("city",$city);
              if($district)$manystoreShop = $manystoreShop->where("district",$district);
               $shop_id = array_merge($shop_id,$manystoreShop->column("id"));
           }
        }
        return $shop_id;
    }

    public static function getHaveCity($adminId){
        $provinces = $citys = $districts = $address_citys = [];
        $admin = self::get($adminId);
        if(!$admin)return compact("provinces","citys","districts","address_citys");
        $area_json = $admin->area_json;

        if($area_json){
            $searchCity = SearchCity::where("id","in",$area_json)->select();
            foreach ($searchCity as $item){
                $address_citys[] = $item->address_city;
                $province = $item->province;
                if($province)$provinces[] = $province;
                $city = $item->city;
                if($city)$citys[] = $city;
                $district = $item->district;
                if($district)$districts[] = $district;
            }
        }
        if(!$provinces)$provinces="*";
        if(!$citys)$citys="*";
        if(!$districts)$districts="*";
        if(!$address_citys)$address_citys="*";
        return compact("provinces","citys","districts","address_citys");
    }



    public static function checkAuthMsg($adminId){
        ["provinces" =>$provinces,"citys"=> $citys, "districts"=>$districts,"address_citys"=>$address_citys] = self::getHaveCity($adminId);
        if(is_array($address_citys))$address_citys = implode(",",$address_citys);
            //必要信息已完善
            return '<div class="alert alert-success-light">
           <b >您当前的区域管理权限为:<span style="color: red"> '.$address_citys.' </span>(能管理该区域下的机构信息，如果是*则不限制)</b></div>';

    }


}
