<?php

require_once(dirname(dirname(__FILE__)) . '/app.php');
need_login();

if(isset($_POST['sort']) && $_POST['sort']=='sale') $sort = 'sale';                         
if(isset($_GET['detail_id']) && $_GET['detail_id']!="") $detail_id = $_GET['detail_id'];    
if(isset($_GET['sid']) && $_GET['sid']!="") $sid = $_GET['sid'];                            


if(isset($sort) && $sort=='sale'){
    $support = array();
    $support['user_id'] = $login_user_id;
    $support['sort'] = 'sale';
    $error = "";
    if(!empty($_POST['sid'])) $support['sid'] = $_POST['sid'];
    empty($_POST['detail_id'])   ? $error = "没有指定相关项目！" : $support['detail_id'] = $_POST['detail_id'];
    empty($_POST['title'])      ? $error = "标题必须填写！" : $support['title'] = $_POST['title'];
    empty($_POST['detail'])     ? $error = "内容必须填写！" : $support['detail'] = $_POST['detail'];
    empty($_POST['state'])      ? $error = "操作错误！请咨询管理员！" : $support['state'] = $_POST['state'];
    if(!empty($error)){
        Session::Set('error',$error);
        redirect(WEB_ROOT."/support/support.php");
    }
    $s = new Support();
    $sid = $s->addSupport($support);
    if($sid!=false){
        Session::Set('success','提交成功！');
        redirect(WEB_ROOT."/support/support.php?sid=".$sid);
    }
}elseif(isset($_POST['sort']) && $_POST['sort']=='support'){

}else{

}



if(isset($sid) && $sid!=""){
    $sql = "select * from support where sid='".$sid."'
            order by id DESC";
    $support = DB::GetQueryResult($sql, FALSE);
    if($support[0]['user_id']!=$login_user_id){ 
        Session::Set('error','非法操作，请按照规范流程操作！');
        redirect(WEB_ROOT."/support/support.php");
    }
    $state = $support[0]['state'];
    
    if(!isset($s)){
        $s = new Support();
    }
    $detail_id = $support[0]['detail_id'];
}



if(isset($detail_id) && $detail_id!=""){
    $order_number = $detail_id;
    $sql = "select a.*,b.name as express_name
            from order_info as a,fare as b
            where a.order_number='".$order_number."'";
    $order = DB::GetQueryResult($sql, TRUE);
    if($order['user_id']!=$login_user_id){ 
        Session::Set('error','非法操作，请按照规范流程操作！');
        redirect(WEB_ROOT."/support/support.php");
    }
    $admin = false;
    $sort = "sale";
    
    include template('support_form');
    exit;
}





if(!isset($s)) $s = new Support();
$sql = "select a.*,b.username as admin_name from support as a,user as b
        where a.user_id=".$login_user_id." and a.last=1
        and a.admin_id=b.id
        order by a.id DESC";
$support = DB::GetQueryResult($sql,false);
include template('support_list');
?>
