<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 小区专家
 */
class Market_block_expert extends MY_Controller {

    /**
     * 小区专家类
     * @var Block_expert 
     */
    public $blockExpert;

    /**
     * 解析函数
     *
     * @access public
     * @return void
     */
    public function __construct() {
        parent::__construct();
        $this->load->model('Block_expert', 'blockExpert');
    }

    /**
     * Index Page for this controller.
     */
    public function index() {
        //加载broker模型类
        $this->load->model('broker_model');
        $css = load_css('zsb/css/v1.0/style1.css');
        $viewData = array();
        $viewData['page_title'] = '租售宝-营销中心-小区专家';
        $viewData['css'] = $css;
        //$info = $this->broker_model->get_user_session();print_r($info);
        //非小区专家也未申请
        if($this->blockExpert->isNothing()||$this->blockExpert->isExpired()
                ||$this->blockExpert->isFire()||$this->blockExpert->isReject()) {
            $viewData['block'] = $this->blockExpert->get_block();//所有的
            //$viewData['notblock'] = $this->blockExpert->get_block_full();//人满的
            $this->view('market', 'market/block_expert_apply_not', $viewData);
        }
        //正在申请
        if($this->blockExpert->isApply()) {
            $viewData['block'] = $this->blockExpert->get_block();//所有的
            $this->view('market', 'market/block_expert_apply_not', $viewData);
        }
        //是小区专家
        if($this->blockExpert->isExpert()){
            //小区信息
            $blockid = $this->blockExpert->getMyBlockId();
            $expert['block'] = $this->blockExpert->get_blockinfo($blockid);
            $expert['record'] = $this->blockExpert->getRecord();
            //到期时间
            $expert['time'] = $this->Sec2Time($expert['record']['endtime'] - time());
            $isApplyExpert = $this->blockExpert->is_apping();
            //符合条件的小区
            //$blocks = $this->blockExpert->get_block();
            //调模板
            $viewData['expert'] = $expert;
            $viewData['isApping'] = $isApplyExpert;
            $this->view('market', 'market/block_expert', $viewData);
        }
        
        
    }

    /**
     * 申请小区专家控制器
     */
    public function apply() {
        $blockid = $this->input->post("blockid");
        $reason = iconv('UTF-8', 'GBK', $this->input->post('reason'));
        $response = array();
        if (empty($blockid) || empty($reason)) {
            $response['status'] = -1; //参数错误
            $response['msg'] = '您的输入不正确！';
            return $this->ajaxResponse($response);
        }
        
        $blockinfo = $this->blockExpert->get_blockinfo($blockid);
        //var_dump($blockid);
        $result = $this->blockExpert->insert_block_apply($blockid, $blockinfo['blockname'], $reason);
        $response['status'] = $result;
        switch ($result) {
            case 1: $response['msg'] = "申请成功，已扣除500房豆，工作人员会尽快审核，请耐心等候！";
                break;
            case 2: $response['msg'] = "房豆不足，需不少于500房豆，谢谢！";
                break;
            case 3: $response['msg'] = "你已申请小区专家，请耐心等待工作人员审核。";
                break;
            case 4: 
                $response['msg'] = '您申请小区的专家名额已满！';
                break;
            case 5: 
                $response['msg'] = '已经申请过，请耐心等待审核！';
                break;
            default:$response['msg'] = "申请失败，请稍候再试。";
                break;
        }
        //添加 使用统计
        $info = $this->broker_model->get_user_session();
        $brokerId = $info['uid'];
        $gbuserbroker = new GrowthBidUserBroker();
        $gbuserbroker->addJoinBidRecord(1, $brokerId);
        return $this->ajaxResponse($response);
    }

    /**
     * 取消申请
     */
    public function cancel() {
        $blockid = intval($this->input->post("blockid"));
        $response = array();
        if (!$blockid) {
            $response['status'] = -1; //参数错误
            $response['msg'] = '您的输入不正确！';
            return $this->ajaxResponse($response);
        }
        $this->blockExpert->del_apply($blockid);
        $response['status'] = 1;
        $response['msg'] = '提交成功，500房豆已经返还！';
        return $this->ajaxResponse($response);
    }

    private function ajaxResponse($data, $encode = 'gbk') {
        if ($encode == 'gbk') {
            $data = $this->gbk2utf($data);
        }
        $this->output->set_content_type('application/json')
                ->set_output(json_encode($data));
    }

    private function gbk2utf($data) {
        if (is_array($data)) {
            foreach ($data as &$value) {
                $value = $this->gbk2utf($value);
            }
        } else {
            $data = iconv('gbk', 'utf-8', $data);
        }
        return $data;
    }

    private function Sec2Time($time) {
        if (is_numeric($time)) {
            $value = array(
                "years" => 0, "days" => 0, "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            if ($time >= 31556926) {
                $value["years"] = floor($time / 31556926);
                $time = ($time % 31556926);
            }
            if ($time >= 86400) {
                $value["days"] = floor($time / 86400);
                $time = ($time % 86400);
            }
            if ($time >= 3600) {
                $value["hours"] = floor($time / 3600);
                $time = ($time % 3600);
            }
            if ($time >= 60) {
                $value["minutes"] = floor($time / 60);
                $time = ($time % 60);
            }
            $value["seconds"] = floor($time);
            return (array) $value;
        } else {
            return (bool) FALSE;
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
