<?php

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

class Translate extends CI_Controller
{
	var $per_page;
    function __construct()
    {
        parent::__construct();
        $this->permission->is_admin();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('Translate_model');
        $this->per_page = 30;
    }

    public function index()
    {
    	$this->load->library('pagination');
    	$recordcount = 0;
    	$page = $this->uri->segment(3);
    	$opiid  =0;
    	$userid =0;

    	if($this->input->post('Submit')){
    		$key = $this->input->post('search');
    	} else {
    		$key = unserialize($this->input->cookie('search'));
    	}
    	$cookie = array(
    			'name' => 'search',
    			'value'  => serialize($key),
    			'expire' => '86500',
    			'path'   => '/'
    	);
    	$searchdata=$key;
    	if($searchdata['tli_userid']=='')unset($searchdata['tli_userid']);
    	if($searchdata['tli_opi_sn']=='')unset($searchdata['tli_opi_sn']);
    	if($searchdata['tli_title']=='')unset($searchdata['tli_title']);
    	if($searchdata['tli_checktime_f']==''|| !$this->is_date($searchdata['tli_checktime_f']) )unset($searchdata['tli_checktime_f']);
    	if($searchdata['tli_checktime_t']==''|| !$this->is_date($searchdata['tli_checktime_t']) )unset($searchdata['tli_checktime_t']);
		
    	$this->input->set_cookie($cookie);
    	$data['translateList'] = $this->Translate_model->getList($searchdata,$opiid,$userid,$searchdata['tli_status'],$recordcount,$page,$this->per_page);
    	$config['base_url'] = site_url("translate/listall/");
    	$config['total_rows'] = $recordcount;
    	$config['per_page'] = $this->per_page;
    	$config['uri_segment'] = 3;
    	$this->pagination->initialize($config);
    	
    	$data['userList'] = $this->Translate_model->getuserList();
    	$data['OpiList']  = $this->Translate_model->getOpiList();
    	$data['search']  = $key;
    	$this->load->view('header',$data);
        $this->load->view('translate');
        $this->load->view('footer');

    }
	
	public function is_date($d){
		// date formate is 01/04/2012
		// or else return false
		if (preg_match ("/^\d{2}\/\d{2}\/\d{4}$/i",$d)) {
			return true;
		} else {
			return false;
		}		
	}
	public function publication($tid=0){
		$this->load->helper('date');
		if($this->input->post('submit')){
			$info = $this->input->post('info');
			$msg = '';$ispass = false;
			/*
			if(strlen($info['tli_content'])<10){
				$msg = '<div>翻译内容不能为空.</div>';
				$ispass = true;
			}
			*/
			if(strlen($info['tli_title'])<1){
				$msg .= '<div>任务标题不能为空.</div>';
				$ispass = true;
			}
			/*
			if(!is_numeric($info['tli_userid'])){
				$msg .= '<div>必须指定翻译人员.</div>';
				$ispass = true;
			}
			*/
			if(!preg_match("/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/",$info['tli_deadline'])){
				$msg .= '<div>有效期日期格式不正确.</div>';
				$ispass = true;
			}
			
			if(strtotime($info['tli_deadline']) < time()){
				$info['tli_deadline'] = time()+3600*24*10;
			}
			
			if($ispass){
				$data[] = array('name'=>'ok','value'=>$msg);
				echo json_encode($data);
				exit();
			}
			if($tid>0){//更新任务新信息
				$result = $this->Translate_model->updatetranslat($info,$tid);
			} else {//新增新任务
				$result = $this->Translate_model->addTranslate($info);
			}
			if($result){
				$data[] = array('name'=>'go','value'=>site_url('translate/listall'));
			} else {
				$data[] = array('name'=>'no','value'=>'系统错误请重试!');
			}
			echo json_encode($data);
			exit();
		}
		
		
		
		
		$data['userList'] = $this->Translate_model->getuserList(); 
        $this->load->view('header',$data);
        $this->load->view('translate_publish');
        $this->load->view('footer');
	}
	//全部列表同部调用function index
	public function listall(){
		$this->index();
	}
	//修改任务表单
	public function editiem($tid){
		$data['userList'] = $this->Translate_model->getuserList();
		$data['translateInfo'] = $this->Translate_model->getTranslateInfo($tid);
		$this->load->view('header',$data);
		$this->load->view('translate_edit');
		$this->load->view('footer');
	}
	/*删除任务*/
	public function deletetranslate($tid){
		$this->Translate_model->deletetranslation($tid);
		redirect(site_url('translate/listall'));
	}
	/*操作表单*/
	//操作表单可用字段有:
	//  管理备注
	//  信息应用地址,
	//  翻译总字数
	//  已经付款总数
	//  状态
	public function opater($tid,$action=''){
		if(!in_array($action,array('check','pay','public')))
		{
			exit();
		}
		
		$data['translateinfo']=$this->Translate_model->getTranslateInfo($tid);
		switch ($action){
			case "check":
				break;
			default:
				
		}
		if($this->input->post('action') == 'check'){
			$this->opatercheck($tid);
		}else if($this->input->post('action') == 'pay'){
			if($data['translateinfo']['tli_status'] < 4){
				$data[]=array('name'=>'no','value'=>'审核未通过不能结算任务!');
				exit(json_encode($data));
			}
			$this->opaterpay($tid);
		}else if($this->input->post('action') == 'public'){
			$this->opaterpublic($tid);
		}

		
		$data['action']=$action;
		$data['tid']=$tid;
		$this->load->view('header',$data);
		$this->load->view('opater');
		$this->load->view('footer');
	}
	
