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

class real_broker extends MY_Controller {

    private $vaildcode_info;
    public $price;

    /**
     * 解析函数
     *
     * @access public
     * @return void
     */
    public function __construct()
    {
        //echo "test1"."<br/>";die;
        parent::__construct();
        $this->load->model('real_house_model');
        $this->load->model('broker_model');

        $this->price = 299;
    }

    public function write_log($par,$file_name='test.txt'){
        $myfile = fopen("./".$file_name, "w+") or die("Unable to open file!");
        if(!$par){
            $par='1111';
        }
        fwrite($myfile, $par);
        //fwrite($myfile, $par);
//记得关闭流
        fclose($myfile);
    }

    public function ancs(){
        $this->write_log('3333','test1.txt');

        $pos = $this->input->post();
        echo json_encode($pos);
        die;
    }

    /**
     * 申请提现
     */
    public function applyRemit()
    {

        $type = $this->input->post('type', TRUE);
        $result['status'] = 0;
        $result['status'] = 0;
        $result['msg'] = '';
        $result['data'] = array();
        $token = $this->input->post('token',TRUE);
        $broker_id = $this->input->post('broker_id', true);
        if ($token && $broker_id) {
            if ($token != md5($broker_id . '365zsb')) {
                $msg = 'token验证失败';
                $result['msg'] = iconv('gbk', 'utf8', $msg);
                echo json_encode($result);
                die;
            }
            $this->user_arr['uid'] = $broker_id;
        }else{
            $msg = '参数错误';
            $result['msg'] = iconv('gbk', 'utf8', $msg);
            echo json_encode($result);
            die;
        }

        //判断是否有房源为待处理状态
        $searchData = array();
        $searchData['uid'] = $this->user_arr['uid'];
        $searchData['report_result'] = 0;
        $count = $this->real_house_model->getReportCount($searchData);

        if ($count > 0) {
            $msg = '很抱歉！您账号中有' . $count . '条房源正处虚假审核处理中，处理完毕后即可进行提现申请，请您耐心等待！如有任何疑问，请电话咨询： ' . $this->getPhone(4);
            $result['msg'] = iconv('gbk', 'utf8', $msg);
            echo json_encode($result);
            die;
        }

        //判断是否有足够的保证金
        $realHouseMoney = $this->real_house_model->getBrokerRealHouseMoney($this->user_arr['uid']);
        if ($realHouseMoney < $this->price) {
            $msg = '保证金余额不足';
            $result['msg'] = iconv('gbk', 'utf8', $msg);
            echo json_encode($result);
            die;
        }

        //符合提现条件
        $this->real_house_model->applyRemit($this->user_arr['uid'],$type='company');

        $result['status'] = 1;
        $msg = '您的提现申请已提交成功，预计15工作日内将保证金原路退回，请您注意余额变动；';
        $result['msg'] = iconv('gbk','utf8',$msg);
        echo json_encode($result);die;
    }

    private function getPhone($type = 1)
    {
        $modelBroker = new Broker_model();
        $databaseSlave = $modelBroker->getDatabaseSlave();
        $result = $databaseSlave
            ->where('type =', $type)
            ->select('telno')
            ->get('telno_config')
            ->result_array();
        $phone = isset($result[0]['telno']) ? $result[0]['telno'] : '';
        return $phone;
    }
}
