<?php
/**
* 寄件
* @date: 2018年1月19日 下午8:59:46
* @author: MIn晓天 QQ416883181
* @param:
* @return:
*/
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$log = array();
$settings = $this->module['config'];
$sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE openid='{$_W['openid']}' AND 	uniacid='{$_W['uniacid']}'";
$member = pdo_fetch($sql);
if (empty($member)) {
    message("不是门店管理者", $this->createMobileUrl('shop', array(
        'op' => 'addmobile'
    )), "error");
}
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$sid = $member['id'];
$sql = "SELECT * FROM" . tablename("evzg_express_expressuser") . "WHERE enabled='1' AND openid='{$_W['openid']}'";
$expuser = pdo_fetch($sql);
$rootset = pdo_fetch("SELECT * FROM".tablename("evzg_express_rootuser")."WHERE uniacid='{$_W['uniacid']}'");
if($op=='ajax'){
    $params = array();
    $condition = ' WHERE uniacid = :aid AND sid = :sid';
    $params[':aid'] = $_W['uniacid'];
    $params[':sid'] = $sid;
    $status = $_GPC['status'];
    if($status!==''&&isset($status)){
        $condition.=" AND status=:status";
        $params[':status'] = $status;
    }
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_expressorder') . $condition, $params);
    $list = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expressorder') . $condition . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
    $stats =['待处理','已接单','已支付','已完成','已评价','已取消'];
    foreach ($list as &$v) {
       if(empty($v['expresssn'])){
            $v['expresssn']="暂无";
        }
        if(empty($v['expressshop'])){
            $v['expressshop']="其他快递";
        }
        $member = pdo_fetch("SELECT * FROM" . tablename("evzg_express_member") . "WHERE openid='{$v['openid']}'");
        $v['status'] = $stats[$v['status']];
        $v['nickname'] = $v['name'];
        $v['createtime'] =date("Y-m-d H:i:s",$v['createtime']);
        unset($v);
    }
    exit(json_encode(array('code'=>200,'content'=>$list)));
}
if($op=='add'){
    include $this->template('expressorder-add');
}
//接单 退单  完成状态修改
if($op=='status'){
    $oid = $_GPC['oid'];
    $stats = intval($_GPC['status']);
    $order = pdo_fetch("SELECT * FROM " . tablename('evzg_express_expressorder') . " WHERE id = '$oid'");
    $result = pdo_update("evzg_express_expressorder",array('status'=>$stats),array('id'=>$oid));
    if($result){
        if($stats==1){
            pdo_update("evzg_express_expressorder",array('price'=>floatval($_GPC['price']),'eid'=>$expuser['id']),array('id'=>$oid));
            $tpl = array(
                'openid'=>$order['openid'],
                "template_id" => $settings['msg']['order_jd_bg'],
                "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('userorder'),
                "data" => array(
                    'first' => "您好，你的快递预约已接收,请支付一下金额",
                    'keyword1' => date('Y年m月d日 H:i', time()),
                    'keyword2' => '待支付',
                    'remark' =>'支付之后,快递员将会上面取件'
                )
            );
            $tplLog = sendMsgTpl($tpl);
            if($tplLog){
                $log['status'] = 1;
            }
            $log['oid'] = $order['id'];
            $log['sid'] = $order['sid'];
            $log['openid'] = $order['openid'];
            $log['operator'] = "店铺：".$member['name'];
            $log['content'] = "您好，你的快递预约已接收,请支付一下金额";
            $log['addtime']=time();
            $log['msg'] = $tplLog;
            $log['type'] = 2;
        }elseif($stats==3){
            $expresssn = $_GPC['expresssn'];
            $data = array('status'=>$stats,'expresssn'=>$expresssn);
            $evgShop = getEvgShop($expresssn);
            if($evgShop){
                $data['expresscode'] = $evgShop['shippercode'];
                $data['expressshop'] = $evgShop['shippername'];
            }
            //exit(json_encode(array('code'=>200,'msg'=>$data)));
            pdo_update("evzg_express_expressorder",$data,array('id'=>$oid));
            $tpl = array(
                'openid'=>$order['openid'],
                "template_id" => $settings['msg']['order_jd_ds'],
                "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('expressline',array('op'=>'ajax','expressn'=>$expresssn)),
                "data" => array(
                    'first' => "您好，你的快递{$data['expressshop']}:{$_GPC['expresssn']}已准备发送",
                    'keyword2' => date('Y年m月d日 H:i', time()),
                    'keyword1' => $_GPC['expresssn'],
                    'remark' => "感谢您的使用"
                )
            );
            $tplLog = sendMsgTpl($tpl);
            if($tplLog){
                $log['status'] = 1;
            }
            $log['oid'] = $order['id'];
            $log['sid'] = $order['sid'];
            $log['openid'] = $order['openid'];
            $log['operator'] = "店铺：".$member['name'];
            $log['content'] = "您好，你的快递{$data['expressshop']}:{$_GPC['expresssn']}已准备发送";
            $log['addtime']=time();
            $log['msg'] = $tplLog;
            $log['type'] = 2;
        }elseif($stats==5){
            $tpl = array(
                'openid'=>$order['openid'],
                "template_id" => $settings['msg']['order_yyerr_id'],
                "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('userorder'),
                "data" => array(
                    'first' => "您好，你的快递预约寄件失败",
                    'keyword2' => date('Y年m月d日 H:i', time()),
                    'keyword1' => '上门取件',
                    'remark' => $_GPC['description']
                        )
            );
            $tplLog = sendMsgTpl($tpl);
            if($tplLog){
                $log['status'] = 1;
            }
            $log['oid'] = $order['id'];
            $log['sid'] = $order['sid'];
            $log['openid'] = $order['openid'];
            $log['operator'] = "店铺：".$member['name'];
            $log['content'] = "您好，你的快递预约寄件失败";
            $log['addtime']=time();
            $log['msg'] = $tplLog;
            $log['type'] = 2;
        }
        msgLog($log);
        exit(json_encode(array('code'=>200,'msg'=>'接单成功')));
    }else{
        exit(json_encode(array('code'=>400,'msg'=>'接单失败')));
    }

}

//发送通知
if($op=='sendMsg'){
    $templateid = $settings['msg']['order_dz_id'];
    $oid = $_GPC['oid'];
    $order = pdo_fetch("SELECT * FROM".tablename("evzg_express_expressorder")."WHERE oid='{$oid}' AND sid = '{$sid}'");
    if(empty($order)){
        exit(json_encode(array('code'=>401,'msg'=>"数据不存在")));
    }
    $expressuser = pdo_fetchall("SELECT * FROM".tablename("evzg_express_expressuser")."WHERE sid = '{$sid}'");
    $type  = $order['type'] ==2 ? '邮件':'送件';
    $data =array(
        'id' =>$order['id'],
        'time'=>date('Y-m-d H:i:s',$order['createtime']),
        'timeslot' =>$order['timeslot'],
        'ordertitle' =>$order['ordertitle'],
        'mobile'=>$order['mobile'],
        'note'=>$order['note'],
        'address'=>$order['address'],
        'type' =>$type,
    );
    foreach ($expressuser as $v){
        sendOrderMsg($v['openid'], $data, $templateid);
    }
    exit(json_encode(array('code'=>200,'msg'=>"发送成功")));
}


include $this->template('expressorder');