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

class Setting extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->permission->is_admin();
        //$this->output->enable_profiler(TRUE);
		$this->load->model('Infoauthors_model');			
    }
	
	public function index()
	{
        $admin_info=$this->session->userdata('session_admin');
        $this->load->model('InfoSMS_model');
        $data['all_unread_sms']=$this->InfoSMS_model->all_unread_sms($admin_info->a_id);

		$data['author']=$this->Infoauthors_model->detail_by_id($admin_info->a_id);
        $data['navbar_active']='setting';
		$data['author_field'] = $this->load->view('author_field',$data,true);
        $this->load->view('header', array('navbar_active'=>'')+$data);
        $this->load->view('profile');
        $this->load->view('footer');
	}
	
	public function profile_submit()
	{
        //$this->form_validation->set_rules('a_email', 'lang:signup_a_email', 'required');
        //$this->form_validation->set_rules('a_name', 'lang:signup_a_name', 'required');
        //$this->form_validation->set_rules('a_name_cn', 'lang:signup_a_name_cn', 'required');
        //$this->form_validation->set_rules('a_id_card', 'lang:signup_a_id_card', 'required');
        //$this->form_validation->set_rules('a_address', 'lang:signup_a_address', 'required');
        //$this->form_validation->set_rules('a_school', 'lang:signup_a_school', 'required');
        //$this->form_validation->set_rules('a_resume', 'lang:signup_a_resume', 'required');
        $this->form_validation->set_rules('a_email', 'lang:signup_a_email', 'required');
        $this->form_validation->set_rules('a_name', 'lang:signup_a_name', 'required');
        $this->form_validation->set_rules('a_bank', 'lang:a_bank', 'required');
        $this->form_validation->set_rules('a_bank_card', 'lang:a_bank_card', 'required');
        //$this->form_validation->set_rules('a_mobile_phone', 'lang:signup_a_mobile_phone', 'required');
        if ($this->form_validation->run() == FALSE)
        {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value)
            {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
            return FALSE;
        }
        else
        {
            //头像处理
            $a_photo=$this->input->post('a_photo');
            if ($this->input->post('profile_url')!='') {
                $profile_file_path=getcwd().'/author/document/profile_photo';
                $profile_url=$this->input->post('profile_url');
                $profile_array=explode('.', $profile_url);
                $profile_ext='jpg';//end($profile_array);
                $new_profile=$profile_file_path.'/profile_'.$this->input->post('a_email').'.'.$profile_ext;
                if(file_exists($new_profile)) unlink($new_profile);
                rename($profile_file_path.'/'.$profile_url,$new_profile);
                rename($profile_file_path.'/'.str_replace('_thumb', '', $profile_url),$profile_file_path.'/profile_'.$this->input->post('a_email').'_org.'.$profile_ext);
                $a_photo='profile_'.$this->input->post('a_email').'.'.$profile_ext;
            }

            //处理上传的身份证复印件
            if ($this->input->post('ID_file_name')!='') {
                $ID_file_path=getcwd().'/author/document/ID_CARD';
                $a_id_file=$this->input->post('ID_file_name');
                $file_array=explode('.', $a_id_file);
                $ext='jpg';//end($file_array);
                $new_file=$ID_file_path.'/id_card_'.$this->input->post('a_email').'.'.$ext;
                if(file_exists($new_file)) unlink($new_file);
                rename($ID_file_path.'/'.$a_id_file,$new_file);
            }

			$admin_info=$this->session->userdata('session_admin');
            $a_name=trim($this->input->post('a_name'));
            $a_name_cn=trim($this->input->post('a_name_cn'));
            $this->Infoauthors_model->update($admin_info->a_id, $this->input->post('a_email'),
				$a_name, $a_name_cn, $a_photo,
				$this->input->post('a_mobile_phone'),$this->input->post('a_phone'), $this->input->post('a_id_card'),
				$this->input->post('a_gender'),$this->input->post('a_address'), $this->input->post('a_school'),
				$this->input->post('a_bank'),$this->input->post('a_bank_card'), $this->input->post('a_resume'));

			$a_password=$this->input->post('a_password');
			if(!empty($a_password))
			{
				$this->Infoauthors_model->set_password($admin_info->a_id,$a_password);
			}
				
			$data[] = array('name' => 'ok', 'value' => $this->lang->line('setting_profile_success'));
			echo json_encode($data);
			return TRUE;
        }	
	}

   
}
