<?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'] : 'display';
if ($operation == 'display') {
    $ids = $_GPC['id'];
    $id = intval($ids);
    $status = intval($_GPC['status']);
    if (!is_array($ids)) {
        $ids = array(
            $ids
        );
    }
    foreach ($ids as $id) {
        if($status==5){
            $result= pdo_delete('evzg_express_settlement', array(
                'uniacid' => $_W['uniacid'],
                'id' => $id
            ));
        }else{
            if ($id <= 0)
                continue;
            if($status==2){
                $status=0;
            }
            $result= pdo_update('evzg_express_settlement', array(
                'enabled' => $status
            ), array(
                'uniacid' => $_W['uniacid'],
                'id' => $id
            ));
        }
    }
    $children = array();
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_settlement') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id ASC DESC");
    $settlement_list = pdo_fetchall("SELECT * FROM " . tablename('evzg_express_settlement') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id ASC LIMIT ". ($pindex - 1) * $psize . "," . $psize);
    $pager = pagination($total, $pindex, $psize);
    foreach ($settlement_list as &$v){
       $shop = pdo_get("evzg_express_shop",array('id'=>$v['sid']));
       $v['shopname'] = $shop['name'];
       $v['channellistall'] = count(json_decode($v['channellist'],true));
       $v['expresslistall'] = count(json_decode($v['expresslist'],true));
    }
    include $this->template('settlement');
} elseif ($operation == 'list') {
    $id = intval($_GPC['id']);
    $settlement = pdo_fetch("SELECT * FROM " . tablename('evzg_express_settlement') . " WHERE id = '$id'");
    $channels = json_decode($settlement['channellist'],true);
    $expresslist = json_decode($settlement['expresslist'],true);
    $alldakprice = $settlement['alldakprice'];
    $channelsall = count($channels);
    if($_W['isajax']){
        $titles = array();
        $fields = array();
        $stat = array();
        $fields[] ='total_success_order';
        $stat[] = array('total_success_order'=>$channelsall);
        $titles[] = $channels[0]['expressshop'];
        foreach ($channels as $ks){
            $titles[] = $ks['expressshop'];
            $fields[] = $ks['shipperCode'];
            $stat[] = array($ks['shipperCode']=>$ks['expresnum']);
        }
        $chart = array('fields'=>$fields,'titles'=>$titles,'stat'=>$stat,'channels'=>$channels);
        message(error(0, $chart), '', 'ajax');
    }
    include $this->template('settlement');
} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $settlement = pdo_fetch("SELECT id FROM " . tablename('evzg_express_settlement') . " WHERE id = '$id'");
    if (empty($settlement)) {
        message('抱歉，结算不存在或是已经被删除！', $this->createWebUrl('shop', array(
            'op' => 'display'
        )), 'error');
    }
    pdo_delete('evzg_express_settlement', array(
        'id' => $id));
    message('结算删除成功！', referer(), 'success');
}