	private function opatercheck($tid){
		$r = $this->input->post('opater');
		$this->form_validation->set_rules('opater[tli_charcount]', 'tli_charcount', 'required|numeric');
		if ($this->form_validation->run() == FALSE){
			$data[]=array('name'=>'no','value'=>'请输入翻译总字数,必须为数字');
		} else {
			
			$r['tli_status']    = ($r['tli_status']==1)?4:3;
			$r['tli_checktime'] = date('Y-m-d H:i:s',time());//审核时间

			if($this->Translate_model->updatetranslat($r,$tid)==false){
				$data[]=array('name'=>'no','value'=>'审核操作失败,请重试');
			}else{
				$data[]=array('name'=>'ok','value'=>'操作成功执行');
			}
		}
		echo json_encode($data);
		exit();
	}
	private function opaterpay($tid){
		$r = $this->input->post('opater');
		$this->form_validation->set_rules('opater[tli_charge]', 'tli_charge', 'required|numeric');
		$this->form_validation->set_rules('opater[tli_chargeunit]', 'tli_chargeunit', 'required');
		if ($this->form_validation->run() == FALSE){
			$data[]=array('name'=>'no','value'=>'支付金额必须为数字,单位币种不能为空');
		} else {
			$r['tli_status'] = 5;
			if($this->Translate_model->updatetranslat($r,$tid)==false){
				$data[]=array('name'=>'no','value'=>'审核操作失败,请重试');
			} else {
				$data[]=array('name'=>'ok','value'=>'操作成功执行');
			}
		}
		echo json_encode($data);
		exit();	
	}
	private function opaterpublic($tid){
		$r = $this->input->post('opater');
		$r['tli_status'] = 6;
		if($this->Translate_model->updatetranslat($r,$tid)==false){
			$data[]=array('name'=>'no','value'=>'发布操作失败,请重试');
		}else{
			$data[]=array('name'=>'ok','value'=>'操作成功执行');
		}
		exit(json_encode($data));
	}
	//操作表单结束//
	
	/*以上部分为翻译内容*/
	/*以下部分为用户管理部分*/
	//用户管理部分以小写u开头
	public function listuser(){
		$this->load->library('pagination');
		$page = $this->uri->segment(3);
		$recordcount = 0;
		
		$data['translateUserList'] = $this->Translate_model->uList($recordcount,$page,$this->per_page);
		$config['base_url'] = site_url("translate/listuser/");
		$config['total_rows'] = $recordcount;
		$config['per_page'] = $this->per_page;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		
		$this->load->view('header',$data);
		$this->load->view('translate_userlist');
		$this->load->view('footer');
	}
	/*编辑*/
	public function uedit($uid){
		$msg='';
		if($this->input->post('Submit') && $uid>0){
			$userinfo= $this->input->post('userinfo1');
			$passwordInfo = $this->input->post('userinfo');
			$msg ='用户信息更新成功.';
			if($passwordInfo['tlui_password']!='' ){
				$newpassword = $this->Translate_model->randomkeys(6);
				$userinfo['tlui_password']=md5($newpassword);
				$msg .="您初始化了密码新密码为：<strong>$newpassword</strong>";
			}
			$this->Translate_model->update_user($userinfo,$uid);
		}
		
		$data = $this->Translate_model->getUserInfo($uid);
		$data->msg=$msg;
		$this->load->view('header',$data);
		$this->load->view('translate_userform');
		$this->load->view('footer');
	}
	/*删除*/
	public function udelete($uid){
		$this->Translate_model->deleteuser($uid);
		redirect(site_url('translate/listuser'));
	}
	
