<?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';
var_dump($operation);die();
if ($operation == 'display') {
    if (! empty($_GPC['displayorder'])) {
        foreach ($_GPC['displayorder'] as $id => $displayorder) {
            pdo_update('evzg_express_shop', array(
                'displayorder' => $displayorder
            ), array(
                'id' => $id
            ));
        }
        message('分类排序更新成功！', $this->createWebUrl('shop', array(
            'op' => 'display'
        )), 'success');
    }
    $children = array();
    $category = pdo_fetchall("SELECT * FROM " . tablename('evzg_express_shop') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id ASC, displayorder DESC");
   
    include $this->template('shop');
} elseif ($operation == 'post') {
    $parentid = intval($_GPC['parentid']);
    $id = intval($_GPC['id']);
    if (! empty($id)) {
        $category = pdo_fetch("SELECT * FROM " . tablename('evzg_express_shop') . " WHERE id = '$id'");
    } else {
        $category = array(
            'displayorder' => 0
        );
    }
    if (checksubmit('submit')) {
        if (empty($_GPC['catename'])) {
            message('抱歉，请输入分类名称！');
        }
        $data = array(
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['catename'],
            'enabled' => intval($_GPC['enabled']),
            'displayorder' => intval($_GPC['displayorder']),
            'isrecommand' => intval($_GPC['isrecommand']),
            'description' => $_GPC['description'],
            'template' => $_GPC['template'],
            'thumb' => $_GPC['thumb']
        );
        if (! empty($id)) {
            unset($data['parentid']);
            pdo_update('evzg_express_shop', $data, array(
                'id' => $id
            ));
            load()->func('file');
            file_delete($_GPC['thumb_old']);
        } else {
            pdo_insert('evzg_express_shop', $data);
            $id = pdo_insertid();
        }
        message('更新分类成功！', $this->createWebUrl('shop', array(
            'op' => 'display'
        )), 'success');
    }
    include $this->template('shop');
} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $category = pdo_fetch("SELECT id, parentid FROM " . tablename('evzg_express_shop') . " WHERE id = '$id'");
    if (empty($category)) {
        message('抱歉，分类不存在或是已经被删除！', $this->createWebUrl('shop', array(
            'op' => 'display'
        )), 'error');
    }
    pdo_delete('city_bst_category', array(
        'id' => $id,
        'parentid' => $id
    ), 'OR');
    message('分类删除成功！', $this->createWebUrl('shop', array(
        'op' => 'display'
    )), 'success');
}