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

    case 'process':
        if(!$dosubmit) {

            $apply = $db->get_one("SELECT i.*,s.shopname,s.subname FROM {$dbpre}itemapply i LEFT JOIN {$dbpre}shops s ON(i.shopid=s.sid) WHERE applyid='$applyid'");
            if(empty($apply)) cpmsg('认领信息不存在。','stop');
            $apply['fullname'] = $apply['shopname'].$apply['subname'];
            include cptpl('itemapply_process', MOD_ROOT.'./admin/templates/');

        } else {

            if(!validKey($applyid)) cpmsg('无效的申请认领ID。','stop');
            if($status == 3) {
                $returned = strip_textarea(unite_textarea($post['content']));
                $db->update("UPDATE {$dbpre}itemapply SET status=3,returned='$returned',checker='$admin->adminname' WHERE applyid='$applyid'");
            } elseif($status == 1) {
                $owner = $db->get_one("SELECT shopid,uid,username FROM {$dbpre}itemapply WHERE applyid='$applyid'");
                if(empty($owner)) cpmsg("认领信息不存在或已删除。",'stop');
                $db->update("UPDATE {$dbpre}shops SET ownerid='{$owner['uid']}', owner='{$owner['username']}' WHERE sid='{$owner['shopid']}'");
                $db->update("UPDATE {$dbpre}itemapply SET status=1,checker='$admin->adminname' WHERE applyid='$applyid'");
            }
            cpmsg("认领信息处理完毕！","admincp.php?action=$action&file=$file");

        }
        break;
    default:

        if(!$dosubmit) {

            $apply = $where = array();

            $status = empty($status) || $status < 0 ? 2 : $status;
            $from = "{$dbpre}itemapply i LEFT JOIN {$dbpre}shops s ON(i.shopid=s.sid)";
            $where[] = "i.status='$status'";
            $pcd = isset($pcd) ? (preg_match("/^[0-9]{2}$/",$pcd) ? $pcd : '') : '';
            $pcd && $where[] = "s.classcode BETWEEN '{$pcd}00' AND '{$pcd}9999'";
            $where = implode(" AND ", $where);
            $total = $db->get_value("SELECT COUNT(*) FROM $from WHERE $where");
            if($total > 0) {
                $limit = get_limit($page, $offset=10);
                $multipage = multi($total, $offset, $page, 'admincp.php?action='.$action.'&file='.$file);
                $query = $db->query("SELECT i.*,s.shopname,s.subname FROM $from WHERE $where ORDER BY dateline DESC LIMIT $limit");
                while($row = $db->fetch_array($query)) {
                    $row['fullname'] = $row['shopname'].$row['subname'];
                    $apply[] = $row;
                }
            }
            include cptpl('itemapply', MOD_ROOT.'./admin/templates/');

        } else {

            if(!is_array($applyids) || count($applyids)==0) cpmsg('未选择操作项，请返回选择。');
            $myids = "'".implode("','", $applyids)."'";
            $db->query("DELETE FROM {$dbpre}itemapply WHERE applyid IN($myids)");
            unset($myids);
            cpmsg('您选择的操作项已被删除。',"admincp.php?action=$action&file=$file");

        }
}
?>