<?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') {
     if (! empty($_GPC['displayorder'])) {
        foreach ($_GPC['displayorder'] as $id => $displayorder) {
            pdo_update('evzg_express_shop', array(
                'displayorder' => $displayorder
            ), array(
                'id' => $id
            ));
        }
    }
    $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_shop', array(
                'uniacid' => $_W['uniacid'],
                'id' => $id
            ));
        }else{
            if ($id <= 0)
                continue;
            if($status==2){
                $status=0;
            }
            $result= pdo_update('evzg_express_shop', array(
                'enabled' => $status
            ), array(
                'uniacid' => $_W['uniacid'],
                'id' => $id
            ));
        }
    }
    if($status>0||!empty($_GPC['displayorder'])){
        message('操作成功！', referer(), 'success');
    }
    $children = array();
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('evzg_express_shop') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id ASC, displayorder DESC");
    //$data = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_expressorder') . $condition . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
    $shops = pdo_fetchall("SELECT * FROM " . tablename('evzg_express_shop') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id ASC, displayorder DESC LIMIT ". ($pindex - 1) * $psize . "," . $psize);
    $pager = pagination($total, $pindex, $psize);
    include $this->template('shops');
} elseif ($operation == 'post') {
    $condition      = ' uniacid = :aid ';
    $params[':aid'] = $_W['uniacid'];
    $community  = pdo_fetchall('SELECT * FROM ' . tablename('evzg_express_community') . ' WHERE ' . $condition . ' ORDER BY displayorder DESC,id ASC  ', $params, 'id');
    $print = pdo_getall("evzg_express_printcompany",array('status'=>1));
    $parentid = intval($_GPC['parentid']);
    $id = intval($_GPC['id']);
    if (! empty($id)) {
        $shops = pdo_fetch("SELECT * FROM " . tablename('evzg_express_shop') . " WHERE id = '$id'");
    } else {
        $shops = array(
            'displayorder' => 0
        );
    }
    if (checksubmit('submit')) {
        if (empty($_GPC['name'])) {
            message('抱歉，请输入门店名称！');
        }
        $data = array(
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'],
            'enabled' => intval($_GPC['enabled']),
            'displayorder' => intval($_GPC['displayorder']),
            'village' => intval($_GPC['village']),
            'description' => $_GPC['description'],
            'province' => $_GPC['citys']['province'],
            'city' => $_GPC['citys']['city'],
            'district' => $_GPC['citys']['district'],
            'lat' => $_GPC['map']['lat'],
            'lng' => $_GPC['map']['lng'],
            'address' => $_GPC['address'],
            'openid' => $_GPC['openid'],
            'mobile' => $_GPC['mobile'],
            'credit2' => floatval($_GPC['credit2']),
            'password' => $_GPC['password'],
            'shipperCode' => $_GPC['shipperCode'],
            'thumb' => $_GPC['thumb'],
            'isprint' => intval($_GPC['isprint']),
            'chitnum'=> intval($_GPC['chitnum']),
			'msgtype' => intval($_GPC['msgtype']),
            'createtime'=>time()
        );
       // var_dump($data);die();
        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();
        }
         //var_dump(pdo_debug());die();
        message('更新门店成功！', $this->createWebUrl('shops', array(
            'op' => 'display'
        )), 'success');
    }
    include $this->template('shops');
} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $shops = pdo_fetch("SELECT id FROM " . tablename('evzg_express_shop') . " WHERE id = '$id'");
    if (empty($shops)) {
        message('抱歉，门店不存在或是已经被删除！', $this->createWebUrl('shop', array(
            'op' => 'display'
        )), 'error');
    }
    pdo_delete('evzg_express_shop', array(
        'id' => $id));
    message('门店删除成功！', referer(), 'success');
}