<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Description of sell_modify
 *
 * @author user
 */
class Sell_modify extends MY_Controller {
    
    /**
    * 解析函数
    *
    * @access public
    * @return void
    */
    public function __construct() 
    {   
        parent::__construct();
    }
    
    /**
     * 渲染房源信息
     * @param type $data_page
     */
    private function render_house($data_page = array())
    {
        //头部
        $data_page['page_title'] = '租售宝';
        $data_page['js'] = load_js('common/js/swf/swfupload.js,'
                . 'zsb/js/v1.0/uploadpic.js,common/js/jquery.validate.min.js,'
                . 'common/js/jquery_ui_autocomplete/ui/jquery.ui.core.js,'
                . 'common/js/jquery_ui_autocomplete/ui/jquery.ui.position.js,'
                . 'common/js/jquery_ui_autocomplete/ui/jquery.ui.widget.js,'
                . 'common/js/jquery_ui_autocomplete/ui/jquery.ui.autocomplete.js');
        
        //页面加载需要的数据
        $data_page['page_js'] = load_js('zsb/js/v1.0/house.js');
        $data_page['css'] = load_css('zsb/css/v1.0/style1.css,zsb/css/v1.0/autocomlete.css');
        $data_page['house'] = $this->config->item('house');
        $data_page['upload_pic_url'] = $this->config->item('base_url') . 'pic/house/';
        //加载模板
        //$this->load->view('header', $data_header);
        //$this->load->view('sell_modify_left');
        //$this->load->view('sell_modify', $data_page);
        //$this->load->view('footer');
        
        $this->view('house_manage', 'sell_modify', $data_page);
    }
    
    /**
     * 发布房源页面
     */
    public function add() 
    { 
        //可发出售和出租的权限
        //读取房源模板信息
        //获取历史小区信息
        //判断库存是否已满
        //统计代码
        //加载模板
        $data_page['method'] = 'add';
        $this->render_house($data_page);
    }
    
    /**
     * 插入房源数据 model
     */
    public function insert() 
    {      
        //一、判断房源是否有否超过上限
        
        
        //二、获取经纪人UID
        $fields_values = array();
        $fields_values['uid'] = 1501078;
        
        //三、插入房源信息
        $this->load->model('sell_model');
        $base_info_fields = array(
            'infotype', 'infofrom', 'agentcode', 'esta', 'district', 'streetid',
            'blockid', 'blockshowname', 'address','price', 'priceterm', 'pricetype',
            'buildarea', 'subfloor', 'floor', 'totalfloor', 'room', 'hall', 'toilet',
            'kitchen', 'balcony' ,'fitment', 'forward', 'mright', 'gardenarea', 'remark',
            'ownid', 'buildyear', 'baseservice'
        );
        //别墅类型
        $fields_values['housekind'] = '';
        //商业分类
        $fields_values['tradeclass'] = '';
        //新房直售
        $fields_values['nofee'] = 0;
        //非优质房源
        $fields_values['grade'] = 0;
        $fields_values['contactor'] = '';
        $fields_values['telno'] = 13851894682;
        $fields_values['feature'] = '';
        $fields_values['creattime'] = time();
        $fields_values['updatetime'] = time();
        $fields_values['expiretime'] = time();
        $fields_values['lucupdatetime'] = time();
        
        /************以下字段暂时没有作用**********/
        $fields_values['school'] = '';
        $fields_values['mobile'] = '';
        $fields_values['cbd'] = 0;
        $fields_values['width'] = 0;
        $fields_values['depth'] = 0;
        $fields_values['note'] = '';
        $fields_values['bieshutop'] = 0;
        $fields_values['bangongtop'] = 0;
        $fields_values['shangputop'] = 0;
        $fields_values['hangye'] = '';
        $fields_values['shop_staus'] = 0;
        /***************************************/
        foreach($base_info_fields as $v)
        {
            if ($v == 'uid')
            {
                $fields_values[$v] = 1501078;
            }
            if ($v == 'infotype')
            {
                $fields_values[$v] = 1;
            }
            if ($v == 'infofrom')
            {
                $fields_values[$v] = 1;
            }
            if (!isset($fields_values[$v]))
            {
                $fields_values[$v] = $this->input->post($v);
            }
        }
        $house_id = $this->sell_model->insert($fields_values);

        //四、处理房源图片
        $pics = array();
        $pfile_name = $this->input->post('p_filename');
        $p_sort = $this->input->post('p_sort');
        $p_title = $this->input->post('p_title');
        if (isset($pfile_name) && $pfile_name != '') 
        {
            foreach ($filed_values as $k => $v)
            {
                $pics[] = array(
                    'url' => $v, 'sort' => $p_sort[$k],  
                    'title' => $p_title[$k],
                );
            }
        }
        $this->sell_model->insert_house_pic($house_id, $house_id);
        
        //判断优质，多图 先有多图再有优质
        $is_star_house = $this->sell_model->find_star_house($house_id);
        
        $is_grade_house = $this->sell_model->find_grade_house($house_id);
        
        //对房源积分
        
        //对房成长值的操作
    }
    
    /**
     * 展示房源修改页面
     */
    public function modify() 
    {
        //加载测试模型类
        $this->load->model('sell_model');
        
        //获取单条房源信息
        //select * from sell where uid = 10000040 and id = 37700972;
        $house_detail = $this->sell_model->find_by_uid_id(10000040, 37700972);
        print_r($house_detail);
    }
    
    /**
     * 修改房源 model
     */
    public function update()
    {
        $this->load->model('sell_model');
        $update_data = array('esta' => 5);
        //update sell set esta = 5 where id in(33899521, 37700775) and uid = 10000040;
        $this->sell_model->update($update_data, array(33899521, 37700775), 10000040);
    }

    /**
     * 物理删除房源，如需更改房源状态为删除，请调用update方法
     */
    public function delete()
    {
        $this->load->model('sell_model');
        //delete from  sell where id = 33899521 and uid = 10000040;
        $this->sell_model->delete(33899521, 10000040);
    }
    
    /**
     * 获取某个经纪人的房源列表
     */
    public function manage()
    {
        //加载测试模型类
        $this->load->model('sell_model');
        $conf = "uid = 10000040 AND esta = 1";
        //select * from sell where uid = 10000040 AND esta = 1
        $house_list = $this->sell_model->find_all_by($conf);
        print_r($house_list);
    }
    
    /**
     * 加载swfupload上传文件组件
     */
    public function swf_upload()
    {
        $this->load->view('pic_inc');
    }
    
    //补充信息
    public function add_ext()
    {
        
    }
}