	/*公告*/
	public function announc($page=0){
		$data['action'] = 'list';
		$page = $this->uri->segment(2);
		$data['announcList'] = $this->Translate_model->getAnnouncList($this->per_page,$page);
		$this->load->view('header',$data);
		$this->load->view('announc');
		$this->load->view('footer');
	}
	public function announcadd(){
		$data['action'] = 'add';
		//$data['announc'] = $this->Translate_model->getAnnounc($id);
		$this->load->view('header',$data);
		$this->load->view('announc');
		$this->load->view('footer');
	}
	public function editannounc($id=''){
		$data['action'] = 'edit';
		if($this->input->post('submit') == 'true'){
			//保存
			$msg['tlm_title'] = $this->input->post('tlm_title');
			$msg['tlm_content'] = $this->input->post('tlm_content');
			$msg['tlm_id'] = $this->input->post('tlm_id');
			//unset($msg['tlm_id']);
			if($msg['tlm_id']==""){//新建
				unset($msg['tlm_id']);
				$msg['tlm_dateline'] = date('Y-m-d H:i:s',time());
				$this->Translate_model->inertAnnounc($msg);
				$data[]=array('name'=>'go','value'=>site_url('translate/announc'));
			} else { //修改
				unset($msg['tlm_id']);
				$this->Translate_model->updateAnnounc($msg,$id);
				$data[]=array('name'=>'go','value'=>site_url('translate/announc'));
			}
			exit(json_encode($data));
		}
		
		$data['announc'] = $this->Translate_model->getAnnounc($id);
		$this->load->view('header',$data);
		$this->load->view('announc');
		$this->load->view('footer');
	}
	
	public function deletennounc($id=''){
		$this->Translate_model->deleteAnnounc($id);
		redirect(site_url('translate/announc'));
	}
	
	//修改后的函数
	//检测是否存在这样一个翻译信息
	public function checktranslate($ic_id){
		$data['result'] = $this->Translate_model->checktranslate($ic_id);
		echo json_encode($data);
	}
	public function gettranslateInfo($ic_id){
	
		if(!$this->Translate_model->checktranslate($ic_id)){
			$tmpinfo['tli_content']=$this->input->post("ic_content");
			$tmpinfo['tli_tagetlgc'] = "";
			$tmpinfo['tli_otherquest'] = "";
			$tmpinfo['tli_title'] = "";
			$tmpinfo['tli_userid'] = 0;
			$tmpinfo['tli_ic_id'] = $ic_id;
			$this->Translate_model->addTranslate($tmpinfo);
		}
	
		$data['translateInfo'] = $this->Translate_model->getTranslateInfo(0,$ic_id);
		$data['userList'] = $this->Translate_model->getuserList();
		$data['cp'] = "checktranslate";
		$this->load->view('translate_form',$data);
	}
	
	public function updateTranslate(){
	
		$this->load->helper('date');
		$info = $this->input->post('info');
		$msg = '';$ispass = false;
		/*
		if(strlen($info['tli_content'])<10){
			$msg = '<div>翻译内容不能为空.</div>';
			$ispass = true;
		}
		*/
		
		if(strlen($info['tli_title'])<1){
			$msg .= '<div>任务标题不能为空.</div>';
			$ispass = true;
		}/*
		if(!is_numeric($info['tli_userid'])){
			$msg .= '<div>必须指定翻译人员.</div>';
			$ispass = true;
		}
		*/
		if(!preg_match("/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/",$info['tli_deadline'])){
			$msg .= '<div>有效期日期格式不正确.</div>';
			$ispass = true;
		}
		if(!preg_match("/^[0-9]+$/",$info['tli_id'])){
			$msg .= '<div>系统错误，错误代码 #1.</div>';
			$ispass = true;
		}
		if(!preg_match("/^[0-9]+$/",$info['tli_ic_id'])){
			$msg .= '<div>系统错误，错误代码 #2.</div>';
			$ispass = true;
		}
		
		if($ispass){
			$data[] = array('name'=>'no','value'=>$msg);
			echo json_encode($data);
			exit();
		}
		$tid = $info['tli_id'];
		unset($info['tli_charcount']);
		unset($info['tli_charge']);
		unset($info['tli_id']);
		unset($info['tli_info']);
		
		$result = $this->Translate_model->updatetranslat($info,$tid);
		if($result){
			$data[] = array('name'=>'ok','value'=>'翻译信息内容更新成功');
		} else {
			$data[] = array('name'=>'no','value'=>'系统错误请重试!');
		}
		echo json_encode($data);
		exit();
	}
	
	public function opaterajax($tid,$action=''){
		$data['cp'] = $action;
		$data['translateInfo'] = $this->Translate_model->getTranslateInfo($tid, 0);
		$str = $this->load->view('translate_form',$data,true);
		$data[] = array('name'=>'ok','value'=>$str);
		echo json_encode($data);
	}
	
	public function viewcon($tid){
		$data['cp'] = 'viewcon';
		$data['translateInfo'] = $this->Translate_model->getTranslateInfo($tid, 0);
		$str = $this->load->view('translate_form',$data);
		
	}
	
}