<?php
namespace admin\action;
use model\supplier;

class supplierAction extends \action
{
	public function detail()
    {
        $req = \mvc::$req;
        $sup_id = $req['sup_id'];
        if($sup_id){
			$supplier = new supplier();
            $item = $supplier->getList(['sup_id'=>$sup_id]);
        }
        if(empty($item['rows'][0]['sup_country'])){
            $item['rows'][0]['sup_country'] = 'CN';
            $item['rows'][0]['captions']['sup_country'] = '中国';
        }
        \view::set('dataArr',$item['rows'][0]);

        \view::put();
    }

    public function getBaseInfo()
    {
        $req = \mvc::$req;
        $sup_id = $req['sup_id'];
        $data = [];
        if($sup_id){
            $supplier = new supplier();
            $data = $supplier->getRowByKey($sup_id);
        }
        $this->successJson($data);
    }

    public function list()
    {
        $req = \mvc::$req;
        \view::put();
    }

    public function getList()
    {
        $req = \mvc::$req;
        $supplier = new supplier();
        $data = $supplier->getList($req);
        $this->successJson($data);
    }

    public function saveDetail()
    {
        $req = \mvc::$req;
        $supplier = new supplier();
        $data = $supplier->saveDetail($req);
        $this->successJson($data);
    }


    public function delData()
    {
        $req = \mvc::$req;
        $supplier = new supplier();
        $data = $supplier->delData(explode(',', $req['ids']));
        $this->successJson($data);
    }

    public function getCountryAllKeyValues()
    {
        $req = \mvc::$req;
		$supplier = new supplier();
        $where['OR'] = [
            'cn_name[~]'=>$req['query'],
            'en_name[~]'=>$req['query'],
        ];
        $rows = $supplier->getDb()->select('crm_country','*',$where);
        $data = [];
        $regi=0;
		$firstkey = isset($req['firstkey'])? $req['firstkey']: 0;
        if(isset($req['hasFirst']) && $req['hasFirst']){
            $data[0] = ['key'=>$firstkey, 'value'=>'' ];
            $regi = 1;
        }
        foreach($rows as $k=>$row){

            $data[$k+$regi] = ['key'=>$row['cc_code'], 'value'=> $row[LG_NAME]??$row['cn_name'] ];
        }
        $this->successJson($data);
    }

    public function getPctList()
    {
        $req = \mvc::$req;
        $supplier = new supplier();
        if(isset($req['pid'])){
            $where['pc_pid'] = $req['pid'];
        }
        if(isset($req['level'])){
            if($req['level']==2){
                $provinceId = $supplier->getDb()->select('province_city', 'pc_id', ['pc_pid' => 0]);
                $where['pc_pid'] = $provinceId;
            }
            if($req['level']==3){
                $sql = "select distinct b.pc_id from province_city as b 
                join province_city as a on b.pc_pid = a.pc_id where a.pc_pid =0";
                $where['RAW #1'] = raw( sprintf('pc_pid in (%s)',$sql));
            }
        }
        if($req['query']) $where['pc_name[~]'] = $req['query'];
        $where['ORDER'] = ['pc_id'=>'ASC'];   
        $rows = $supplier->getDb()->select('province_city','*',$where);
       
        $data = [];
        $regi=0;
		$firstkey = isset($req['firstkey'])? $req['firstkey']: 0;
        if(isset($req['hasFirst']) && $req['hasFirst']){
            $data[0] = ['key'=>$firstkey, 'value'=>'' ];
            $regi = 1;
        }
        foreach($rows as $k=>$row){

            $data[$k+$regi] = ['key'=>$row['pc_id'], 'value'=>$row['pc_name'] ];
        }
        $this->successJson($data); 
    }

    //获取公司信息
    public function getCompanyInfo()
    {   
        $req = \mvc::$req;
        $supplier = new supplier();
        $data  = $supplier->getCompanyInfoByApi($req['name']);
        // $json = '{"com_hy":"软件和信息技术服务业","com_clsj":"2007-03-26","com_frdb":"蒋芳","com_zczj":"15459.89659","com_gsdj":"330100400013364","com_uninum":"91330100799655058B","com_addr":"浙江省杭州市滨江区长河街道网商路699号4号楼5楼508室","com_nsrsbh":"91330100799655058B","com_nsrzz":"增值税一般纳税人","com_worker":null,"com_status":"存续","com_enname":"Alibaba (China) Co., Ltd","com_oldname":"存续"}';
        // $data = json_decode($json,true);
        $this->successJson($data); 
    }

    public function setContact()
    {
        $req = \mvc::$req;
        $contactList =[];
        if($req['sup_id'])
        {
            $supplier  = new supplier();
		    $detail = $supplier->getList(['sup_id'=>$req['sup_id']]);
            $contactList = $detail['rows'][0]['contactList']??[];
        }

		\view::set('contactList',$contactList);
		\view::set('req',$req);
		\view::put();
    }

    public function getSupplierKeyValues()
    {
        $req = \mvc::$req;

        $req = \mvc::$req;
		$supplier = new supplier();
        $where = [
            'sup_code[~]'=>$req['query'],
        ];

        //权限 - 小类

        $join = [];    
       
        $rows = $supplier->getDb()->select('supplier(sup)',$join,['sup_id','sup_code'],$where);
        $data = [];
        $regi=0;
		$firstkey = isset($req['firstkey'])? $req['firstkey']: 0;
        if(isset($req['hasFirst']) && $req['hasFirst']){
            $data[0] = ['key'=>$firstkey, 'value'=>'' ];
            $regi = 1;
        }
        foreach($rows as $k=>$row){

            $data[$k+$regi] = ['key'=>$row['sup_id'], 'value'=> $row['sup_code']??'' ];
        }
        $this->successJson($data);
    }
}