<?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 enabled='1' AND 	uniacid='{$_W['uniacid']}' ";
$shop = pdo_fetchall($sql);
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'list';
$openid = $_W['openid'];
if($op=='ajax'){
    $condition = ' WHERE e.uniacid = :aid AND e.openid = :openid AND e.sid = s.id';
    $params[':aid'] = $_W['uniacid'];
    $params[':openid'] = $openid;
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_expressorder') . " WHERE uniacid = {$_W['uniacid']} AND openid = {$openid}");
    $list = pdo_fetchall('SELECT e.*,s.mobile as shopname FROM ' . tablename('evzg_express_expressorder').'as e,'.tablename('evzg_express_shop').'as s' . $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']="暂无";
        }
        $v['status'] = $stats[$v['status']];
        $v['nickname'] = $v['name'];
        $v['createtime'] =date("Y-m-d H:i:s",$v['createtime']);
        unset($v);
    }
    exit(json_encode(array('code'=>200,'content'=>$list)));

}
//通知快递员
if($op=="spendMsg"){
    $oid = $_GPC['oid'];
    $order = pdo_fetch("SELECT * FROM " . tablename('evzg_express_expressorder') . " WHERE id = '{$oid}' AND status=2");
    if($order){
      $expressuser = pdo_fetch("SELECT * FROM " . tablename('evzg_express_expressuser') . " WHERE id = '{$order['eid']}'");
      if(empty($expressuser)){
         $expressuser = pdo_get("evzg_express_shop",array('id'=>$order['sid']));
      }
      $log = array();
      $log['oid'] = $oid;
      $log['sid'] = $order['sid'];
      $log['operator'] = "用户：".$order['name'];
      $tpl = array(
          'openid'=>$expressuser['openid'],
          "template_id" => $settings['msg']['order_jd_cd'],
          "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('dyindex'),
          "data" => array(
              'first' => "要加油了！客户等得不耐烦啦，急需要您的处理。别让人家等太久哟！↖(^ω^)↗",
              'keyword1' => "{$order['id']}",
              'keyword2' => "用户已支付",
              'keyword3' => $order['price']."元",
              'remark' => "\n 地址：{$order['address']} \n联系方式：{$order['mobile']} \n物品：{$order['goods']} \n重量：{$order['weight']}kg "
          )
      );
      $tplLog = sendMsgTpl($tpl,$log);
      exit(json_encode(array('code'=>200,'msg'=>'通知成功')));
    }else{
        exit(json_encode(array('code'=>400,'msg'=>'通知成功')));
    }
}
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));
    if($result){
        if($stats==1){
            pdo_update("evzg_express_expressorder",array('price'=>floatval($_GPC['price'])),array('id'=>$oid));
            $template = array(
                "touser" => $order['openid'],
                "template_id" => $settings['msg']['order_jd_bg'],
                "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('userorder'),
                "topcolor" => "#FF0000",
                "data" => array(
                    'first' => array(
                        'value' => urlencode("您好，你的快递预约已接收,请支付一下金额" . "\\n"),
                        'color' => "#743A3A"
                    ),
                    'keyword2' => array(
                        'value' =>  urlencode('待支付'),
                        'color' => '#000000'
                    ),
                    'keyword1' => array(
                        'value' =>urlencode(date('Y年m月d日 H:i:s', time())),
                        'color' => '#EF4F4F'
                    ),
                    'remark' => array(
                        'value' => urlencode("支付之后,快递员将会上面取件"),
                        'color' => "#01579b"
                    )
                )
            );
            $result =  sendMsg(urldecode(json_encode($template)));
        }elseif($stats==3){
            pdo_update("evzg_express_expressorder",array('status'=>$stats,'expresssn'=>$_GPC['expresssn']),array('id'=>$oid));
            $template = array(
                "touser" => $order['openid'],
                "template_id" => $settings['msg']['order_jd_ds'],
                "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('express'),
                "topcolor" => "#FF0000",
                "data" => array(
                    'first' => array(
                        'value' => urlencode("您好，你的快递{$_GPC['expresssn']}已准备发送" . "\\n"),
                        'color' => "#743A3A"
                            ),
                            'keyword1' => array(
                                'value' => urlencode($_GPC['expresssn']),
                                'color' => '#000000'
                            ),
                            'keyword2' => array(
                                'value' => urlencode(date('Y年m月d日 H:i:s', time())),
                                'color' => '#EF4F4F'
                            ),
                            'remark' => array(
                                'value' => urlencode("\\n感谢您的使用"),
                                'color' => "#01579b"
                            )
                )
            );
            $result =  sendMsg(urldecode(json_encode($template)));
        }elseif($stats==5){
            $expressuser = pdo_fetch("SELECT * FROM " . tablename('evzg_express_expressuser') . " WHERE id = '{$order['eid']}'");
            if(!empty($expressuser)){
                $template = array(
                    "touser" => $expressuser['openid'],
                    "template_id" => $settings['msg']['order_jd_qx'],
                    "url" => $_W['siteroot'] . 'app/' . $this->createMobileUrl('expressorder'),
                    "topcolor" => "#FF0000",
                    "data" => array(
                        'first' => array(
                            'value' => urlencode("{$order['name']}客户取消订单 请注意" . "\\n"),
                            'color' => "#743A3A"
                        ),
                        'keyword3' => array(
                            'value' => urlencode($order['mobile']),
                            'color' => '#000000'
                        ),
                        'keyword2' => array(
                            'value' => urlencode("快件邮寄"),
                            'color' => '#000000'
                        ),
                        'keyword1' => array(
                            'value' => urlencode($order['id']),
                            'color' => '#EF4F4F'
                        ),
                        'remark' => array(
                            'value' => urlencode("预约取件地址:{$order['address']}"),
                            'color' => "#01579b"
                                )
                    )
                );
                $result= sendMsg(urldecode(json_encode($template)));
            }
        }
        exit(json_encode(array('code'=>200,'msg'=>'操作成功')));
    }else{
        exit(json_encode(array('code'=>400,'msg'=>'操做失败')));
    }

}
if($op=='pays'){
    $id = $_GPC['id'];
    if(empty($id)){
        exit(json_encode(array('code'=>'400','mgs'=>'订单出错！')));
    }
    $result = pdo_fetch("SELECT * FROM".tablename('evzg_express_expressorder')."WHERE id='{$id}' AND  paystatus=0 AND status=1");
    if(empty($result)){
        exit(json_encode(array('code'=>'400','mgs'=>'订单已支付,或者不存在！')));
    }
    $orderid = 'evzgpay'.$result['id']."y".time();
    exit(json_encode(array('code'=>'200','fee'=>$result['price'],'ordertid'=>$orderid)));
}
include $this->template('userorder');