<?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'];
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$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_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['mobile'] = $member['mobile'];
        $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']);
    $order = pdo_fetch("SELECT * FROM " . tablename('evzg_express_expressorder') . " WHERE id = '$oid'");
    $result = pdo_update("evzg_express_expressorder", array(
        'status' => $stats
    ), array(
        'id' => $oid
    ));
    $log = array();
    $log['oid'] = $oid;
    $log['sid'] = $delivey['sid'];
    $log['openid'] = $order['openid'];
    $log['operator'] = $delivey['nickname'];
    if ($result) {
        if ($stats == 1) {
            pdo_update("evzg_express_expressorder", array(
                'price' => floatval($_GPC['price']),
                'eid' => $delivey['id']
            ), array(
                'id' => $oid
            ));
            $url = $_W['siteroot'] . 'app/' . $this->createMobileUrl('userorder');
            $sendData = array(
                'openid' => $order['openid'],
                'template_id' => $settings['msg']['order_jd_bg'],
                'url' => $url,
                "data" => array(
                    'first' => "您好，你的快递预约已接收,请支付一下金额",
                    'keyword2' => '待支付',
                    'keyword1' => date('Y年m月d日 H:i:s', time()),
                    'remark' => "支付之后,快递员:{$member['nickname']} 电话：{$member['mobile']}将会上面取件"
                )
            );
            $log['content'] = "{$member['nickname']}设置订单：{$oid} 价格为：{$_GPC['price']}";
        } 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'];
            }
            pdo_update("evzg_express_expressorder", $data, array(
                'id' => $oid
            ));
            $url = $_W['siteroot'] . 'app/' . $this->createMobileUrl('expressline', array(
                'op' => 'ajax',
                'expressn' => $expresssn
            ));
            $sendData = array(
                'openid' => $order['openid'],
                'template_id' => $settings['msg']['order_jd_ds'],
                'url' => $url,
                "data" => array(
                    'first' => "您好，你的{$data['expressshop']}{$_GPC['expresssn']}已准备发送" ,
                    'keyword1' =>$_GPC['expresssn'],
                    'keyword2' => date('Y年m月d日 H:i:s', time()),
                    'remark' => "\\n感谢您的使用"
                )
            );
            $log['content'] = "{$member['nickname']}设置订单：{$oid} 单号为：{$expresssn}";
        } elseif ($stats == 5) {
            $url = $_W['siteroot'] . 'app/' . $this->createMobileUrl('userorder');
            $sendData = array(
                'openid' => $order['openid'],
                'template_id' => $settings['msg']['order_yyerr_id'],
                'url' => $url,
                "data" => array(
                    'first' => "您好，你的快递预约寄件失败",
                    'keyword1' =>"上门取件",
                    'keyword2' => date('Y年m月d日 H:i:s', time()),
                    'keyword3'=>$order['address'],
                    'remark' => $_GPC['description']
                       )
            );
            $log['content'] = "{$member['nickname']}拒绝接订单：{$oid}";
        }
        $log['addtime']=time();
        // 开启发送判断
        $result = sendMsgTpl($sendData);
        $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($tpl . 'expressorder');