<?php
/**
 *
 * @author min晓天
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;

load()->func('tpl');
$operation = ! empty($_GPC['op']) ? $_GPC['op'] : 'list';
if ($operation == 'list') {
    $company = pdo_get("evzg_express_printcompany",array('uniacid'=>$_W['uniacid']));
    if(empty($company)){
        $insql ="INSERT INTO `ims_evzg_express_printcompany` (`uniacid`, `expressname`, `ShipperCode`, `status`) VALUES
            ({$_W['uniacid']}, '顺丰', 'SF', 0),
            ({$_W['uniacid']}, 'EMS(广东)', 'EMS', 0),
            ({$_W['uniacid']}, '韵达', 'YD', 0),
            ({$_W['uniacid']}, '圆通', 'YTO', 0),
            ({$_W['uniacid']}, '百世快递', 'HTKY', 0),
            ({$_W['uniacid']}, '圆通', 'YTO', 0),
            ({$_W['uniacid']}, '百世快递', 'HTKY', 0),
            ({$_W['uniacid']}, '申通', 'STO', 0),
            ({$_W['uniacid']}, '中通', 'ZTO', 0),
            ({$_W['uniacid']}, '天天快递', 'HHTT', 0),
            ({$_W['uniacid']},  '京东', 'JD', 0),
            ({$_W['uniacid']}, '全一快递', 'UAPEX', 0),
            ({$_W['uniacid']}, '优速', 'UC', 0),
            ({$_W['uniacid']}, '宅急送', 'ZJS', 0),
            ({$_W['uniacid']}, 'EMS', 'EMS', 0),
            ({$_W['uniacid']}, '安能', 'ANE', 0),
            ({$_W['uniacid']}, '国通', 'GTO', 0),
            ({$_W['uniacid']}, '德邦', 'DBL', 0),
            ({$_W['uniacid']}, '品骏快递', 'PJ', 0);";
        pdo_query($insql);
    }
    $condition = ' WHERE uniacid = :aid';
    $params[':aid'] = $_W['uniacid'];
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_printcompany') . $condition, $params);
    $list = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_printcompany') . $condition . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);

    // 分页
    $pager = pagination($total, $pindex, $psize);
    include $this->template('print');
}
if ($operation == 'change') {
    $id = intval($_GPC['id']);
    pdo_update("evzg_express_printcompany",array('status'=>$_GPC['status']),array('id'=>$id));
    message('设置快递公司成功！', $this->createWebUrl('print', array(
        'op' => 'list',
    )), 'success');
}

if ($operation == 'print_post') {
    if ($_W['ispost']) {
         var_dump($_GPC);die();
     }
    include $this->template('print_post');
}
if ($operation == 'evzg_post') {
    $item = pdo_get("evzg_express_printcompany",array('id'=>$_GPC['id']));
    if ($_W['ispost']) {
        $data = array();
        $data['uniacid'] = $_W['uniacid'];
        $data['expressname'] = $_GPC['expressname'];
        $data['ShipperCode'] = $_GPC['ShipperCode'];
        $data['status'] = $_GPC['status'];
        $data['dakprice'] = $_GPC['dakprice'];
        if(!empty($_GPC['id'])){
           pdo_update("evzg_express_printcompany",$data,array('id'=>$_GPC['id']));
        }else{
             pdo_insert("evzg_express_printcompany",$data);
        }
        message('设置快递公司成功！', $this->createWebUrl('print', array(
            'op' => 'list',
        )), 'success');
    }
    include $this->template('evzg_post');
}