<?php

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

class Welcome extends CI_Controller {

    function __construct() {
        parent::__construct();
        //todo:判断用户是否登录
        $this->permission->is_admin();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('Groupinfo_model');
        $this->load->model('User_model');
    }

    public function index() {
        echo 'ok';
    }

    public function login() {
        $username = $this->input->get('security_day');
        $password = $this->input->get('security_code');
        if (!empty($username) && !empty($password)) {
            $user_info_wu_id = $this->User_model->check_login($username, $password);
            if (!empty($user_info_wu_id)) {
                $user_info = $this->User_model->get_user_info($user_info_wu_id);
                $this->session->set_userdata('session_admin', $user_info);
                echo 'ok';
                return TRUE;
            }
        }
        echo 'false';
        return FALSE;
    }

    public function feedback_detail($gri_sn, $eoi_sn,$mei_sn = false) {
        $data = array();
        $data['gri_sn'] = $gri_sn;
        $data['eoi_sn'] = $eoi_sn;
        $vei_sn = $this->permission->admin()->LMI_VEI_SN;
        $data['group_info'] = $this->Groupinfo_model->get_group_info($gri_sn);//执行两次，第一次可能因为数据没有建立
        $data['group_info'] = $this->Groupinfo_model->get_group_info($gri_sn);
		
		// 判断多家庭
		$many_family = $this->Groupinfo_model->get_many_family($gri_sn);
		$data['many_family'] = $many_family;
		if($mei_sn != false){
			$esi_mei_sn = $mei_sn;	
		}else{
			$esi_mei_sn = $many_family ? $many_family[0]->ESI_MEI_SN : 0;	
		}
		$data['esi_mei_sn']=$esi_mei_sn;
        $data['object_info'] = $this->Groupinfo_model->get_eva_objectinfo(1,$eoi_sn);
		
        //当前城市导游SN
        $data['guide_sn'] = $this->User_model->get_guide_sn($gri_sn, $vei_sn, $data['object_info']->EOI_CII_SN);
		
        //地接社导游列表
        $data['guide_list'] = $this->User_model->get_guide_list($vei_sn);
		
		$data['service_item_info'] = $this->Groupinfo_model->get_service_item_info($eoi_sn, $gri_sn, $vei_sn,$esi_mei_sn);	
		
		//表示反馈表没有生成项目，需要运行存储过程生成
		/***/
		if (empty($data['service_item_info'])) {
			$this->Groupinfo_model->create_service_item_info($eoi_sn, $gri_sn, $vei_sn);
			$data['service_item_info'] = $this->Groupinfo_model->get_service_item_info($eoi_sn, $gri_sn, $vei_sn,$esi_mei_sn);
		}
		
		//print_r($data['service_item_info']);
        
        //问题编号和新产品名称
        $data['group_service'] = $this->Groupinfo_model->get_group_service($eoi_sn, $vei_sn);
        $data['group_service'] = $this->Groupinfo_model->get_group_service($eoi_sn, $vei_sn);//执行两次，第一次可能因为数据没有建立
        $data['service_question_four'] = $this->Groupinfo_model->get_eva_feedback($gri_sn, $vei_sn, $eoi_sn, $data['group_service']->QSFour);
        $data['service_question_five'] = $this->Groupinfo_model->get_eva_feedback($gri_sn, $vei_sn, $eoi_sn, $data['group_service']->QSFive);
        //问题6
        $data['other_service_question'] = $this->Groupinfo_model->get_eva_feedback($gri_sn, $vei_sn, $eoi_sn, 223012);

        //判断是否已经由小助手那边提交了反馈数据
        $data['CheckXzsSubmit'] = $this->Groupinfo_model->CheckXzsSubmit($gri_sn,$vei_sn);

        $this->load->view('header', $data);
        $this->load->view('feedback_detail');
        $this->load->view('footer');
    }

    public function feedback_detail_submit() {
        $data = array();
        $gri_sn = $this->input->post('gri_sn');
        $eoi_sn = $this->input->post('eoi_sn');
        $vei_sn = $this->permission->admin()->LMI_VEI_SN;

        foreach ($_POST as $key => $item) {
            if (substr($key, 0, 7) == 'esi_sn_') {
                $esi_sn_temp = str_replace('esi_sn_', '', $key);
                $this->Groupinfo_model->update_esi_rate($esi_sn_temp, $item);
            }
        }

        //保存导游信息 这个代码是错误的，不能根据eoi_sn直接修改导游信息，而是需要加上$gri_sn,$vei_sn,$data['object_info']->EOI_CII_S 等，参考User_model->get_guide_sn
        //$guide_info_sn = $this->input->post('guide_info_sn');
        // $this->Groupinfo_model->update_guide($eoi_sn, $guide_info_sn);
        //问题4
        $q4_eef_type = $this->input->post('q4_eef_type');
        $q4_eef_content = $this->input->post('q4_eef_content');
        $this->Groupinfo_model->update_eva_answer($gri_sn, $eoi_sn, $vei_sn, $q4_eef_type, $q4_eef_content, '');
        //问题5
        $q5_eef_type = $this->input->post('q5_eef_type');
        $q5_eef_content_radio = $this->input->post('q5_eef_content_radio');
        $q5_eef_content = $this->input->post('q5_eef_content');
        $this->Groupinfo_model->update_eva_answer($gri_sn, $eoi_sn, $vei_sn, $q5_eef_type, $q5_eef_content, $q5_eef_content_radio);
        //问题6
        $q6_eef_type = $this->input->post('q6_eef_type');
        $q6_eef_content = $this->input->post('q6_eef_content');
        $this->Groupinfo_model->update_eva_answer($gri_sn, $eoi_sn, $vei_sn, $q6_eef_type, $q6_eef_content, '');

        //生成各项平均分
        $this->Groupinfo_model->sp_vendorquality_setmark($gri_sn, $vei_sn, $eoi_sn);

        //申请审核标记
        $eoi_vrequestverify = $this->input->post('eoi_vrequestverify');
        if (!empty($eoi_vrequestverify)) {
            $this->Groupinfo_model->update_feedback_verify($eoi_sn, $eoi_vrequestverify, $this->permission->admin()->LMI_SN);
        }


        $data[] = array('name' => 'ok', 'value' => '保存成功！');
        echo json_encode($data);
    }

}

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