<?php
/**
 *
 * @author min晓天
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$settings = $this->module['config'];
load()->func('tpl');
$operation = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
$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'];
if ($operation == 'display') {

    $condition      = ' WHERE uniacid = :aid AND sid =:sid ';
    $params[':aid'] = $_W['uniacid'];
    $params[':sid'] = $sid;
    $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 ', $params);
    foreach ($list as &$v){
        $shop =pdo_fetch("SELECT name FROM".tablename("evzg_express_shop")."WHERE id='{$v['sid']}'");
        $v['shopname'] = $shop['name'];
        unset($v);
    }
    include $this->template('expressuser');
}elseif ($operation == 'add'){
    include $this->template('expressuser-add');
}elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $expres = pdo_fetch("SELECT id FROM " . tablename('evzg_express_expressuser') . " WHERE id = '$id'");
    if (empty($expres)) {
        message('抱歉，快递员不存在或是已经被删除！', $this->createMobileUrl('expressuer', array(
            'op' => 'display','sid'=>$sid
        )), 'error');
    }
    pdo_delete('evzg_express_expressuser', array(
        'id' => $id,
    ));
   exit(json_encode(array('code'=>200,'msg'=>'删除成功')));
} 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('expressuser');
}elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
        if (empty($_GPC['mobile'])) {
            message('抱歉，请输入手机号名称！');
        }
        $user= pdo_fetch("SELECT * FROM".tablename("evzg_express_member")."WHERE mobile='{$_GPC['mobile']}'");
        if(empty($user)){
            message('快递员openid 不存在,快递员需要是已经注册的用户，请重新输入一下手机号');
        }
        $data = array(
            'uniacid' => $_W['uniacid'],
            'openid' => $user['openid'],
            'nickname' => $user['nickname'],
            'enabled' => 1,
            'avatar' => $user['avatar'],
            'sid' => intval($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();
        }
        message('更新快递员成功！', $this->createMobileUrl('expressuser'), 'success');

}
