<?php

class Support{
    
    public $sort = array(
        'support'   => array(
            'u_new'       =>'新问题',
            'u_again'     =>'补充提问',   
            'u_end'       =>'用户关闭',   
            'a_replied'   =>'已回复',     
            'a_end'       =>'解决完毕'    
        ),
        'sale'      => array(
            'u_request'   =>'<font color="red">申请退款</font>',
            'u_returning' =>'<font color="orange">退货已发出</font>',
            'a_accept'    =>'<font color="blue">同意</font>',     
            'a_refundend' =>'<font color="green">已退款</font>',
            'a_end'       =>'解决完毕'
        ),
    );

    public $wflow = array(
        'support'   =>array(
            'u_new'         => array('u_again','u_end','a_dealing','a_replied','a_end'),
            'u_again'       => array('u_again','u_end','a_dealing','a_replied','a_end'),
            'u_end'         => null,
            'a_replied'     => array('u_again','u_end','a_replied','a_end'),
            'a_end'         => null
        ),
        'sale'      =>array(
            'u_request'     => array(
                                'admin' => array('a_accept','a_refundend','a_end')),
            'u_returning'   => array(
                                'admin' => array('a_refundend','a_end')),
            'a_accept'      => array(
                                'user' => array('u_returning'),
                                'admin'  => array('a_end')),
            'a_refundend'   => null,
            'a_end'         => null
        )
    );


    
    
    
    public function addSupport($support){
        $sort = "sale"; 
        
        
        
        
        if(isset($support['user_id']) || isset($support['admin_id'])){
            if(!isset($support['admin_id'])){
                $support['admin_id'] = 1;
                $who = "user";
            }else{
                $who = "admin";
            }
            
            
            if(isset($support['sid']) && !empty($support['sid'])){
                $sql = "select * from support
                        where sid='".$support['sid']."'
                        order by id DESC";
                $re = DB::GetQueryResult($sql, true);
                $support['user_id'] = $re['user_id'];
                
                if( !in_array( $support['state'], $this->wflow[$sort][$re['state']][$who] ) ){
                    Session::Set('error','操作失败，请使用正确的操作流程，如有问题咨询管理员');
                    return false;
                }


            
            
            }else{
                $support['sid'] = $this->genSid();
                reset($this->wflow); 
                $support['state'] = key($this->wflow[$sort]); 
            }

            
            $sql ="update support set last=0 where sid='".$support['sid']."'";
            $re = DB::Query($sql); 
            $sql = "insert into support(sid,sort,user_id,admin_id,detail_id,title,detail,create_time,state,last)
                    values('".$support['sid']."','".$sort."',".$support['user_id'].",".$support['admin_id'].",'".$support['detail_id']."','".$support['title']."',
                        '".$support['detail']."',".time().",'".$support['state']."',1)";
            $re = DB::query($sql);
            return $support['sid'];

        }else{
            Session::Set('error','操作失败，请使用正确的操作流程，如有问题咨询管理员');
            return false;
        }
    }
    private static function genSid() {
        $sid = "";

        DB::Query('LOCK TABLES `sys` WRITE');
        $sid = Table::Fetch('sys', 1);
        $sid = $sid['sid'];
        if (substr($sid, 0, 8) == date('Ymd')) {
            $sid = $sid + 1;
        } else {
            $sid = date('Ymd') . '001';
        }
        Table::UpdateCache('sys', 1, array('sid' => $sid));
        DB::Query('UNLOCK TABLES');
        return $sid;
    }
    
    public static function opHtml($sort='sale',$state=null,$admin=false){
        $html ="";
        $s = new Support;
        if(empty($state)){
            reset($s->wflow);
            $state = key($s->wflow[$sort]); 
            $html = "<input type='submit' value='".strip_tags($s->sort[$sort][$state])."' onclick='$(\"#state\")[0].value=\"".$state."\"'>";
            return $html;
        }
        $op = $s->wflow[$sort][$state];
        if(isset($op)){
            if($admin){
                foreach($op['admin'] as $act){
                    $html .= "<input type='submit' value='".strip_tags($s->sort[$sort][$act])."' onclick='$(\"#state\")[0].value=\"".$act."\"'>&nbsp;&nbsp;";
                }
            }else{
                foreach($op['user'] as $act){
                    $html .= "<input type='submit' value='".strip_tags($s->sort[$sort][$act])."' onclick='$(\"#state\")[0].value=\"".$act."\"'>&nbsp;&nbsp;";
                }
            }
        }
        if($html=="")$html="<input type='button' disabled='disabled' value='无可用操作' />";
        return $html;
    }
    public static function getList($sid){
        $support = DB::LimitQuery('support', array(
            'condition' => array('sid' =>$sid)
        ));
        return $support;
    }
}

?>
