<?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']}'";
$member = pdo_fetch($sql);
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$sid = $member['id'];
$putnum = shop_get_evzg_put($sid);
$outnum = shop_get_evzg_out($sid);
$overnum = shop_get_evzg_overplus($sid);
if (empty($member)) {
    message("你不是门店管理者,请先注册", $this->createMobileUrl('login', array(
        'op' => 'reg'
    )), "error");
}
if(empty($member['enabled'])){
    message("亲的店铺已被封停，请联系管理员开启",$this->createMobileUrl('shop'), "error");
}
if ($op == 'qj') {
    $sid = $member['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 mobile='{$express['mobile']}'");
    // var_dump($user);die();
    if ($result) {
        $log = array();
        $log['oid'] = $express['id'];
        $log['sid'] = $member['id'];
        $log['operator'] = "店铺：".$member['name'];
        $tpl = array(
            "touser" => $user['openid'],
            "template_id" => $settings['msg']['order_qj_id'],
            "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('express'),
            "topcolor" => "#FF0000",
            "data" => array(
                'first' => array(
                    'value' => urlencode("您好，你的快递{$express['expresssn']}已被取走" . "\\n"),
                    'color' => "#743A3A"
                ),
                'keyword1' => array(
                    'value' => urlencode($express['expresssn']),
                    'color' => '#000000'
                ),
                'keyword2' => array(
                    'value' => urlencode($member['address']),
                    'color' => '#EF4F4F'
                ),
                'remark' => array(
                    'value' => urlencode("感谢亲的使用"),
                    'color' => "#01579b"
                )
            )
        );
        sendMsgTpl($tpl,$log);
        message("取件成功", $this->createMobileUrl('take'), "success");
    } else {
        message("取件失败", $this->createMobileUrl('take'), "error");
    }
}
include $this->template('shop');