<?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 openid='{$_W['openid']}' AND 	uniacid='{$_W['uniacid']}'";
$member = pdo_fetch($sql);
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'expressorder';
$sid = $_GPC['sid'];
 $condition = ' WHERE uniacid = :aid AND sid = :sid';
    $params[':aid'] = $_W['uniacid'];
    $params[':sid'] = $sid;

    $status         = $_GPC['status'];
    $keyword = trim($_GPC['keyword']);
    if (!empty($keyword)) {
        $condition .= " AND (name LIKE '%{$keyword}%' OR mobile LIKE '%{$keyword}%')";
        $status='all';
    }

    if ($status!='all') {
        $condition .= ' AND status = :stu';
        $params[':stu'] = $status;
    }
    if (!empty($_GPC['createtime'])) {
        $starttime = strtotime($_GPC['createtime']['start']);
        $endtime   = strtotime($_GPC['createtime']['end']) + 86399;
    } else {
        $starttime = strtotime('-30 day');
        $endtime   = TIMESTAMP;
    }
    $condition .= " AND createtime > :start AND createtime < :end";
    $params[':start'] = $starttime;
    $params[':end']   = $endtime;
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_expressorder') . $condition, $params);
    $data = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expressorder') . $condition . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
    foreach ($data 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['mobile'] = $member['mobile'];
        $v['nickname'] = $member['nickname'];
        unset($v);
    }
    $pager = pagination($total, $pindex, $psize);
if ($op == 'status') {
    $ids = $_GPC['id'];
    $id = intval($ids);
    $status = intval($_GPC['status']);
    if (!is_array($ids)) {
        $ids = array(
            $ids
        );
    }
    $status = intval($_GPC['status']);
    foreach ($ids as $id) {
       if($status==6){
            $result= pdo_delete('evzg_express_expressorder', array(
                'uniacid' => $_W['uniacid'],
                'id' => $id
            ));
        }else{
            if ($id <= 0)
                continue;
            $result= pdo_update('evzg_express_expressorder', array(
                'status' => $status
            ), array(
                'uniacid' => $_W['uniacid'],
                'id' => $id
            ));
        }
    }
    if($result){
        message('更新订状态成功', referer(), 'success');
    }else{
        message('更新订状态失败', referer(), 'error');
    }
}
if($op=='orderdel'){
    $id = intval($_GPC['id']);
    $result= pdo_delete('evzg_express_expressorder', array(
        'uniacid' => $_W['uniacid'],
        'id' => $id
    ));
    if($result){
        message('更新订状态成功', referer(), 'success');
    }else{
        message('更新订状态失败', referer(), 'error');
    }
}
if($op=='orderdetail'){
    $id        = intval($_GPC['id']);
    $data     = pdo_fetch('SELECT * FROM ' . tablename('evzg_express_expressorder') . ' WHERE uniacid = :aid AND id = :id', array(
        ':aid' => $_W['uniacid'],
        ':id' => $id
    ));
    if (empty($data)) {
        message('订单不存在或已经删除', referer(), 'error');
    }
   $exuser = pdo_fetch("SELECT * FROM".tablename("evzg_express_expressuser")."WHERE id='{$data['eid']}'");
   $data['exuser'] =$exuser['nickname'];
}
if($op=='print'){
    $id = intval($_GPC['id']);
    $data     = pdo_fetch('SELECT * FROM ' . tablename('evzg_express_expressorder') . ' WHERE uniacid = :aid AND id = :id', array(
        ':aid' => $_W['uniacid'],
        ':id' => $id
    ));
    if (empty($data)) {
        message('订单不存在或已经删除', referer(), 'error');
    }
    if(empty($data['shipperCode'])){
        message('用户下单没有选择快递公司', referer(), 'error');
    }
    $print= pdo_get("evzg_express_print",array('oid'=>$id,'resultCode'=>'100'));
    if(empty($print)){
        pdo_delete("evzg_express_print",array('oid'=>$id));
        $result = printEvg($id);
        $print =  pdo_get("evzg_express_print",array('oid'=>$id));
        if(!$result){
           message($print['reason'], referer(), 'error');
        }
    }
   $tpl = str_replace('<!DOCTYPE html><html lang="zh-CN">',"",$print['printTemplate']);
   $tpl = str_replace("</body></html>","",$tpl);
   $tpl = str_replace("<body>","",$tpl);
   $tpl = str_replace("<head>","<div>",$tpl);
   $tpl = str_replace("</head>","</div>",$tpl);
   include $this->template('printing');die();
}
include $this->template('expressorder');