load->_model('Model_trademark','trademark');
		$this->load->_model('Model_excel','excel');
	}
	//定义方法的调用规则 获取URI第二段值
    public function _remap($arg,$arg_array)
    {
		if($arg == 'add')//添加
        {
             $this->_add();
        }
		else if($arg == 'edit')//修改
        {
             $this->_edit($arg_array);
        }
		else if($arg == 'del')//修改
        {
             $this->_del();
        }
		else if($arg == 'rows')//修改
        {
             $this->_rows();
        }
		else if($arg == 'code')
        {
             $this->_code($arg_array);
        }
		else if($arg == 'excel')
        {
             $this->_excel();
        }
		else if($arg == 'down')
        {
             $this->_down();
        }
		else
		{
			 $this->_index();
		}
    }
	//管理
	public function _index()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['page']))  
		{
		    $page = $this->input->post('page',true);
		    $perpage = $this->input->post('perpage',true);
			$name = $this->input->post('name',true);
			$type = $this->input->post('type',true);
			$class = $this->input->post('class',true);
			$owner = $this->input->post('owner',true);
			$agent = $this->input->post('agent',true);
			$number = $this->input->post('number',true);
			$xztime = $this->input->post('xztime',true);
			$timetk = $this->input->post('timetk',true);
			$timetj = $this->input->post('timetj',true);
			$timetk = strtotime($timetk);
			$timetj = strtotime($timetj);
			$where = "1=1 ";
            //数据排序
            $order_str = $xztime." desc";
			if($name)
            {
                $where  .= " and name like '%$name%'";
            }
			if($type)
            {
                $where  .= " and type = '$type'";
            }
			if($class)
            {
                $where  .= " and class like '%$class%'";
            }
			if($owner)
            {
                $where  .= " and owner like '%$owner%'";
            }
			if($agent)
            {
                $where  .= " and agent like '%$agent%'";
            }
			if($number)
            {
                $where  .= " and number like '%$number%'";
            }
			if($timetk && $timetj)
            {
                $where  .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
            }
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
            //取得信息列表
            $info_list = $this->trademark->find_all($where,'id,type,name,number,class,owner,agent,djtime,sqtime,ggtime',$order_str,$start,$perpage);
			 //格式化数据
            foreach ($info_list as $key=>$value) 
		    {
				if($value['type'] == 1)
				{
					$info_list[$key]['type'] = '国内商标';
				}
				else if($value['type'] == 2)
				{
					$info_list[$key]['type'] = '美国商标';
				}
				else if($value['type'] == 3)
				{
					$info_list[$key]['type'] = '国内专利';
				}
				else if($value['type'] == 4)
				{
					$info_list[$key]['type'] = '美国专利';
				}
				else if($value['type'] == 5)
				{
					$info_list[$key]['type'] = '欧洲商标';
				}
				$info_list[$key]['djtime'] = ($value['djtime'] > 0)?date('Y-m-d H:i:s',$value['djtime']):'';
				$info_list[$key]['sqtime'] = ($value['sqtime'] > 0)?date('Y-m-d H:i:s',$value['sqtime']):'';
				$info_list[$key]['ggtime'] = ($value['ggtime'] > 0)?date('Y-m-d H:i:s',$value['ggtime']):'';
            }
		    $total = $this->trademark->find_count($where);
		    $pagenum = ceil($total/$perpage);
		    $over = $total-($start+$perpage);
		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
		    echo json_encode($rows);exit;
		}
		$this->_Template('trademark',$this->data);
	}
	//添加
	public function _add()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['name']))
		{
			if($this->trademark->insert($post))
        	{
         		echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
        	}
		}
		$this->_Template('trademark_add',$this->data);
	}
	//修改
	public function _edit($arg_array)
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['id']))
		{
			$id = $this->input->post('id',true);
			if($this->trademark->save($post,$id))
        	{
         		echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
        	}
		}
		$arg_array = $arg_array[0];
		$trademark = $this->trademark->read($arg_array);
		$this->data['trademark'] = $trademark;
		$this->_Template('trademark_edit',$this->data);
	}
	//删除
	public function _del()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['s']))  
		{
            $id_arr = $this->input->post('s');
            $id_arr =  explode(',',$id_arr);
            if(!$id_arr)
            {
                echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
            }
            //循环删除记录
            foreach ($id_arr as $v) 
		    {
                $this->trademark->remove($v);
            }
            echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
		}
    }
	
	public function _excel()
	{
		$dir = '/data/excel/'.date('Ymd',time()).'/';
		$config['upload_path'] = '.'.$dir ;
		$config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
        $config['allowed_types'] = 'xls|xlsx|csv';
        $config['max_size'] = 10240;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
        if ($this->upload->do_upload('userfile'))
        {
			$full_path = $dir.$this->upload->data('file_name');
			$fileName = '.' . $full_path;
            if (!file_exists($fileName)) 
			{
				echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
            }
			else
			{
				require_once "./data/excel/PHPExcel/IOFactory.php";
				$phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
				$phpExcel->setActiveSheetIndex(0);// 设置为默认表
				$sheetCount = $phpExcel->getSheetCount();// 获取表格数量
				$row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
				$column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
				++$column;//如果列数大于26行
				$list = array();
				for ($i = 2; $i <= $row; $i++) // 行数循环
				{
					$data = array();
                    for ($c = 'A'; $c != $column; $c++)  // 列数循环
	                {
						$data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
                    }
					$list[] = $data;
                }
			}
			$i = 0;$j = 0;$ed = array();$tytime = time();
		    foreach ($list as $key=>$value)
		    {
				$cf = $this->trademark->find_all("number = '".$value['3']."'");
				if(!isset($value['3'][0])) 
				{
					$post['name'] = $value['2'];
					if($value['1'] == '国内商标')
					{
						$post['value'] = 1;
					}
					else if($value['1'] == '美国商标')
					{
						$post['value'] = 2;
					}
					else if($value['1'] == '国内专利')
					{
						$post['value'] = 3;
					}
					else if($value['1'] == '美国专利')
					{
						$post['value'] = 4;
					}
					else if($value['1'] == '欧洲商标')
					{
						$post['value'] = 5;
					}
					$post['number'] = $value['3'];
					$post['class'] = $value['4'];
					$post['sqtime'] = $value['5'];
					$post['ggtime'] = $value['6'];
					$post['owner'] = $value['7'];
					$this->trademark->insert($post);	
				}
				else
				{
					$ed[] = array($value['3'].'-有重复');
					$i++;
				}
				sleep(2);//停留2秒
			}
			if($i+$j > 0)
			{
				$time = date('Ymd',time());
				$title = '错误信息-'.$time;
        		$titlename = "
";
				$tail = "\n";
        		$filename = $title.".xls";
        		$ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail);
				$dir = '/data/excel/'.$time.'/';
				$file_name = 'error_'.$time.rand(1000,9999);
				if(!is_dir('.'.$dir))mkdir('.'.$dir,0777);
				$myfile = fopen(".".$dir.$file_name.".xls", "w") or die();
				fwrite($myfile,$ecl);
				fclose($myfile);
				$error = $dir.$file_name.'.xls';
				echo json_encode(array('msg'=>'添加成功,'.$i.'条异常,'.$j.'条失败','error'=>$error,'success'=>true));exit;
			}
			else
			{
				echo json_encode(array('msg'=>'添加成功!','error'=>1,'success'=>true));exit;
			}
        }
		else
		{
			echo json_encode(array('msg'=>'上传失败!','t'=>$this->upload->display_errors(),'success'=>false));exit;
		}
    }
	
	//下载excel模板
	public function _down()
	{
		if(isset($_GET['excela']))  
		{
			$title = "许昌龙熠美发饰品有限公司第一届雀王争霸赛"; 
            $titlename = "
            | 许昌龙熠美发饰品有限公司第一届雀王争霸赛 | 
|---|
            
			| 参加/不参加选手昵称 | 年龄 | 喜好 | 联系电话 | 麻龄 | 输赢 | 牌后总结 | 
            
"; 
            $filename = $title.".xls"; 
			$tail = "\n"; 
            $this->excel->get_fz2($info_list,$titlename,$filename,$tail);
			exit;
            $title = "导入模板"; 
            $titledata = array(array('序号','商标类型','商标名称','注册号','国际分类','申请日期','注册公告日期','申请人'),array('1','国内商标','AAA','1111111','18','2000-01-01','2000-01-01','许昌龙熠美发饰品有限公司'));
            $filename = $title.".xls";
            $this->get_excel($titledata,$filename);
	    }
	}
	
	public function get_excel($titledata,$filename)
	{
		require_once "./data/excel/PHPExcel/IOFactory.php";
		$objPHPExcel = new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $dataArray = $titledata;
        $objPHPExcel->getActiveSheet()->fromArray($dataArray, null, 'A1');
		header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename='.$filename);
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        $objPHPExcel->disconnectWorksheets();
		
	}
}