<?php

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


class Index extends CI_Controller
{
    public $textline=1;
    public $textline2=1;

    public function __construct() 
    {
        parent::__construct();
        $this->permission->is_admin(false);
        $this->load->model('workflow_model');
    }
	
	public function test(){
		$this->workflow_model->delete_test();
	}
	
    //首页
    public function index() 
    {
        if ($this->session->userdata('admin_chtcdn') === FALSE) {
            $this->permission->is_admin();
        }
        $data=array();
        $typelist=$this->workflow_model->get_type_list('all');
		//print_r($typelist);
        $data['typelist']=array();
        foreach ($typelist as $t) {
            $data['typelist'][$t->wt_parentid][]=$t;
        }
        if (empty($data['typelist'])) {
            $data['typelist'][0]=array();
        }
        $data['left_nav']='index';
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/index');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

    //添加具体事务页面
    public function add_work($wt_sn) 
    {
        $this->permission->is_admin();
        $data=array();
        //当前类型信息
        $data['typelist']=$this->workflow_model->get_type_list('all',$wt_sn);
        //提取使用的表单
        $form=$data['typelist'][0]->wt_form;
        $data['current_wt']=$wt_sn;
        $data['add_work']=1;
        $data['left_nav']='index';
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view("form/$form");
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

    //添加一个具体事务到数据库
    public function do_add_work()
    {	
		
        $this->load->model('operator_model');
        $current_user=$this->input->post('wf_user');
        $tmp = $this->operator_model->get_user_by_name($current_user);

        //判断是不是补填的值班记录
        if ($this->input->post('mail_title')=='值班记录') {
            //本月和上个月的值班表(有可能本月初和上月尾是连着的假期)
            $currentmonth=date('Y-m-01 00:00:00',time());
            $datetime = strtotime("$currentmonth -1 month"); 
            $data['list']=$this->workflow_model->get_rata_list($datetime,2,'DESC');
            foreach ($data['list'] as $v) {
                $data['flow_data'][]=$this->workflow_model->get_work_flow_data($v->wf_sn);
            }
            //节假日列表
            $needdate_list=$myrota=array();
            foreach ($data['flow_data'] as $f) {
                foreach ($f as $d) {
                    $wfd_key_arr=explode('_', $d->wfd_key);
                    //值班日期是周末的日期集合
                    if ($wfd_key_arr[0]=='weekend' && $d->wfd_value==1) {
                        $wfd_key_arr[1]=strlen($wfd_key_arr[1])==7?substr($wfd_key_arr[1], 0,-1).'0'.substr($wfd_key_arr[1],-1):$wfd_key_arr[1];
                        $needdate_list[]=$wfd_key_arr[1];
                    }
                    //当前用户的值班日期集合
                    $name_string=str_replace('惠毅', '惠毅;惠 毅', $d->wfd_value);
                    if (!empty($d->wfd_value) && stripos($name_string, $tmp['OPI_Name'])!==false && isset($wfd_key_arr[2]) && is_numeric($wfd_key_arr[2])) {
                        $need_date=strlen($wfd_key_arr[2])==7?substr($wfd_key_arr[2], 0,-1).'0'.substr($wfd_key_arr[2],-1):$wfd_key_arr[2];
                        $myrota[]=date('Ymd',strtotime($need_date));
                    }
                }
            }
            $needdate_list=array_intersect($needdate_list,$myrota);
            $zhiban_date=date('Ymd',strtotime($this->input->post('wf_time')));
            if (empty($needdate_list) or !in_array($zhiban_date, $needdate_list)) {
                //echo json_encode(array('status'=>'no','msg'=>'值班日期与值班表不符，更换了值班的同学请更改值班表'));
                //return true;
            }
        }

        //保存任务
        $wf_user=$tmp['OPI_SN'];
        $wf_time=strtotime($this->input->post('wf_time'));
        $group_admin=explode('@', $this->input->post('wf_wt_sn'));
        $wf_wt_sn=$group_admin[0];
        $wfd_wf_sn=$this->workflow_model->add_work($wf_wt_sn,$wf_user,$wf_time);

        //保存键值对
        $col_val='';
        $mail_data=array();
        $mail_data['wf_time']=$wf_time;
        foreach ($this->input->post() as $wfd_key => $wfd_value) {
            if ($wfd_key=='emailcontent') {
                $wfd_value=htmlspecialchars($wfd_value);
            }
            // $col_val.="('".trim($wfd_key)."','".$wfd_value."',".$wfd_wf_sn."),";
            $col_val.="('".trim($wfd_key)."','".chop($wfd_value)."',".$wfd_wf_sn."),";//csk 2016-9-22 去掉末尾的换行和空格
            $mail_data[$wfd_key]=$wfd_value;
            $mail_data['flow_data'][$wfd_key]=$wfd_value;
            $mail_data['flow_data'][$wfd_key]=chop($wfd_value);//csk 2016-9-22 去掉末尾的换行和空格            
        }

        $apply_user=$this->input->post('apply_user');
        if (empty($apply_user)) {
            $col_val.="('apply_user','".$current_user."',".$wfd_wf_sn."),";
        }

        $col_val=substr($col_val, 0, -1);
        $wfd_sn=$this->workflow_model->add_work_data($col_val);

        //添加审核人
        $typeinfo=$this->workflow_model->get_type_list(0,$wf_wt_sn);
        $verify_tolist_array=$this->get_verify_admin_list($wfd_wf_sn,$group_admin[1],$typeinfo[0]->wt_linear);
        //抄送邮件给指定人员
        if ($wfd_wf_sn && $wfd_sn) {
            $data['userdata']=$this->session->userdata('admin_chtcdn');
            
            //发送邮件给审核人员
            $this->load->model('common_model');
            if ($typeinfo[0]->wt_needmail==1) {
                $verify_from_email=$data['userdata']['whu_email'];
                $verify_subject=$this->input->post('mail_title');
                $verify_mailbody='<p>我刚刚提交了一份'.$verify_subject.'，请您审核一下下：</p><p><a href="http://www.mycht.cn/webht.php/apps/workflow/index/verify/'.$wfd_wf_sn.'/0/verify">http://www.mycht.cn/webht.php/apps/workflow/index/verify/'.$wfd_wf_sn.'/0/verify</a></p>';
                $verify_mailbody.='<br><br><p>非常感谢！</p>';
                $verify_mailbody.='<br><p style="color:#888;">本次提醒邮件来自中华游在线平台</p>';
                $this->common_model->sendmail($verify_from_email,$verify_tolist_array,array(),$verify_subject,$verify_mailbody);
            }

            //抄送邮件给指定人员
            $cc_mail_list=$this->input->post('cc_mail_list');
            if (!empty($cc_mail_list)) {
                $typeinfo[0]->wt_msg_user=$cc_mail_list.';'.$typeinfo[0]->wt_msg_user;
            }
            $tolist_array=explode(';', $typeinfo[0]->wt_msg_user);
            if(!empty($tolist_array)){
                $mail_tpl=$typeinfo[0]->wt_form;
                $subject=$current_user.'-'.date('Y-m-d',$wf_time).'-'.$this->input->post('mail_title');
                $mailbody=$subject.'<br><br>'.$this->load->view("mail_tpl/$mail_tpl",$mail_data,true);
                $from_email=$data['userdata']['whu_email'];

                //外部邮箱
                foreach ($tolist_array as $key => $m) {
                    if (strpos($m, 'citsguilin.com')===false) {
                        $this->common_model->save_mail($tmp['OPI_Name'],$from_email,$m,$m,$subject,$mailbody);
                        unset($tolist_array[$key]);
                    }
                }
                //内部邮箱
                if(!empty($tolist_array)){
                    $this->common_model->sendmail($from_email,$tolist_array,array(),$subject,$mailbody);
                }
            }
            echo json_encode(array('status'=>'ok','msg'=>'添加成功!','action'=>'edit'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'添加失败!'));
        }
    }

    //事务具体内容界面
    public function verify($wf_sn,$wv_status=0,$nav_type='userlist')
    {
        if(isset($_SERVER['HTTP_REFERER'])){
            $this->session->set_userdata('verify_referer', $_SERVER['HTTP_REFERER']);
        }

        $data['userdata']=$this->session->userdata('admin_chtcdn');
		//print_r($data['userdata']);
        //事务申请人信息
        $userdata=$this->session->userdata('admin_chtcdn'); 
        $flow=$this->workflow_model->get_work_list($userdata['OPI_SN'],$wv_status,$wf_sn);
        $data['flow']=$flow[0];
        
        //事务具体内容
        $flow_data=$this->workflow_model->get_work_flow_data($wf_sn);
        $data['flow_data']=array();
        foreach ($flow_data as $d) {
            $data[$d->wfd_key]=$d->wfd_value;
            $data['flow_data'][$d->wfd_key]=$d->wfd_value;
        }
		//print_r($data);
        $form=$data['flow']->wt_form;
        $data['wf_sn']=$wf_sn;
        
        //事务类型
        $data['typelist']=$this->workflow_model->get_type_list($data['flow']->wt_parentid,false,'all');
        
        //以往步骤的审核信息
        $data['verify_data']=$this->workflow_model->get_verify_by_filed($wf_sn,'wv_comment');

        //当前用户是否审核人员 0否  1是
        $verify_user=$this->workflow_model->get_verify($wf_sn,0,false,50);
        $data['is_verify_user']=0;
        // var_dump($data['userdata']);die;
        if(!empty($data['userdata']['whu_uid'])){//csk2016-11-29
           foreach ($verify_user as $vu) {
            if ($data['userdata']['whu_uid']==$vu->wv_admin) {
                $data['is_verify_user']=1;
                break;
            }
           } 
        }
        
        //用于做导航激活状态
        $data['nav_type']=$nav_type;
        if ($nav_type=='userlist') {
            $data['left_nav']='userlist';
        }elseif ($nav_type=='rata_list') {
            $data['left_nav']='rata_list';
        }elseif ($nav_type=='verifyed') {
            $data['left_nav']='verify';
        }elseif ($wv_status==0) {
            $data['left_nav']='unverify';
        }else{
            $data['left_nav']='verify';
        }
        $data['wv_status']=$wv_status;
        $data['form']=$this->load->view("form/$form",$data,true);
		//print_r($data);
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        //$this->load->view('w-left');
        $this->load->view('index/verify');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

    //审核操作
    public function do_verify($wv_status=0)
    {
        $userdata=$this->session->userdata('admin_chtcdn'); 
        $wv_wf_sn=$this->input->post('wv_wf_sn');
        $wv_comment=$this->input->post('wv_comment');

        //判断该任务是否已审核
        $workdata=$this->workflow_model->get_work_detail($wv_wf_sn);
        if ($workdata[0]->wf_status==1) {
            echo json_encode(array('status'=>'no','msg'=>'该任务已经审核过了!'));
            return false;
        }

        $all_step=$this->workflow_model->get_verify($wv_wf_sn,false,false,50);
        $current_actived=0;
        foreach ($all_step as $s) {
            //当前激活的审核次序
            if ($s->wv_actived!=-1) {
                $current_actived=$s->wv_actived;
            }
            //下一个激活的审核次序
            else
            {
                if (!isset($next_actived)) {
                    $next_actived=$s->wv_sort;
                }
            }
            //用于提取下一个审核次序里的审核者
            $next_admin_list_tmp[$s->wv_sort][]=$s;
        }

        //把当前审核者状态设置为已审核
        $result=$this->workflow_model->update_wv($wv_wf_sn,$wv_comment,1,$userdata['whu_uid'],$current_actived);
        if (!$result) {
            echo json_encode(array('status'=>'no','msg'=>'操作失败!'));
            return false;
        }

        //同一职务级别还有人没有审核完成
        $current_step_flag=true;
        foreach ($next_admin_list_tmp[$current_actived] as $v) {
            if ($v->wv_admin!=$userdata['whu_uid'] && $v->wv_status==0) {
                $current_step_flag=false;
                echo json_encode(array('status'=>'ok_notips','msg'=>'操作成功!'));
                break;
            }
        }
        if(!$current_step_flag) return false;
        
        //审核不通过，事务结束
        if ($wv_status==1) 
        {
            $this->workflow_model->update_wv($wv_wf_sn,$wv_comment,1,false);
            $result=$this->workflow_model->update_wf($wv_wf_sn,array('wf_status'=>1));
        }
        //审核结束，录入考勤系统,事务结束
        elseif (!isset($next_actived)) 
        {
            $result=$this->workflow_model->update_wf($wv_wf_sn,array('wf_status'=>1));
            //如果需要考勤、、、
            if ($workdata[0]->wt_isneedkaoqin==1) 
            {
                $this->load->model('Kaoqin_model');
                //wt_isrest为1则表示是补休
                if ($workdata[0]->wt_isrest==1) {
                    $atd_sn_arr=$this->Kaoqin_model->get_atd_type('BUX');
                }
                //wt_isrest为0则表示是加班
                else{
                    $atd_sn_arr=$this->Kaoqin_model->get_atd_type('JAB');
                }
                //提取参加申请的需要考勤的人员名单
                $apply_user_list=$this->workflow_model->get_wfd_by_wfd_key('apply_user',$wv_wf_sn);
                if (empty($apply_user_list)) {
                    $namestrings=$workdata[0]->OPI_Name;
                }else{
                    $namestrings=$apply_user_list[0]->wfd_value;
                }
                $namestrings=str_replace(' ', ';', $namestrings);
                $namestrings=str_replace('；', ';', $namestrings);
                $namestrings=str_replace('（', '(', $namestrings);
                $namestrings=str_replace('）', ');', $namestrings);
                //$namestrings=str_replace('惠毅', '惠 毅', $namestrings);
                $namestrings=str_replace("\n", ";", $namestrings);
                $namestrings=str_replace("\r", ";", $namestrings);
                $namestrings_arr=explode(';', $namestrings);
                $namestrings_array=(array_filter(array_flip(array_flip($namestrings_arr))));
                $rest_start_days=$this->workflow_model->get_wfd_by_wfd_key('rest_start_date',$wv_wf_sn);
                $rest_end_days=$this->workflow_model->get_wfd_by_wfd_key('rest_end_date',$wv_wf_sn);
                foreach ($namestrings_array as $name) 
                {
                    $epy_sn_arr=$this->Kaoqin_model->get_epy_by_name($name);
                    if (empty($epy_sn_arr)) {
                        continue;
                    }
                    $ATD_StartDate=date('Y-m-d 00:00:00',$workdata[0]->wf_time);
                    if (!empty($rest_start_days)) {
                       $ATD_StartDate=date('Y-m-d 00:00:00',strtotime($rest_start_days[0]->wfd_value));
                    }
                    $ATD_EndDate=date('Y-m-d 00:00:00',$workdata[0]->wf_time);
                    if (!empty($rest_end_days)) {
                        $ATD_EndDate=date('Y-m-d 00:00:00',strtotime($rest_end_days[0]->wfd_value));
                    }
                    if (isset($atd_sn_arr[0]->ATT_SN) && isset($epy_sn_arr[0]->EPY_ID)) {
                        $jabtime=7;
                        //如果有自定义的考勤时间则使用自定义的数据
                        $kaoqin_time=$this->input->post('kaoqin_time')?$this->input->post('kaoqin_time'):'';
                        $kaoqin_time=trim($kaoqin_time);
                        if (!empty($kaoqin_time)) {
                            $jabtime=trim($kaoqin_time);
                        }
                        if ($jabtime!=0) {
                            //添加考勤数据
                            $this->Kaoqin_model->add_atd($epy_sn_arr[0]->EPY_ID,$atd_sn_arr[0]->ATT_SN,$jabtime,'小时',$ATD_StartDate,$ATD_EndDate);
                        }
                    }
                }
            }
        }
        //转交下一步
        else
        {
            //把下一个职务标注为激活
            $this->workflow_model->active_verify($next_actived,$wv_wf_sn,$next_actived);
            
            //发送邮件给审核人员
            $workdata=$this->workflow_model->get_work_detail($wv_wf_sn);
            if ($workdata[0]->wt_needmail==1) {
                $next_admin_str='';
                foreach ($next_admin_list_tmp[$next_actived] as $next_admin) {
                    $next_admin_str.=$next_admin->wv_admin.',';
                }
                $next_admin_str=trim($next_admin_str,',');
                $next_admin_array=$this->workflow_model->get_user_list($next_admin_str);
                $verify_tolist_array=array();
                foreach ($next_admin_array as $key => $na) {
                    $verify_tolist_array[]=$na->whu_email;
                }
                $verify_from_email=$userdata['whu_email'];
                $verify_subject=$this->input->post('mail_title');
                $verify_mailbody='<p>我刚刚通过了这份'.$verify_subject.'，请您审核一下下：</p><p><a href="http://www.mycht.cn/webht.php/apps/workflow/index/verify/'.$wv_wf_sn.'/0/verify">http://www.mycht.cn/webht.php/apps/workflow/index/verify/'.$wv_wf_sn.'/0/verify</a></p>';
                $verify_mailbody.='<br><br><p>非常感谢！</p>';
                $verify_mailbody.='<br><p style="color:#888;">本次提醒邮件来自中华游在线平台</p>';
                $this->load->model('common_model');
                $this->common_model->sendmail($verify_from_email,$verify_tolist_array,array(),$verify_subject,$verify_mailbody);
            }

        }
        echo json_encode(array('status'=>'ok_notips','msg'=>'操作成功!'));
    }

    public function get_verify_admin_list($wfd_wf_sn,$wv_admins,$wt_linear=0)
    {

        //取得申请人列表
        $apply_user=$this->input->post('apply_user')?$this->input->post('apply_user'):$this->input->post('wf_user');
        $apply_user=str_replace(' ', ';', $apply_user);
        $apply_user=str_replace('；', ';', $apply_user);
        $apply_user=str_replace('（', '(', $apply_user);
        $apply_user=str_replace('）', ');', $apply_user);
        $apply_user=str_replace('惠毅', '惠 毅', $apply_user);
        $apply_user=str_replace("\n", ";", $apply_user);
        $apply_user=str_replace("\r", ";", $apply_user);
        $name_arr=explode(';', $apply_user);
        $uname_array=(array_filter(array_flip(array_flip($name_arr))));
        $user_list=$this->workflow_model->get_department_by_name($uname_array);

        //取得N级线性上司职务信息
        $admin_str='';
        if ($wt_linear!=0) {
            $admin_roles=array();
            foreach ($user_list as $u) {
                if (empty($u->role)) {
                    $admin_roles[]=$this->workflow_model->get_parent_admin('',$wt_linear);
                }else{
                    $role=$this->workflow_model->get_user_top_role($u->role);
                    $admin_roles[]=$this->workflow_model->get_parent_admin($role->is_level,$wt_linear);
                }
            }
            $newadmin_roles=array();
            foreach ($admin_roles as $admins) {
                foreach ($admins as $admin) {
                    $newadmin_roles[$admin->is_level]=$admin->wt_sn;
                }
            }
            krsort($newadmin_roles);
            $admin_str=implode(';',$newadmin_roles);
        }
        $wv_admins=$admin_str.$wv_admins;
        $wv_admins=trim($wv_admins,';');
        $wv_admins=explode(';', $wv_admins);
        $verify_tolist_array=array();
        foreach ($wv_admins as $level=>$wt_sn) {
           $role_type=$this->workflow_model->get_type_list(0,$wt_sn,0,2);
            //如果是事业部分组的负责人，则取出对应分组的负责人
            if ($role_type[0]->wt_form==0) 
            {
                $department_str='';
                foreach ($user_list as $u) {
                    $u->department=$u->department==36?15:$u->department;//是产品组的话，算到德语事业部里面去
                    $department_str.=$u->department.',';
                }
                $department_str.='0';

                $isid_list=$this->workflow_model->get_department_parent($department_str);
                $wt_parentids='';
                foreach ($isid_list as $key => $is) {
                    $wt_parentids.=$is->is_path;
                }
                $wt_parentids=rtrim($wt_parentids,',');
                $admin_user_list=$this->workflow_model->get_user_by_wtparentid($role_type[0]->wt_sn,$wt_parentids);
            }
            //如果是跨事业部的职务，则取出符合当前职务的用户
            else
            {
                $admin_user_list=$this->workflow_model->get_user_by_role($role_type[0]->wt_sn);
            }

            foreach ($admin_user_list as $ad) {
                $wv_time=time();
                $wv_wf_sn=$wfd_wf_sn;
                if ($level==0) {
                    $verify_tolist_array[]=$ad->whu_email;
                    $wv_actived=0;
                }else{
                    $wv_actived=-1;
                }
                $wv_sn=$this->workflow_model->add_work_verify($ad->whu_uid,$wv_time,$wv_wf_sn,$level,$wv_actived);
            }
        }
        return $verify_tolist_array;
    }

    //
    public function other_verify($wf_sn,$wv_status=0)
    {
        //获取当前值班名单
        $userdata=$this->session->userdata('admin_chtcdn'); 
        $data['userdata']=$userdata;
        $haslog=$this->workflow_model->get_work_flow_data_by_key($wf_sn);
        if (empty($haslog)) {
            $data['haslog']=false;
        }else{
            $data['haslog']=true;
        }
        $flow_data=$this->workflow_model->get_work_flow_data($wf_sn);

        $en_uname_list=$sw_uname_list=$jd_uname_list=$js_uname_list=$weekend_list=array();
        $js_ct_list=array('文娟','王征','宋芳馨','岑靖鑫','赵嘉琪','赵鹏','罗玲玲');
        $datetemp=$datetemp2=$datetemp3=array();
        foreach ($flow_data as $value) {
            if (!empty($value->wfd_value) && stripos($value->wfd_key, 'en_uname')!==false) {
                $tmp['name']=$value->wfd_value;
                $wfd_key=explode('_', $value->wfd_key);
                $tmp['date']=end($wfd_key);
                $en_uname_list[$value->wfd_value][]=$tmp;
            }
            elseif (!empty($value->wfd_value) && stripos($value->wfd_key, 'sw_uname')!==false) {
                $tmp2['name']=$value->wfd_value;
                $wfd_key=explode('_', $value->wfd_key);
                $tmp2['date']=end($wfd_key);
                $datetemp[]=$tmp2['date'];
                $sw_uname_list[]=$tmp2;
            }
            elseif (!empty($value->wfd_value) && stripos($value->wfd_key, 'jd_uname')!==false) {
                $tmp3['name']=$value->wfd_value;
                $wfd_key=explode('_', $value->wfd_key);
                $tmp3['date']=end($wfd_key);
                $datetemp2[]=$tmp3['date'];
                $jd_uname_list[]=$tmp3;
            }
            elseif (!empty($value->wfd_value) && stripos($value->wfd_key, 'js_uname')!==false) {
                $wfd_key=explode('_', $value->wfd_key);
                $date=end($wfd_key);
                $wfd_value=$value->wfd_value;
                $wfd_value=str_replace(' ', ';', $wfd_value);
                $wfd_value=str_replace('；', ';', $wfd_value);
                $wfd_value=str_replace("\n", ";", $wfd_value);
                $wfd_value=str_replace("\r", ";", $wfd_value);

                $name_list=explode(';', $wfd_value);
                foreach ($name_list as $n) {
                    $tmp4['isct']=0;
                    if (in_array(trim($n), $js_ct_list)) {
                        $tmp4['isct']=1;
                    }
                    $tmp4['date']=$date;
                    $tmp4['name']=$n;
                    $datetemp3[]=$tmp4['date'];
                    $js_uname_list[]=$tmp4;
                }
            }elseif ($value->wfd_value==1 && stripos($value->wfd_key, 'weekend')!==false) {
                $wfd_key=explode('_', $value->wfd_key);
                $weekend_list[]=end($wfd_key);
            }
        }

        foreach ($js_uname_list as $key=>$v) {
            if (!in_array($v['date'], $weekend_list)) {
                unset($js_uname_list[$key]);
                unset($datetemp3[$key]);
            }
        }

        array_multisort($datetemp, SORT_ASC, $sw_uname_list);
        array_multisort($datetemp2, SORT_ASC, $jd_uname_list);
        array_multisort($datetemp3, SORT_ASC, $js_uname_list);

        $data['en_uname_list']=$en_uname_list;
        $data['sw_uname_list']=array();//$sw_uname_list;
        $data['jd_uname_list']=array();//$jd_uname_list;
        $data['js_uname_list']=array();//$js_uname_list;
        
        $data['wf_sn']=$wf_sn;
        $data['nav_type']='rata_list';
        $data['left_nav']='rata_list';
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/other_verify');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

    public function do_other_verify()
    {
        $this->load->model('Kaoqin_model');
        $username=$this->input->post('username');
        $rotadate=date('Y-m-d 00:00:00',strtotime($this->input->post('rotadate')));
        $jabtimes=$this->input->post('jabtimes');
        $wf_sn=$this->input->post('wf_sn');

        $atd_sn_arr=$this->Kaoqin_model->get_atd_type('JAB');
        $epy_sn_arr=$this->Kaoqin_model->get_epy_by_name($username);
        $ATD_StartDate=$rotadate;
        $ATD_EndDate=$rotadate;

        if (isset($atd_sn_arr[0]->ATT_SN) && isset($epy_sn_arr[0]->EPY_ID)) {
            $this->Kaoqin_model->add_atd($epy_sn_arr[0]->EPY_ID,$atd_sn_arr[0]->ATT_SN,$jabtimes,'小时',$ATD_StartDate,$ATD_EndDate);
            $col_val="('rota".trim($wf_sn)."','1',0)";
            if($this->input->post('hasinsert')==0) {
                $this->workflow_model->add_work_data($col_val);
            }
            echo json_encode(array('status'=>'ok','msg'=>$col_val));
        }else{
            echo json_encode(array('status'=>'no','msg'=>$col_val));
        }
    }


    //我的事务
    public function work_list($page=1)
    {
        $wt_sn=false;
        $userdata=$this->session->userdata('admin_chtcdn'); 
        //分页
        $page_flag=true;
        $list=$this->workflow_model->get_user_work_list($userdata['OPI_SN'],$wt_sn,$page_flag);
        $pages['total']=count($list);//数据总条数
        $pages['pageSize']= 30;//每页展示数量
        $pages['url'] = site_url("apps/workflow/index/work_list");//页码链接
        $pages['current']=$page;//当前页码
        $data['pageinfo']=show_page($pages);

        //获取事务列表
        $task_id_str=0;
        $list=array_slice($list,$pages['pageSize']*($page-1),$pages['pageSize']);
        foreach ($list as $v) {
            $task_id_str.=','.$v->wf_sn;
        }
        $data['list']=$this->workflow_model->get_user_work_list($userdata['OPI_SN'],$wt_sn,false,$task_id_str);
        
        $data['nav_type']='userlist';
        $data['left_nav']='userlist';

        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/unverify');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');

    }

    //待审核事务
    public function unverify($wv_status=0,$page=1)
    {
        $userdata=$this->session->userdata('admin_chtcdn'); 
        //分页
        $page_flag=true;
        $list=$this->workflow_model->get_work_list($userdata['whu_uid'],$wv_status,false,$page_flag,false);
		//print_r($list);
        $pages['total']=count($list);//数据总条数
        $pages['pageSize']= 30;//每页展示数量
        $pages['url'] = site_url("apps/workflow/index/unverify/$wv_status/");//页码链接
        $pages['current']=$page;//当前页码
        $data['pageinfo']=show_page($pages);

        //获取事务列表
        $task_id_str=0;
        $list=array_slice($list,$pages['pageSize']*($page-1),$pages['pageSize']);
        foreach ($list as $v) {
            $task_id_str.=','.$v->wv_sn;
        }
        $datalist=$this->workflow_model->get_work_list_by_sn($task_id_str);
        $worklist=array();
        $wf_sn_str='0';
        foreach ($datalist as $wlist) {
            $worklist[$wlist->wf_sn]=$wlist;
            $wf_sn_str.=','.$wlist->wf_sn;
        }
		
        $data['list']=$worklist;
        $flow_data=$this->workflow_model->get_work_flow_data($wf_sn_str);
		
        $f_datas=array();
        foreach ($flow_data as $k => $f) {
            $f_datas[$f->wfd_wf_sn][$f->wfd_key]=$f->wfd_value;
        }
        $data['f_datas']=$f_datas;

        $data['nav_type']='verify';
        $data['left_nav']=$wv_status==0?'unverify':'verify';
        $data['wv_status']=$wv_status;
		//print_r($data);
        if ($wv_status==1) {
            $data['nav_type']='verifyed';
        }
        // var_dump($data["list"]);
        // foreach ($data["list"] as $key => $v) {
        //     echo $v->wf_status;
        //     echo $v->isverify;
        //     echo "<br>";
        // }
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/unverify');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

    //值班表
    public function rata_list()
    {
        $data['list']=$this->workflow_model->get_rata_list(false,12);
        $data['nav_type']='rata_list';
        $data['left_nav']='rata_list';
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/rata_list');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

    //事务类型列表
    public function work_type_list()
    {
        $data['type_list']=$this->workflow_model->get_type_list('all',false,'all');
        $data['left_nav']='type_list';
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/type_list');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');

    }

    //添加事务类型界面
    public function add_work_type($wt_sn=false)
    {
        $data=array();
        //类型列表
        $wt_isdelete=$wt_sn===false?0:'all';
        $data['typelist']=$this->workflow_model->get_type_list(0,false,$wt_isdelete);
        //职务列表
        $data['rolelist']=$this->workflow_model->get_department_list(2);
        
        //表单列表
        $form_path=APPPATH."third_party/workflow/views/form";
        $form_array=scandir($form_path);
        unset($form_array[0],$form_array[1]);
        $data['formlist']=$form_array;

        //要编辑的事务类型信息
        if ($wt_sn!=false) {
            $data['type_info']=$this->workflow_model->get_type_list(0,$wt_sn);
            isset($data['type_info'][0]) && $data['type_info']=$data['type_info'][0];
        }
        $this->load->model('operator_model');
        if (isset($data['type_info']->wt_step) && !empty($data['type_info']->wt_step)) {
            $OPI_SN_STRING=str_replace(';', ',', $data['type_info']->wt_step);
            $OPI_SN_STRING=trim($OPI_SN_STRING,',');
            $admin=$this->operator_model->get_user_by_sn($OPI_SN_STRING);
            $data['wt_step_uname']='';
            foreach ($admin as $u) {
                $wt_step_uname[$u->OPI_SN]=$u->OPI_Name;
            }
            $wt_step_array=explode(';', $data['type_info']->wt_step);
            foreach ($wt_step_array as $v) {
                $data['wt_step_uname'].=$wt_step_uname[$v].'->';
            }
            $data['wt_step_uname']=rtrim($data['wt_step_uname'],'->');
        }
        
        $this->load->view('index/add_work_type',$data);
    }

    //职务类型管理
    public function add_role_type($is_id='')
    {
        $data=array();
        $data['title']='职务';
        $data['wt_refuse']=2;
        $data['structure_list']=$this->workflow_model->get_structure_list($data['wt_refuse']);
        if (empty($data['structure_list'])) {
            $this->add_partment_tree($data['wt_refuse'],$data['title'],0,false);
            redirect(site_url("apps/workflow/index/add_partment_type"));
        }
        if (empty($is_id)) {
            $is_id = $data['structure_list'][0]->id;
        }
        $data['is_id']=$is_id;
        $data['typelist']=$this->workflow_model->get_type_list($is_id,false,0,$data['wt_refuse']);
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/role_type');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

    //事业部小组类型管理
    public function add_partment_type($is_id='')
    {
        $data=array();
        $data['title']='事业部';
        $data['wt_refuse']=1;

        $data['structure_list']=$this->workflow_model->get_structure_list();
        if (empty($data['structure_list'])) {
            $this->add_partment_tree($data['wt_refuse'],$data['title'],0,false);
            redirect(site_url("apps/workflow/index/add_partment_type"));
        }
        if (empty($is_id)) {
            $is_id = $data['structure_list'][0]->id;
        }
        $data['is_id']=$is_id;

        $data['typelist']=$this->workflow_model->get_type_list($is_id,false,0,$data['wt_refuse']);
        
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/partment_type');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

     //添加事业部节点
    public function add_partment_tree($wt_refuse=1,$title='事业部',$is_parent_id=0,$is_ajax=true)
    {
        $this->load->model('InfoStructures_model');
        $insert_id = $this->InfoStructures_model->Add($is_parent_id);

        $wt_name=$title;
        $wt_form='';
        $wt_msg_user='';
        $wt_step='';
        $wt_parentid=$insert_id;
        $wt_time=time();

        if ($insert_id && $wt_sn=$this->workflow_model->add_type($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$wt_time,$wt_refuse)){
            $data[] = array('name' => 'ok', 'value' => $insert_id);
        }else{
            $data[] = array('name' => 'no', 'value' => $this->lang->line('form_info_error'));
        }
        if ($is_ajax) {
            echo json_encode($data);
            return TRUE;
        }
        return $insert_id;
    }
    //移动排序树形结构
    public function tree_move()
    {
        //网站会提交一个同级节点id列表字符串，按照这个去排序
        $this->load->model('InfoStructures_model');
        $parent_id = $this->input->post('pid');
        $idsStr = $this->input->post('ids');
        $idsArray = explode(',', $idsStr);
        foreach ($idsArray as $key => $value)
        {
            if ($value)
            {
                //设置排序
                $this->InfoStructures_model->set_sort($value, $key);
                //设置path
                $this->InfoStructures_model->set_path($parent_id, $value);
            }
        }
        $data[] = array('name' => 'ok', 'value' => $this->lang->line('structures_success_move'));
        echo json_encode($data);
        return TRUE;
    }
    //删除指定事业部分组
    public function delete_partment_tree()
    {
        $isdelete=$this->workflow_model->delete_wt($this->input->post('wt_sn'));
        if ($isdelete) {
            $this->load->model('InfoStructures_model');
            $this->InfoStructures_model->Delete($this->input->post('wt_parentid'));
            $data[] = array('name' => 'ok', 'value' => site_url('apps/workflow/index/add_partment_type'));
            echo json_encode($data);
        }else{
            $data[] = array('name' => 'no', 'value' => $this->lang->line('form_keyworlds_error'));
            echo json_encode($data);
        }
    }

    //员工管理
    public function user_manage($whu_uid=false)
    {
        //事业部分组
        $data['typelist']=$this->workflow_model->get_department_list();
        //职务列表
        $data['rolelist']=$this->workflow_model->get_department_list(2);
        //用户列表
        $data['userlist']=$this->workflow_model->get_user_list();
		
        if ($whu_uid!==false) {
            $data['userinfo']=$this->workflow_model->get_user_list($whu_uid);
			$data['userinfo']=$data['userinfo'][0];
			$data['userrole']=array_filter(explode(',', $data['userinfo']->role));
        }
        $this->load->view('n-header', $data);
        $this->load->view('w-nav');
        $this->load->view('w-left');
        $this->load->view('index/user_manage');
        $this->load->view('w-bottom');
        $this->load->view('n-footer');
    }

    //员工资料更新
    public function user_edit()
    {
		$type=$this->input->post('type');
        $whu_role=$this->input->post('userrole');
        $whu_uname=$this->input->post('whu_uname');
        $whu_email=$this->input->post('whu_email');
        $whu_ip=$this->input->post('whu_ip');
        $department=$this->input->post('department');
        $role=!empty($whu_role)?implode(',',$this->input->post('userrole')):NULL;
        $whu_uid=$this->input->post('whu_uid');
		if($type == 'add'){
			$result = $result=$this->workflow_model->add_webuser($whu_uname,$whu_email,$whu_ip,$department,$role,$whu_uid);
		}else if($type == 'delete'){
			$result=$this->workflow_model->delete_webuser($whu_uname,$whu_uid);
			
		}else if($type == 'update'){
			$result=$this->workflow_model->update_webuser($whu_uname,$whu_email,$whu_ip,$department,$role,$whu_uid);
		}
        
        if ($result) {
            echo json_encode(array('status'=>'ok','msg'=>'操作成功!'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'操作失败!'));
        }
    }

    //添加事业部、职务类型
    public function do_add_role_type()
    {
        $wt_name=$this->input->post('wt_name');
        $wt_refuse=$this->input->post('wt_refuse');
        $wt_form=$this->input->post('wt_form')?1:0;
        $wt_msg_user='';
        $wt_step='';
        $wt_parentid=$this->input->post('wt_parentid')?$this->input->post('wt_parentid'):0;
        $wt_time=time();
        if ($this->input->post('wt_sn')) {
            $result=$this->workflow_model->update_wt($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$this->input->post('wt_sn'));
        }else{
            $result=$this->workflow_model->add_type($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$wt_time,$wt_refuse);
        }
        if ($result) {
            echo json_encode(array('status'=>'ok','msg'=>'保存成功!'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'保存失败!'));
        }
    }


    //更换值班对话框
    public function changerota($wf_sn)
    {
        $data['wf_sn']=$wf_sn;
        $this->load->view('index/changerota',$data);
    }

    //执行更换值班操作
    public function dochangerota()
    {
        $wf_sn=$this->input->post('wf_sn');
        $yusername=$this->input->post('yusername');
        $yrotadate=$this->input->post('yrotadate');
        $tausername=$this->input->post('tausername');
        $tarotadate=$this->input->post('tarotadate');

        if (empty($yrotadate) && empty($tarotadate)) {
            echo json_encode(array('status'=>'no','msg'=>'当前月份你们两个都没有值班安排!'));
            return false;
        }
        if (empty($yusername) || empty($tausername)) {
            echo json_encode(array('status'=>'no','msg'=>'请把更换人填写完整!'));
            return false;
        }

        $yrotadate=str_replace('-', '', $yrotadate);
        $tarotadate=str_replace('-', '', $tarotadate);
        if (!$wf_sn) {
            $datetime=$yrotadate;
            if(empty($yrotadate)) $datetime=$tarotadate;
            $datetime=strtotime(date('Y-m-01 00:00:00',strtotime($datetime)));
            $list=$this->workflow_model->get_rata_list($datetime,1,'ASC');
            if(empty($list)){
                echo json_encode(array('status'=>'no','msg'=>'更换失败，请联系梁宇云!'));
                return false;
            }
            $wf_sn=$list[0]->wf_sn;
        }

        $y_data='';
        $ta_data='';
        $flow_data=$this->workflow_model->get_work_flow_data($wf_sn);
        foreach ($flow_data as $d) {
            if ($yrotadate && stripos($d->wfd_key, $yrotadate)!==false && stripos($d->wfd_value, $yusername)!==false) {
                $y_data=$d;
            }elseif ($tarotadate && stripos($d->wfd_key, $tarotadate)!==false && stripos($d->wfd_value, $tausername)!==false) {
                $ta_data=$d;
            }
        }

        //当月我有值班，TA没有值班
        if ($yrotadate && empty($tarotadate)) 
        {
            $new_y_wfd_sn=$y_data->wfd_sn;
            $new_y_wfd_value=str_replace($yusername, $tausername, $y_data->wfd_value);
            $this->workflow_model->update_wfd($new_y_wfd_sn,$new_y_wfd_value);
        }
        //当前月我没有值班，TA有值班
        elseif (empty($yrotadate) && $tarotadate) 
        {
            $new_ta_wfd_sn=$ta_data->wfd_sn;
            $new_ta_wfd_value=str_replace($tausername, $yusername, $ta_data->wfd_value);
            $this->workflow_model->update_wfd($new_ta_wfd_sn,$new_ta_wfd_value);
        }
        //当月两人同时有值班
        else
        {
            $new_y_wfd_sn=$y_data->wfd_sn;
            $new_y_wfd_value=str_replace($yusername, $tausername, $y_data->wfd_value);
            $new_ta_wfd_sn=$ta_data->wfd_sn;
            $new_ta_wfd_value=str_replace($tausername, $yusername, $ta_data->wfd_value);
            $this->workflow_model->update_wfd($new_y_wfd_sn,$new_y_wfd_value);
            $this->workflow_model->update_wfd($new_ta_wfd_sn,$new_ta_wfd_value);
        }

        //给双方发送值班更换通知
        $userdata=$this->session->userdata('admin_chtcdn'); 
        $from_email=$userdata['whu_email'];
        $this->load->model('operator_model');
        $tolist_array[]=$this->operator_model->get_webhtuser_by_name($tausername,'whu_email');
        $tolist_array[]=$from_email;
        $subject=$yusername.'和你更换了值班，请注意查看中华游值班表';
        $mailbody=$subject.'<br><br> <a href="http://www.mycht.cn/webht.php/apps/workflow/index/rata_list">http://www.mycht.cn/webht.php/apps/workflow/index/rata_list</a>';
        $this->load->model('common_model');
        $this->common_model->sendmail($from_email,$tolist_array,array(),$subject,$mailbody);
        echo json_encode(array('status'=>'ok2','msg'=>'操作成功,请刷新页面查看效果!'));
    }

    public function send_rota_mail()
    {
        $wf_time=strtotime($this->input->post('wf_time'));
        $mail_data=array();
        $mail_data['wf_time']=$wf_time;
        foreach ($this->input->post() as $wfd_key => $wfd_value) {
            $mail_data[$wfd_key]=$wfd_value;
            $mail_data['flow_data'][$wfd_key]=$wfd_value;
        }

        //发送邮件
        $mail_tpl='rota.php';
        $subject='海纳国旅'.date('n',$wf_time).'月值班安排';
        $html='各位同事，<br>以下是海纳国旅'.date('Y',$wf_time).'年'.date('n',$wf_time).'月值班安排，请大家<span style="color:red;">做好提醒</span>，按相关值班规定值班。<br>为方便统计加班小时数，请记得到<span style="color:red;">值班记录系统</span>填写值班记录，自行调班的同事请记得到<span style="color:red;">值班记录系统</span>里面更改。<br>谢谢！';
        $mailbody=$html.'<br><br>'.$this->load->view("mail_tpl/$mail_tpl",$mail_data,true);

        //发送人信息
        $this->load->model('common_model');
        $userdata=$this->session->userdata('admin_chtcdn');
        $from_email=$userdata['whu_email'];
        $tolist_array[]='UAAS2013@citsguilin.com';
        $this->common_model->sendmail($from_email,$tolist_array,array(),$subject,$mailbody);
        echo json_encode(array('status'=>'ok','msg'=>'发送成功!'));
    }

    //更新指定事务内容
    public function update_work()
    {
        //更新事务
        $this->load->model('operator_model');
        $current_user=$this->input->post('wf_user');
        $tmp = $this->operator_model->get_user_by_name($current_user);
        $wf_user=$tmp['OPI_SN'];
        $wf_time=strtotime($this->input->post('wf_time'));
        $group_admin=explode('@', $this->input->post('wf_wt_sn'));
        $wf_wt_sn=$group_admin[0];
        $wf_sn=$this->input->post('wf_sn');
        $result=$this->workflow_model->update_wf($wf_sn,array('wf_wt_sn'=>$wf_wt_sn,'wf_user'=>$wf_user,'wf_time'=>$wf_time));
        
        //更新键值对
        if ($this->workflow_model->delete_wfd($wf_sn)) {
            $col_val='';
            foreach ($this->input->post() as $wfd_key => $wfd_value) {
                // $col_val.="('".trim($wfd_key)."','".$wfd_value."',".$wf_sn."),";
				
                $col_val.="('".trim($wfd_key)."','".chop($wfd_value)."',".$wf_sn."),";//csk 2016-9-22 去掉末尾的换行和空格
            }
            $apply_user=$this->input->post('apply_user');
            if (empty($apply_user)) {
                $col_val.="('apply_user','".$current_user."',".$wf_sn."),";
            }
            $col_val=substr($col_val, 0, -1);

            $wfd_sn=$this->workflow_model->add_work_data($col_val);
        }

        //如果还没有开始审核，则更改审核人员
        $has_verify_list=$this->workflow_model->get_verify($wf_sn,false,1);
        if (empty($has_verify_list) && $this->workflow_model->delete_wv($wf_sn)) {
            $typeinfo=$this->workflow_model->get_type_list(0,$wf_wt_sn);
            $verify_tolist_array=$this->get_verify_admin_list($wf_sn,$group_admin[1],$typeinfo[0]->wt_linear);
        }

        $verify_referer_url=site_url('apps/workflow/index/unverify');
        if ($this->session->userdata('verify_referer')) {
            $verify_referer_url=$this->session->userdata('verify_referer');
        }
		
        echo json_encode(array('status'=>'ok_go','msg'=>'更新成功!','url'=>$verify_referer_url));
    }

    //添加事务类型
    public function do_add_work_type()
    {
        $wt_name=$this->input->post('wt_name');
        $wt_form=$this->input->post('wt_form');
        $wt_msg_user=rtrim($this->input->post('wt_msg_user'),';');
        $wt_step=rtrim($this->input->post('wt_step'),';');
        $wt_parentid=$this->input->post('wt_parentid');
        $wt_time=time();
        $wt_isneedkaoqin=$this->input->post('wt_isneedkaoqin')?1:0;
        $wt_description=$this->input->post('wt_description');
        $wt_isrest=$this->input->post('wt_isrest')?1:0;
        $wt_userdefined=$this->input->post('wt_userdefined')?1:0;
        $wt_needmail=$this->input->post('wt_needmail')?1:0;
        $wt_linear=$this->input->post('wt_linear');
        if ($this->input->post('wt_sn')!='') {
            $insertid=$this->workflow_model->update_wt($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$this->input->post('wt_sn'),$wt_isneedkaoqin,$wt_description,$wt_isrest,$wt_userdefined,$wt_needmail,$wt_linear);
        }else{
            $insertid=$this->workflow_model->add_type($wt_name,$wt_form,$wt_step,$wt_msg_user,$wt_parentid,$wt_time,0,$wt_isneedkaoqin,$wt_description,$wt_isrest,$wt_userdefined,$wt_needmail,$wt_linear);
        }
        if ($insertid) {
            echo json_encode(array('status'=>'ok','msg'=>'操作成功!'));
        }else{
            echo json_encode(array('status'=>'no','msg'=>'操作失败!'));
        }
    }

    //
    public function dalete_work_type($wt_sn,$wt_isdelete=1)
    {
        $tips=$wt_isdelete==1?'已停用!':'启用成功!';
        $this->workflow_model->delete_wt($wt_sn,$wt_isdelete);
        echo json_encode(array('status'=>'ok','msg'=>$tips));
    }

    //删除指定事务
    public function dalete_work_flow($wf_sn)
    {
        $this->workflow_model->delete_wf($wf_sn);
        $this->workflow_model->delete_wfd($wf_sn);
        $this->workflow_model->delete_wv($wf_sn);
        echo json_encode(array('status'=>'ok','msg'=>'删除成功!'));
    }

    //用户名自动提示
    public function get_user_list(){
        $q = $this->input->get('q');
        $str_array=explode(';', $q);
        $q=end($str_array);
        if (!$q) return;

        $showcount = 1;
        $this->load->model('operator_model');
        $tmp = $this->operator_model->all();
        foreach ($tmp as $u) {
            if ($showcount > 8)
                break;
            if (strstr($u->OPI_Name,$q)) {
                echo $u->OPI_Name.';<span class="hidden">'.$u->OPI_SN.'</span>'."\n";
                $showcount++;
            }
        }
    }

    public function myrota(){
        $startdate=date('Y-m-01 00:00:00');
        $datetime=strtotime("$startdate -3 month");
        $list=$this->workflow_model->get_rata_list($datetime,5,'ASC');
        $data['datelist']=array();
        if (!empty($list)) {
            $userdata=$this->session->userdata('admin_chtcdn');
            foreach ($list as $v) {
                $flow_data=$this->workflow_model->get_work_flow_data($v->wf_sn);
                foreach ($flow_data as $key => $d) {
                    $wfd_key_arr=explode('_', $d->wfd_key);
                    if (!empty($d->wfd_value) && stripos($d->wfd_value, $userdata['OPI_Name'])!==false && isset($wfd_key_arr[2]) && is_numeric($wfd_key_arr[2])) {
                        $need_date=strlen($wfd_key_arr[2])==7?substr($wfd_key_arr[2], 0,-1).'0'.substr($wfd_key_arr[2],-1):$wfd_key_arr[2];
                        $myrotadate=$need_date;
                        $need_date=date('Y-n-j',strtotime($need_date));
                        $data['datelist'][]=array('date'=>$need_date,'remark'=>'值班','link'=>'http://www.mycht.cn/webht.php/apps/workflow/index/changemyrota/'.$myrotadate,'title'=>(date('n月j日',strtotime($need_date))).'有你的值班！');
                    }
                }
            }
        }
        if (!empty($data['datelist'])) {
            $data['datelist']=json_encode($data['datelist']);
        }else{
            $data['datelist']='';
        }
        $this->load->view('index/myrota',$data);
    }

    public function changemyrota($myrotadate=false)
    {
        $data=array();
        $data['yrotadate']='';
        $myrotadate && $data['yrotadate']=date('Y-m-d',strtotime($myrotadate));
        $this->load->view('index/changemyrota',$data);
    }

   public function send_prev_notify()
   {
        //本月和下个月的值班数据
        $datetime=strtotime(date('Y-m-01 00:00:00'));
        $data['list']=$this->workflow_model->get_rata_list($datetime,2);
        if(empty($data['list'])) return false;
        foreach ($data['list'] as $v) {
            $data['flow_data'][]=$this->workflow_model->get_work_flow_data($v->wf_sn);
        }

        $today=date('Ymd');
        $userlist=array();
        $needdate_list=array();
        if(empty($data['flow_data'])) return false;
        foreach ($data['flow_data'] as $f) {
            foreach ($f as $d) {
                $wfd_key_arr=explode('_', $d->wfd_key);
                if (!empty($d->wfd_value) && isset($wfd_key_arr[2]) && is_numeric($wfd_key_arr[2])) {
                    $wfd_key_arr[2]=strlen($wfd_key_arr[2])==7?substr($wfd_key_arr[2], 0,-1).'0'.substr($wfd_key_arr[2],-1):$wfd_key_arr[2];
                    //值班日期在今天之后的
                    $wfd_key_arr[2]>$today && $userlist[$wfd_key_arr[2]][]=$d->wfd_value;
                }
                //获取非上班的值班日期集合
                if ($wfd_key_arr[0]=='weekend' && $d->wfd_value==1) {
                    $wfd_key_arr[1]=strlen($wfd_key_arr[1])==7?substr($wfd_key_arr[1], 0,-1).'0'.substr($wfd_key_arr[1],-1):$wfd_key_arr[1];
                    $needdate_list[]=$wfd_key_arr[1];
                }
            }
        }
        //提取未来最近的一个节假日
        ksort($userlist,SORT_NUMERIC);
        $num=0;
        $name_string='';
        $time_arr=array();
        foreach ($userlist as $key => $value) {
            if (!in_array($key, $needdate_list)) {
                continue;
            }
            $num==0?$num=$key:'';
            if ($key-$num>1) {
                $isnextmonth = date('Ymd',strtotime("$key -1 day")); 
                if ($isnextmonth!=$num) {
                    break;
                }
            }
            $num=$key;
            $time_arr[]=$key;
            foreach ($value as $n) {
                $name_string.=$n.';';
            }
        }
        //如果不是节前第一天，停止发送提醒邮件处理
        $next_festival=min($time_arr);
        $pre_festival=date('Ymd',strtotime("$next_festival -1 day"));
        if ($today!=$pre_festival) {
            echo '还没到节前一天';
            return false;
        }

        $name_string=str_replace(' ', ';', $name_string);
        $name_string=str_replace('；', ';', $name_string);
        $name_string=str_replace('（', '(', $name_string);
        $name_string=str_replace('）', ')', $name_string);
        $name_string=str_replace('(', ';(', $name_string);
        $name_string=str_replace(')', ');', $name_string);
        $name_string=str_replace('惠毅', '惠毅;惠 毅', $name_string);
        $name_string=str_replace(array('，',",","\t","\n","\r"),';', $name_string);
        $name_arr=explode(';', $name_string);
        $name_arr=(array_filter(array_flip(array_flip($name_arr))));
        if (empty($name_arr)) {
            return false;
        }
        $map=' WHERE ';
        foreach ($name_arr as $name) {
            $name=preg_replace("/\(.*\)/", '', $name);
            $name=trim($name);
            !empty($name) && $map.=" whu_uname like '$name%' OR ";
        }
        $map=substr($map, 0,-3);
        $this->load->model('operator_model');
        $mailarr=$this->operator_model->get_webhtuser_by_name($map,'*',true);
        $tolist_array=array();
        foreach ($mailarr as $list) {
            !empty($list->whu_email) && $tolist_array[]=$list->whu_email;
            //echo $list->whu_uname.':'.$list->whu_email.'<br>';
        }

        //发送人信息
        $this->load->model('common_model');
        $from_email='AAS@citsguilin.com';
        
        $subject="节假日值班提醒";
        $mailbody='<p>中华游温馨提醒，本周有您的值班的安排：</p><p><a href="http://www.mycht.cn/webht.php/apps/workflow/index/rata_list">点击查看中华游值班表</a></p>';
        $mailbody.='<br><p style="color:#888;">PS：本次提醒仅供参考，详情请查看中华游值班表</p>';
        $this->common_model->sendmail($from_email,$tolist_array,array('cht07@citsguilin.com'),$subject,$mailbody);
        echo 'ok';
   }

   public function send_after_notify()
   {
        //本月和上个月的值班表(有可能本月初和上月尾是连着的假期)
        $currentmonth=date('Y-m-01 00:00:00',time());
        $datetime = strtotime("$currentmonth -1 month"); 
        $data['list']=$this->workflow_model->get_rata_list($datetime,2,'DESC');
        if(empty($data['list'])) return false;
        foreach ($data['list'] as $v) {
            $data['flow_data'][]=$this->workflow_model->get_work_flow_data($v->wf_sn);
        }
        //获取今天之前且离今天最近的假期值班人员列表
        $today=date('Ymd');
        $userlist=array();
        $needdate_list=array();
        if(empty($data['flow_data'])) return false;

        foreach ($data['flow_data'] as $f) {
            foreach ($f as $d) {
                $wfd_key_arr=explode('_', $d->wfd_key);
                //值班日期在今天之前的值班人
                if (!empty($d->wfd_value) && isset($wfd_key_arr[2]) && is_numeric($wfd_key_arr[2])) {
                    $wfd_key_arr[2]=strlen($wfd_key_arr[2])==7?substr($wfd_key_arr[2], 0,-1).'0'.substr($wfd_key_arr[2],-1):$wfd_key_arr[2];
                    $wfd_key_arr[2]<$today && $userlist[$wfd_key_arr[2]][]=$d->wfd_value;
                }
                //获取非上班的值班日期集合
                if ($wfd_key_arr[0]=='weekend' && $d->wfd_value==1) {
                    $wfd_key_arr[1]=strlen($wfd_key_arr[1])==7?substr($wfd_key_arr[1], 0,-1).'0'.substr($wfd_key_arr[1],-1):$wfd_key_arr[1];
                    $needdate_list[]=$wfd_key_arr[1];
                }
            }
        }
        //如果没有获取到值班人或者当前日期正值节假日，则不继续处理
        if(empty($userlist) || in_array($today, $needdate_list)) return false;

        krsort($userlist,SORT_NUMERIC);
        $num=0;
        $name_string='';
        $time_arr=array();
        //剔除非休假时间
        foreach ($userlist as $key => $value) {
            if (!in_array($key, $needdate_list)) {
                continue;
            }
            //如果已经不是最近一个假期，则跳出循环
            if ($num-$key>1) {
                $islastmonth = date('Ymd',strtotime("$num -1 day")); 
                if ($islastmonth!=$key) {
                    break;
                }
            }
            $num=$key;
            $time_arr[]=$key;
            foreach ($value as $n) {
                $name_string.=$n.';';
            }
        }

        //如果不是节后第一天，停止发送提醒邮件处理
        $last_festival=max($time_arr);
        $after_festival=date('Ymd',strtotime("$last_festival +1 day"));
        if ($today!=$after_festival) {
            echo '已经发送过了';
            return false;
        }

        $name_string=str_replace(' ', ';', $name_string);
        $name_string=str_replace('；', ';', $name_string);
        $name_string=str_replace('（', '(', $name_string);
        $name_string=str_replace('）', ')', $name_string);
        $name_string=str_replace('(', ';(', $name_string);
        $name_string=str_replace(')', ');', $name_string);
        $name_string=str_replace('惠毅', '惠毅;惠 毅', $name_string);
        $name_string=str_replace(array('，',",","\t","\n","\r"),';', $name_string);
        $name_arr=explode(';', $name_string);
        $name_arr=(array_filter(array_flip(array_flip($name_arr))));
        
        //过滤掉已经发了记录的名字
        $namestring="'".implode("','", $name_arr)."'";
        $date_time=strtotime(min($time_arr));
        $remarks=$this->workflow_model->get_list_by_namestring($namestring,$date_time);
        foreach ($remarks as $key => $value) {
            if (in_array($value->OPI_Name, $name_arr)) {
                if ($value->OPI_Name=='惠毅') {
                    unset($name_arr[array_search('惠 毅', $name_arr)]);
                }
                unset($name_arr[array_search($value->OPI_Name, $name_arr)]);
            }
        }
        if (empty($name_arr)) return false;

        //提取邮件地址
        $map=' WHERE ';
        foreach ($name_arr as $name) {
            $name=trim($name);
            !empty($name) && $map.=" whu_uname like '$name%' OR ";
        }
        $map=substr($map, 0,-3);
        $this->load->model('operator_model');
        $mailarr=$this->operator_model->get_webhtuser_by_name($map,'*',true);
        $tolist_array=array();
        foreach ($mailarr as $list) {
            !empty($list->whu_email) && $tolist_array[]=$list->whu_email;
            //echo $list->whu_uname.':'.$list->whu_email.'<br>';
        }

        //发送人信息
        $this->load->model('common_model');
        $from_email='AAS@citsguilin.com';
        
        $subject="值班记录填写提醒";
        $mailbody='<p>你的值班记录还没有填写:</p><p><a href="http://www.mycht.cn/webht.php/apps/workflow/">点击去填写</a></p>';
        $mailbody.='<br><p style="color:#888;">PS：本次提醒名单由程序自动提取，如果你已经填过了，请忽略！</p>';
        !empty($tolist_array) && $this->common_model->sendmail($from_email,$tolist_array,array('cht07@citsguilin.com','CHT67@citsguilin.com'),$subject,$mailbody);
        echo 'ok';
   }

   public function print_business($wf_sn,$wv_status=1,$nav_type='userlist')
   {
        $data['wf_sn']=$wf_sn;
        $data['wv_status']=$wv_status;
        $data['nav_type']=$nav_type;
        $this->load->view('index/print_business',$data);
   }
   public function print_text($wf_sn,$wv_status=1,$nav_type='userlist')
   {
        $data['userdata']=$this->session->userdata('admin_chtcdn');
        //事务申请人信息
        $userdata=$this->session->userdata('admin_chtcdn'); 
        $flow=$this->workflow_model->get_work_list($userdata['OPI_SN'],$wv_status,$wf_sn);
        $data['flow']=$flow[0];
        
        //事务具体内容
        $flow_data=$this->workflow_model->get_work_flow_data($wf_sn);
        $data['flow_data']=array();
        foreach ($flow_data as $d) {
            $data[$d->wfd_key]=$d->wfd_value;
            $data['flow_data'][$d->wfd_key]=$d->wfd_value;
        }
        $form=$data['flow']->wt_form;
        $data['wf_sn']=$wf_sn;
        
        //事务类型
        $data['typelist']=$this->workflow_model->get_type_list($data['flow']->wt_parentid,false,'all');
        
        //以往步骤的审核信息
        $data['verify_data']=$this->workflow_model->get_verify_by_filed($wf_sn,'wv_comment');

        $data['nav_type']=$nav_type;
        if ($nav_type=='userlist') {
            $data['left_nav']='userlist';
        }elseif ($nav_type=='rata_list') {
            $data['left_nav']='rata_list';
        }elseif ($wv_status==0) {
            $data['left_nav']='unverify';
        }else{
            $data['left_nav']='verify';
        }
        $this->load->view('index/print_text', $data);
   }
	
	
}
