<?php
/**
 *
 * @author min晓天
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$tmpl = "doweb/";
$settings = $this->module['config'];
load()->func('tpl');
$operation = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$sid = $_SESSION['sid'];
//判断是否登录,如果没有登录，请重新登录
if (empty($sid)){
    message("您的登录出错！请重新登录。",mweb('login'),'error');
}
if ($operation == 'display') {

    $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_expressuser') . $condition, $params);
    $list  = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expressuser') . $condition . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
    //pdo_debug();die();
   foreach ($list as &$v){
       // $expres =pdo_fetchcolumn("SELECT COUNT(*) FROM".tablename("evzg_express_expres")."WHERE mobile='{$v['mobile']}'");
        $shop =pdo_fetch("SELECT name FROM".tablename("evzg_express_shop")."WHERE id='{$v['sid']}'");
       // $v['expresssn'] = $expres;
      // var_dump($shop);
        $v['shopname'] = $shop['name'];
        unset($v);
    }
   // die();
    //分页
    $pager = pagination($total, $pindex, $psize);
    include $this->template($tmpl.'expressuser');
}  elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $expres = pdo_fetch("SELECT id FROM " . tablename('evzg_express_expressuser') . " WHERE id = '$id'");
    if (empty($expres)) {
        message('抱歉，快递员不存在或是已经被删除！', mweb('expressuser', array(
            'op' => 'display','sid'=>$sid
        )), 'error');
    }
    pdo_delete('evzg_express_expressuser', array(
        'id' => $id,
    ));
    message('快递员删除成功！', mweb('expressuser', array(
        'op' => 'display'
    )), 'success');
} elseif ($operation == 'list') {
    $id = intval($_GPC['id']);
    $member = pdo_fetch("SELECT * FROM " . tablename('evzg_express_expressuser') . " WHERE id = '$id'");
    $condition      = ' WHERE uniacid = :aid AND mobile =:mobile';
    $params[':aid'] = $_W['uniacid'];
    $params[':mobile'] = $member['mobile'];
    $pindex           = max(1, intval($_GPC['page']));
    $psize            = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_expres') . $condition, $params);
    $lists  = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expres') . $condition . ' ORDER BY displayorder DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
    //分页
    $pager = pagination($total, $pindex, $psize);
    include $this->template($tmpl.'expressuser');
}elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    $shop = pdo_fetchall("SELECT * FROM " . tablename('evzg_express_shop') . " WHERE uniacid = '{$_W['uniacid']}' AND enabled = 1 ");
    if (! empty($id)) {
        $express = pdo_fetch("SELECT * FROM " . tablename('evzg_express_expres') . " WHERE id = '$id'");
    } else {
        $shops = array(
            'displayorder' => 0
        );
    }
    if (checksubmit('submit')) {
        if (empty($_GPC['mobile'])) {
            message('抱歉，请输入手机号名称！');
        }
        $user= pdo_fetch("SELECT * FROM".tablename("evzg_express_member")."WHERE openid='{$_GPC['openid']}'");
        if(empty($user)){
            message('快递员openid 不存在,快递员需要是已经注册的用户，请重新输入一下');
        }
        $data = array(
            'uniacid' => $_W['uniacid'],
            'openid' => $_GPC['openid'],
            'nickname' => $user['nickname'],
            'enabled' => intval($_GPC['enabled']),
            'avatar' => $user['avatar'],
            'sid' => $sid,
            'note' => $_GPC['note'],
            'mobile' => $_GPC['mobile'],
            'createtime' => time()
        );

        if (!empty($id)) {
            unset($data['parentid']);
            pdo_update('evzg_express_expressuser', $data, array(
                'id' => $id
            ));
            load()->func('file');
            file_delete($_GPC['thumb_old']);
        } else {
            $settings = $this->module['config'];
            $result = pdo_insert('evzg_express_expressuser', $data);
            $id = pdo_insertid();
        }
//         pdo_debug();
//         var_dump($data);die();
        message('更新快递员成功！', mweb('expressuser', array('op'=>'post')), 'success');
    }
    include $this->template($tmpl.'expressuser');
}
