<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');
if(!$dosubmit) {

    if(empty($search['classcode'])) {
        $search['classcode'] = $_config['default_panel'];
    }
    if(empty($search['orderby'])) {
        $search['orderby'] = 'addtime'; 
        $search['ordertype'] = 'DESC';
    }
    if(is_array($search)) {
        if($search['classcode']) {
            if(strlen($search['classcode'])==2) {
                $where_and[] = "a.classcode BETWEEN '{$search['classcode']}00' AND '{$search['classcode']}99'";
            } elseif(strlen($search['classcode'])==4) {
                $where_and[] = "a.classcode='{$search['classcode']}'";
            }
        }
        if($search['areacode']) {
            if(strlen($search['areacode']) == 6) {
                $where_and[] = "a.streetcode='".$search['areacode']."'";
            } else {
                $where_and[] = "a.areacode='{$search['areacode']}'";
            }
        }
        if($search['keyword']) {
            $where_or[] = "CONCAT(a.shopname,a.subname) like '%{$search['keyword']}%'";
            $where_or[] = "a.address like '%{$search['keyword']}%'";
        }
        $where = $where_and ? implode(' AND ', $where_and) : '';
        $where .= $where ? ($where_or ? ' AND '.implode(' OR ', $where_or) : '') : ($where_or ? implode(' OR ', $where_or) : '');
        $where = $where ? 'WHERE a.status<>2 AND '.$where : 'WHERE a.status<>2 ';

        $orderby = $search['orderby'] .' '.$search['ordertype'];

        $total = $db->get_one("SELECT COUNT(*) as count FROM {$dbpre}shops a $where");
        if($total['count'] > 0) {
            $offset = 10;
            $limit = get_limit($page, $offset);
            $query = $db->query("SELECT a.sid,a.shopname,a.subname,a.classcode,a.reviews,a.pictures,a.status,a.addtime,a.finer,b.classname,c.areaname FROM {$dbpre}shops a LEFT JOIN {$dbpre}classcode b ON(a.classcode=b.classcode) LEFT JOIN {$dbpre}areacode c ON(a.areacode=c.areacode) $where ORDER BY $orderby LIMIT $limit");
            while($shop = $db->fetch_array($query)) {
                $status_arr = array('1'=>'正常','2'=>'未审核','3'=>'停止营业','4'=>'暂停营业','5'=>'尚未开业','6'=>'隐藏显示');
                $shop['statusname'] = $status_arr[$shop['status']];
                $pcd = substr($shop['classcode'],0,2);
                $shop['panelname'] = $_panels[$pcd];
                $shops[] = $shop;
            }

            $multurl = "";
            if($search) foreach($search as $key => $value) {
                $multurl .= '&search'.rawurlencode('['.$key.']').'='.rawurlencode($value);
            }

            $multurl = "admincp.php?action=$action&file=$file".$multurl;
            $multipage = multi($total['count'], $offset, $page, $multurl);
        }
    }
    include cptpl('itemlist', MOD_ROOT.'./admin/templates/');

} else {

    if(!is_array($shop)) {
        cpmsg('无商铺进行操作，请返回修改。');
    } else {
        if(empty($operation)) {
            foreach($finer as $id => $fvalue) {
                $db->update("UPDATE {$dbpre}shops SET finer='".intval($fvalue)."' WHERE sid='$id'");
            }
        } else {
            if($shopids && is_array($shopids)) {
                if(!function_exists('deleteItem')) {
                    require_once(MUDDER_ROOT.'./'.$_modules['shop']['directory'].'/include/fun_item.php');
                }
                if($operation == 'delete') {
                    deleteItem($shopids,-1,$delete_subpoint);
                } elseif($operation == 'rebulid') {
                    rebuildItem($shopids);
                } elseif($operation == 'unfiner' || $operation == 'finer') {
                    $ids = "'".implode("','", $shopids)."'";
                    $finer = $operation == 'unfiner' ? 0 : 1;
                    $db->update("UPDATE {$dbpre}shops SET finer='$finer' WHERE sid IN ($ids)");
                } else {
                    cpmsg('未选择', $backrul);
                }
            }
        }
        //$cacheclass->Read_Shops('finershops','',TRUE);
        $backrul = $_SERVER['HTTP_REFERER'];
        cpmsg('商铺更新完毕！', $backrul);
    }
}
?>