<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];
$sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE openid='{$_W['openid']}' AND 	uniacid='{$_W['uniacid']}'";
$shop = pdo_fetch($sql);
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';

if (empty($shop)) {
    message("不是门店管理者", $this->createMobileUrl('shop', array(
        'op' => 'addmobile'
    )), "error");
}
if ($op == 'qj') {
    $sid = $shop['id'];
    $condition = ' WHERE uniacid = :aid AND sid = :sid AND status =:status';
    $params[':aid'] = $_W['uniacid'];
    $params[':sid'] = $sid;
    $params[':status'] = 0;
    $ordersn = trim($_GPC['ordersn']);
    if (! empty($ordersn)) {
        $condition .= " AND orderno = :orderno";
        $params[':orderno'] = $ordersn;
        $list = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expres') . $condition . ' ORDER BY displayorder DESC ', $params);
    }
    $code = $_GPC['code'];
    if (! empty($code)) {
        // var_dump($code);die();
        $codes = explode(',', $code);
        // var_dump($codes);die();
        if (! strstr($code, ',')) {
            $user = pdo_fetch('SELECT * FROM ' . tablename('evzg_express_member') . "WHERE openid='{$code}'");
            $condition .= " AND mobile = :mobile";
            $params[':mobile'] = $user['mobile'];
            $list = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expres') . $condition . ' ORDER BY displayorder DESC ', $params);
            //var_dump($list);die();
        } else {
            $len = strlen($codes[1]);
            $lentime = strlen(time());
            $lensubstr = $len - $lentime;
            $mid = substr($codes[1], 0, $lensubstr);
            $user = pdo_fetch('SELECT * FROM ' . tablename('evzg_express_member') . "WHERE id='{$mid}'");
            $condition .= " AND mobile = :mobile";
            $params[':mobile'] = $user['mobile'];
            $list = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expres') . $condition . ' ORDER BY displayorder DESC ', $params);
            // var_dump($mid);die();
        }
    }
}
if ($op == 'success') {
    $id = $_GPC['id'];
    $result = pdo_update("evzg_express_expres", array(
        'status' => 1
    ), array(
        'id' => $id
    ));
    $express = pdo_fetch('SELECT * FROM ' . tablename('evzg_express_expres') . "WHERE id='{$id}'");
    $user = pdo_fetch('SELECT * FROM ' . tablename('evzg_express_member') . "WHERE uniacid ='{$_W['uniacid']}' AND mobile='{$express['mobile']}'");
     /* var_dump($user);die(); */
    if ($result) {
        $log = array();
        $log['oid'] = $express['id'];
        $log['sid'] = $shop['id'];
        $log['operator'] = "店铺：".$shop['name'];
        $outtieme = 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['expressshop']}{$express['expresssn']}在{$outtieme}被取走",
                'keyword1'=>$express['expresssn'],
                'keyword2' => $shop['province'].$shop['city'].$shop['district'].$shop['address'],
                'keyword3' => $outtieme,
                'remark' => "如有疑义 请联系客服"
            )
        );
        $results = sendMsgTpl($tpl,$log);
        message("取件成功", $this->createMobileUrl('take'), "success");
    } else {
        message("取件失败", $this->createMobileUrl('take'), "error");
    }
}
include $this->template('take');