<?php
/**
* 快件
* @date: 2018年1月19日 下午8:59:46
* @author: MIn晓天 QQ416883181
* @param:
* @return:
*/
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];
$sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE id='{$delivey['sid']}' AND uniacid='{$_W['uniacid']}'";
$shop = pdo_fetch($sql);
$log = array();
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$sid = $shop['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'){
    $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_expres') . $condition, $params);
    $list = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expres') . $condition . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
    $stats =['待处理','已取件','已取消'];
    //exit(json_encode(array('code'=>200,'content'=>pdo_debug())));
    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['thumb'] = toimage($v['thumb']);
        $v['nickname'] = $member['nickname'];
        $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($tpl.'expressorder-add');
}
//接单 退单  完成状态修改
if($op=='status'){
    $oid = $_GPC['oid'];
    $stats = intval($_GPC['status']);
    $express = pdo_fetch("SELECT * FROM " . tablename('evzg_express_expres') . " WHERE id = '$oid'");
    $result = pdo_update("evzg_express_expres",array('status'=>$stats),array('id'=>$oid));
    $user = pdo_fetch('SELECT * FROM ' . tablename('evzg_express_member') . "WHERE mobile='{$express['mobile']}'");
    if($result){
        $log['oid'] = $oid;
        $log['sid'] = $shop['id'];
        $log['operator'] = "店铺：".$shop['name'];
        if($stats==1){
               $expresstime = date("Y-m-d H:i:s",time());
               $tpl = array(
                "openid" => $user['openid'],
                "template_id" => $settings['msg']['order_qj_id'],
                "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('express'),
                "data" => array(
                    'first' => "您好，你的快递{$express['expresssn']}在{$expresstime}已被取走",
                    'keyword1' => "{$express['expresssn']}",
                    'keyword2' => $shop['province'].$shop['city'].$shop['district'].$shop['address'],
                    'keyword3' => date("Y-m-d H:i:s",time()),
                    'remark' => array(
                        'value' => urlencode("感谢亲的使用,如果非本人取走请联系客服"),
                        'color' => "#01579b"
                    )
                )
            );
           sendMsgTpl($tpl,$log);
           exit(json_encode(array('code'=>200,'msg'=>"推送成功")));
        }
        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_expres")."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.'expresslist');