<?php

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


class Index extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->permission->is_admin();
        $this->load->model('InfoStructures_model');
        $this->load->model('Information_model');
        $this->load->model('InfoContents_model');
        $this->load->model('infoRecommends_model');
        $this->load->model('infoTips_model');
    }

    public function index()
    {
        $data = array();
        $data['groupList'] = $this->Information_model->group_list();
        $data['tipsList'] = $this->infoTips_model->get_list();
        $this->load->view('bootstrap3/header', $data);
        $this->load->view('welcome');
        $this->load->view('bootstrap3/footer');
    }

    public function search()
    {
      
      $keywords = $this->input->post('keywords');
      $byWhat = $this->input->post('byWhat');
      $data = array();
      $data['groupList'] = $this->Information_model->group_list();
      $data['tipsList'] = $this->infoTips_model->search($keywords, $byWhat);
      $data['lastKeyword'] = $keywords;
      $this->load->view('bootstrap3/header', $data);
      $this->load->view('welcome');
      $this->load->view('bootstrap3/footer');
    }

    //节点绑定信息推荐或者广告
    public function bind($is_id)
    {
        $data = array();
        $data['information'] = $this->Information_model->Detail($is_id);
        if ($data['information'] == FALSE) {
            show_404();
            return;
        }
        //如果本身不是分组节点则查询分组信息
        if ($data['information']->is_level > 0) {
            $data['group_detail'] = $this->Information_model->get_detail_by_path($data['information']->is_path, 0);
        }

        //查询所有推荐信息
        $recommends= $this->infoRecommends_model->detail_list($is_id);
        foreach ($recommends as $item){
            $data['recommends'][$item->ir_name]=$item;
        }

        $this->load->view('bootstrap3/header', $data);
        $this->load->view('bind');
        $this->load->view('bootstrap3/footer');
    }

    public function bind_info()
    {
        $data = array();
        $ir_is_id = $this->input->post('ir_is_id');
        $ir_name = $this->input->post('ir_name');
        $data['infoRecommend'] = $this->infoRecommends_model->detail($ir_is_id, $ir_name);
        //广告列表
        $data['tipsList'] = $this->infoTips_model->get_list();
        if (empty($data['infoRecommend'])) {
            $data['infoRecommend'] = new StdClass;
            $data['infoRecommend']->ir_is_id = $ir_is_id;
            $data['infoRecommend']->ir_name = $ir_name;
            $data['infoRecommend']->ir_keyword = '';
            $data['infoRecommend']->ir_pointer_is_id = null;
            $data['infoRecommend']->ir_pointer_it_id = null;
            $data['infoRecommend']->ir_urls = '';
            $data['infoRecommend']->ir_rule = 'rule_parent';
            $data['infoRecommend']->ir_datetime = time();
            $data['infoRecommend']->ir_sitecode = $this->config->item('site_code');
            $data['infoRecommend']->ir_id = $this->infoRecommends_model->add('infoRecommends', $data['infoRecommend']);
        }
        echo json_encode($this->load->view('bind_info', $data, true));
    }

    //根据is_id查询对应的信息，用来检查绑定的is是否正确
    public function check_is_id_info(){
        $ir_pointer_is_id=$this->input->post('ir_pointer_is_id');
        if(!empty($ir_pointer_is_id)) {
            $information = $this->Information_model->Detail($ir_pointer_is_id);
        }
        if(!empty($information)){
            echo json_encode($information->ic_title);
        }else{
            echo json_encode('找不到信息');
        }
    }

    public function bind_info_save()
    {
        $infoRecommend = new StdClass;
        $ir_id = $this->input->post('ir_id');
        //先查一遍这个数据是否存在，不存在则退出，防止被攻击
        if (empty($this->infoRecommends_model->detail_by_ir_id($ir_id))) {
            $data[] = array('name' => 'no', 'value' => '查询不到数据，请重试');
        } else {
            $infoRecommend->ir_rule = $this->input->post('ir_rule');
            $infoRecommend->ir_keyword = trim($this->input->post('ir_keyword'));
            $infoRecommend->ir_pointer_is_id = $this->input->post('ir_pointer_is_id');
            $infoRecommend->ir_pointer_it_id = $this->input->post('ir_pointer_it_id');
            $infoRecommend->ir_urls =trim($this->input->post('ir_urls'));
            $infoRecommend->ir_datetime =time();
            $infoRecommend->ir_id = $this->infoRecommends_model->update('infoRecommends', $infoRecommend, 'ir_id=' . $ir_id);
            $data[] = array('name' => 'ok', 'value' => '保存成功！');
        }
        echo json_encode($data);
    }

}
