<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];
mload()->model('sms');
$sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE openid='{$_W['openid']}' AND 	uniacid='{$_W['uniacid']}'";
$shop = pdo_fetch($sql);
$sid = $shop['id'];
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$log = array();
if (empty($shop)) {
    message("不是门店管理者", $this->createMobileUrl('shop', array(
        'op' => 'addmobile'
    )), "error");
}
if($op=="add"){
    load()->classs('weixin.account');
    $account_api = WeAccount::create();
    $account_api->clearAccessToken();
    $access_token = $account_api->getAccessToken();
    $mobile = $_GPC['mobile'];
    if(empty($mobile)){
        message("手机号不能为空",$this->createMobileUrl('appointment'),"error");
    }
    $expresssn = $_GPC['expresssn'];
    if(empty($expresssn)){
        message("快递号不能为空", $this->createMobileUrl('appointment'),"error");
    }
    $isexpresssn = pdo_fetch("SELECT * FROM".tablename("evzg_express_expres")."WHERE expresssn='{$expresssn}'");
    if(!empty($isexpresssn)){
        message("快递号已存在", $this->createMobileUrl('appointment'),"error");
    }
    $serverId = $_GPC['serverId'];
    if(!empty($serverId)){
       $thumb = getmedia($access_token,$serverId[0],"serverId");
    }
    $x = $_GPC['x'];
    $orderno = $x.substr($expresssn,-2).build_order_no();
    $isorderno = pdo_fetch("SELECT * FROM".tablename("evzg_express_expres")."WHERE orderno='{$orderno}'");
    if(!empty($isorderno)){
         $orderno = $orderno.rand(0,9);
    }
    $data = array(
        'uniacid' => $_W['uniacid'],
        'orderno' => $orderno,
        'expresssn' => $_GPC['expresssn'],
        'enabled' => 1,
        'weight' =>0,
        'sid' => intval($sid),
        'description' => $_GPC['description'],
        'mobile' => $_GPC['mobile'],
        'thumb' => $thumb,
        'createtime' => time()
    );
    $evgShop = getEvgShop($expresssn);
    if ($evgShop) {
        $data['shipperCode'] = $evgShop['shippercode'];
        $data['expressshop'] = $evgShop['shippername'];
    }
    $result = pdo_insert('evzg_express_expres', $data);
    $exid = pdo_insertid();
    $user = pdo_fetch("SELECT * FROM" . tablename("evzg_express_member") . "WHERE mobile='{$mobile}'AND uniacid='{$_W['uniacid']}' ");
    $log['oid'] = $exid;
    $log['sid'] = $shop['id'];
    $log['operator'] = "店铺：".$shop['name'];
    if(!empty($user['openid'])){
		if($shop['msgtype']==1||$shop['msgtype']==3){
         $tpl = array(
            'openid'=>$user['openid'],
            "template_id" => $settings['msg']['order_dz_id'],
            "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('express'),
            "data" => array(
                'first' => "您好，你的{$data['expressshop']}{$data['expresssn']}已经到达{$shop['name']}门店",
                'keyword1' => $shop['name'],
                'keyword2' => $data['expresssn'],
                'keyword3' => $data['orderno'],
                'keyword4' => $shop['province'].$shop['city'].$shop['district'].$shop['address'],
                'remark' => "{$_GPC['description']}"
            )
        );
        sendMsgTpl($tpl,$log);
	  }
    }
	if($shop['msgtype']==1||$shop['msgtype']==2){
		if ($settings['enabled']==1){
		   sms_send_dayu($exid);

		}else{
			$key =$settings['juhe']['juhe_appkey'];
			$tpl_value ='#shop#='.$shop['name'].'&#ordersn#='.$data['orderno'];
			$tpl_id = $settings['juhe']['juhe_temp2'];
			juHeSms($key, $_GPC['mobile'], $tpl_id, $tpl_value,$log);

		}
	}
    message('更新快件信息成功！', $this->createMobileUrl('appointment'), 'success');
}
$default='default2/shop/';
include $this->template($default.'appointment');