<?php
/**
 * 房源模板控制器
 *
 * @author sun
 */
class House_Template_Wang extends MY_Controller {
    
    /**
     * 初始化，并加载房源模板业务
     */
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('house_template_model');
    }
   
    /**
     * 渲染添加、修改房源模板页面
     * @param array $data_page 页面参数
     */
    private function render_house_template($data_page = array())
    {
        //头部 标题和js
        $data_page['js'] = load_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['css'] = load_css('zsb/css/v1.0/autocomlete.css,'
                . 'zsb/css/v1.0/style2.css');
        //页面加载所需要的数据
        $data_page['page_js'] = load_js('zsb/js/v1.0/house.js');
        $data_page['house'] = $this->config->item('house');
        //加载模板
        $this->view('house_template', 'house_template_wang', $data_page);
    }
    
    /**
     * 添加房源模板页
     * @param string $module 模板类型，sell和rent 默认为sell
     */
    public function add($module = '')
    {
        //添加页基本参数
        $module = $this->house_template_model->check_module($module);
        $data_page['module'] = $module;
        $data_page['module_name'] = $this->house_template_model->find_module_name($module);
        $data_page['method'] = 'add';
        $data_page['method_name'] = '添加';
        $data_page['url_manage'] = $this->house_template_model->find_func_url('manage');
        $data_page['action'] = $this->house_template_model->
                find_func_url('insert') . $module;
        //发布房源时保存的模板
        $data_page['block_name'] = $this->input->post('t_blockname');
        $data_page['block_id'] = $this->input->post('t_block_id');
        $data_page['district'] = $this->input->post('t_district');
        $data_page['street'] = $this->input->post('t_street');
        $data_page['address'] = $this->input->post('t_blockaddress');
        $data_page['remark'] = $this->input->post('t_remark');
        $data_page['page_title'] = '房源管理-' . $data_page['method_name'] 
                . $data_page['module_name'] . '房源模板';
         $data_page['documentation'] = $this->config->item('documentation');//帮助文档
        $this->render_house_template($data_page);
    }
    
    /**
     * 保存房源模板信息
     * 接受的参数有 模板名称，小区名称，小区编号，类型，详细描述
     * 验证模板个数是否超过上限、模板名称是否重复
     * @return bollean 成功返回跳转到模板列表页
     */
    public function insert()
    {
        //当前在哪个模块
        $module = $this->input->post('module');
        //操作成功后返回模板列表页
        $url_manage_module = $this->house_template_model->
                find_func_url('manage') . $module . '/';
        //判断参数的有效性 模板名称、房源描述小区为必填项
        if (ltrim($this->input->post('template_name') == '') 
                || ltrim($this->input->post('blockshowname')) == ''
                || ltrim($this->input->post('remark')) == '')
        {
            //跳转页面
            $this->jump($url_manage_module, '模板名称、小区信息和房源描述为必填项');
            return;
        }
        //判断模板的个数有没有超过上限
        $moudle_template_num = $this->house_template_model->find_count_by_module(
                $module, $this->user_arr['uid']);
        //存在模板数 >= 模板上限
        if($moudle_template_num >= $this->house_template_model->template_num)
        {
            $page_text = $module == 'sell' ? '对不起，您的出售房源模板数量已满'
                    . '，请及时删除不需要的模板！' : '对不起，您的出租房源模板数量已'
                . '满，请及时删除不需要的模板！';
            $this->jump($url_manage_module, $page_text);
            return;
        }
        //组装sql数据
        $template_info = array(
            'template_name' => ltrim($this->input->post('template_name')),
            'block_id'      => $this->input->post('block_id'),
            'block_name'    => $this->input->post('blockshowname'),
            'broker_id'     => $this->user_arr['uid'],
            'remark'        => $this->input->post('remark'),
            'update_time'   => time(),
            'flag'          => $this->house_template_model->flag($module),
        );
        //判断同名模板
        $same_template_name_num = $this->house_template_model->find_count_by_name(
                $template_info['template_name'], $module, $this->user_arr['uid']);
        if ($same_template_name_num > 0)
        {
            $page_text = "模板名称已存在!";
        }
        else
        {
            //保存模板 
            $insert_id = $this->house_template_model->insert($template_info);
            $page_text = $insert_id ? '模版保存成功!' : '模版保存失败!';
        }
        
        //跳转页面
        $this->jump($url_manage_module, $page_text);
        return;
    }
    
    /**
     * 修改房源模板页面
     * 接受参数 id编号 模板类型
     * @param string $module 模板类型，sell和rent 默认为sell
     */
    public function modify($module = '', $id = '')
    {
        //修改页基本参数
        $module = $this->house_template_model->check_module($module);
        if ($module == '' || $id == '') 
        {
            $this->jump($this->house_template_model->
                find_func_url('manage') . $module . '/', '参数不合法', 3000);
            return;
        }
        $data_page['module'] = $module;
        $data_page['module_name'] = $this->house_template_model->find_module_name($module);
        $data_page['method'] = 'modify';
        $data_page['method_name'] = '修改';
        $data_page['url_manage'] = $this->house_template_model->find_func_url('manage');
        $data_page['action'] = $this->house_template_model->
                find_func_url('update') . $module;
        //查询房源模板信息
        $template_detail = $this->house_template_model->find_by_id($id, 
                $this->user_arr['uid']);
        if ($template_detail)
        {
            $block_id = $template_detail['block_id'];
            $data_page['id'] = $template_detail['id'];
            $data_page['template_name'] = $template_detail['template_name'];
            $data_page['block_name'] = $template_detail['block_name'];
            $data_page['block_id'] = $block_id;
            $data_page['remark'] = $template_detail['remark'];
            $this->load->model('block_model');
            //查找小区的基本信息
            $block_detail = $this->block_model->base_info_by_id($block_id);
            if ($block_detail)
            {
                $data_page['district'] = $block_detail['district'];
                $data_page['street'] = $block_detail['streetname'];
                $data_page['address'] = $block_detail['address'];
            }
            $data_page['documentation'] = $this->config->item('documentation');//帮助文档
            $data_page['page_title'] = '房源管理-' . $data_page['method_name'] 
                                     . $data_page['module_name'] . '房源模板';
            $this->render_house_template($data_page);
        } 
        else 
        {
            $this->jump($this->house_template_model->
                find_func_url('manage') . $module . '/');
        }
    }
    
    /**
     * 更新模板信息
     * 接受的参数有 模板名称，小区名称，小区编号，类型，详细描述
     * 验证模板名称是否重复
     * @return bollean 成功返回跳转到模板列表页
     */
    public function update()
    {
        //当前在哪个模块
        $module = $this->input->post('module');
        //模板编号
        $id = $this->input->post('id');
        //操作成功后返回模板列表页
        $url_manage_module = $this->house_template_model->
                find_func_url('manage') . $module . '/';
        //判断参数的有效性 模板名称、小区和房源描述为必填项
        if (ltrim($this->input->post('template_name') == '') 
                || ltrim($this->input->post('blockshowname')) == ''
                || ltrim($this->input->post('remark')) == '')
        {
            //跳转页面
            $this->jump($url_manage_module, '模板名称、小区信息和房源描述为必填项');
            return;
        }
        $url_manage_module = $this->house_template_model->
                find_func_url('manage') . $module;
        //组装sql数据
        $template_info = array(
            'template_name' => ltrim($this->input->post('template_name')),
            'block_id'      => $this->input->post('block_id'),
            'block_name'    => $this->input->post('blockshowname'),
            'remark'        => $this->input->post('remark'),
            'update_time'   => time(),
            'flag'          => $this->house_template_model->flag($module),
        );
        //查询当前房源模板信息
        $template_detail = $this->house_template_model->find_by_id($id, 
                $this->user_arr['uid']);
        //修改房源模板名称
        if ($template_detail['template_name'] != $template_info['template_name']) 
        {
            //判断同名模板
            $same_template_name_num = $this->house_template_model->find_count_by_name(
                    $template_info['template_name'], $module, $this->user_arr['uid']);
            if ($same_template_name_num > 0)
            {
                $page_text = "模板名称已存在!";
                //跳转页面
                $this->jump($url_manage_module, $page_text);
                return;
            } 
        }
        $affected_rows = $this->house_template_model->update($template_info, $id, 
                $this->user_arr['uid']);
        $page_text = $affected_rows > 0 ? '模版保存成功!' : '模版保存失败!';
        $this->jump($url_manage_module, $page_text);
    }
    
    /**
     * 删除房源模板
     * 接受参数 id编号 模板类型
     * @param string $module 模板类型，sell和rent 默认为sell
     */
    public function delete($module = '', $id = '')
    {
        $module = $this->house_template_model->check_module($module);
        if ($module == '' || $id == '') 
        {
            $this->jump($this->house_template_model->
                find_func_url('manage') . $module . '/', '参数不合法', 3000);
            return;
        }
        $status = $this->house_template_model->delete($id, $this->user_arr['uid']);
        $page_text = $status ? '模板删除成功。' : '模板删除失败。';
        $this->jump($this->house_template_model->
                find_func_url('manage') . $module, $page_text);
    }
    
    /**
     * 房源模板管理
     * @param string $module 模板类型，sell和rent 默认为sell
     */
    public function manage($module = '')
    {
        //头部
        $data_page['page_title'] = '租售宝';
        $data_page['css'] = load_css('zsb/css/v1.0/style2.css');
        //页面加载需要的数据
        $module = $this->house_template_model->check_module($module);
        $data_page['module'] = $module;
        $data_page['module_name'] = $this->house_template_model->find_module_name($module);
        //获取房源模板列表数据
        $data_page['array_template'] = $this->house_template_model->find_by_module($module, 
                $this->user_arr['uid']);
        //模板数量
        $data_page['template_num'] = $this->house_template_model->
                find_count_by_module($module, $this->user_arr['uid']);
        //发布出售和出租的房源地址
        if ($module == 'sell')
        {
            $this->load->model('sell_model');
            $data_page['url_add_house'] = $this->sell_model->find_func_url('add');
        } 
        else 
        {
            $this->load->model('rent_model');
            $data_page['url_add_house'] = $this->rent_model->find_func_url('add');
        }
        //添加房源模板地址
        $data_page['url_add'] = $this->house_template_model
                ->find_func_url('add') . $module . '/';
        //修改房源模板地址
        $data_page['url_modify'] = $this->house_template_model
                ->find_func_url('modify') . $module . '/';
        //删除房源模板地址
        $data_page['url_delete'] = $this->house_template_model->find_func_url('delete');   
        //页面标题
        $data_page['title'] = '房源模板管理';  
        //出售房源模板列表地址
        $data_page['url_manage_sell'] = $this->house_template_model->
                find_func_url('manage') . 'sell/';   
        //出租房源模板列表地址
        $data_page['url_manage_rent'] = $this->house_template_model->
                find_func_url('manage') . 'rent/';   
        $data_page['page_title'] = '房源管理-房源模板管理';
        //加载模板
        $this->view('house_template_manage', 'house_template_manage', $data_page);
    }
}
