<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$sid = $shop['id'];
if($op=='ajax'){
    $condition = ' WHERE uniacid = :aid AND sid = :sid';
    $params[':aid'] = $_W['uniacid'];
    $params[':sid'] = $sid;
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_order') . $condition, $params);
    $list = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_order') . $condition . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
    $stats =['待处理','已接单','已派单','已完成','已评价','已取消'];
    foreach ($list as &$v) {
        $member = pdo_fetch("SELECT * FROM" . tablename("evzg_express_member") . "WHERE openid='{$v['openid']}'");
        $expres = pdo_fetch("SELECT * FROM" . tablename("evzg_express_expres") . "WHERE id='{$v['eid']}'");
        $v['status'] = $stats[$v['status']];
        $v['nickname'] = $v['name'];
        $v['expresssn'] = $expres['expresssn'];
        $v['createtime'] =date("Y-m-d H:i:s",$v['createtime']);
        unset($v);
    }
    exit(json_encode(array('code'=>200,'content'=>$list)));
}
//接单 退单  完成状态修改
if($op=='status'){
    $log = array();
    $oid = $_GPC['oid'];
    $stats = intval($_GPC['status']);
    $order = pdo_fetch("SELECT * FROM " . tablename('evzg_express_order') . " WHERE id = '$oid'");
    $express = pdo_fetch("SELECT * FROM" . tablename("evzg_express_expres") . "WHERE id='{$order['eid']}'");
    $result = pdo_update("evzg_express_order",array('status'=>$stats),array('id'=>$oid));
    if($result){
        if($stats==1){
            $tpl = array(
                'openid'=>$order['openid'],
                "template_id" => $settings['msg']['order_yysu_id'],
                "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('express'),
                "data" => array(
                    'first' => "您好，你的快递{$express['expresssn']}预约上门成功" . "\\n",
                    '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'] = "快递员：".$delivey['nickname'];
            $log['content'] = "您好，你的快递{$express['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('express'),
                "data" => array(
                    'first' => "您好，你的快递{$express['expresssn']}预约上门失败" . "\\n",
                    'keyword3'=>$order['address'],
                    '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'] = "快递员：".$delivey['name'];
            $log['content'] = "您好，你的快递{$express['expresssn']}预约上门失败";
            $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_order")."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($tpl.'order');