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

class Import extends CI_Controller {
    
    public function __construct(){
        //parent::__construct();
     //   $this->load->library(‘php-excel/reader’);
	    parent::__construct();
		$this->load->model('excel_import_model');
		$this->load->library('excel');
		$this->common_model->checkpurview(72);
		 
    }
	
	public function index() {
	    $this->common_model->checkpurview(72);
	    $this->load->model('data_model');
		$data['select_user']    = $this->mysql_model->get_results('user',array('isdel'=>0));
		//$where='';
		//$data['list']= $this->data_model->get_user_list($where,$type=2);
		//$post = array('User','excelfile','excelfile');
		$this->load->view('Import/index',$data);
		//var_dump($data);


    }
	function daoru()
	{

	    $jieshou = str_htmlencode($this->input->post(NULL,TRUE));
	    //var_dump($data);
		$addusers=$jieshou['User'];
		//var_dump($addusers);


		if(isset($_FILES["file"]["name"]))
		{
			$path = $_FILES["file"]["tmp_name"];
			$object = PHPExcel_IOFactory::load($path);
			foreach($object->getWorksheetIterator() as $worksheet)
			{
				$highestRow = $worksheet->getHighestRow();
				$highestColumn = $worksheet->getHighestColumn();
				for($row=2; $row<=$highestRow; $row++)
				{
					$name = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
					$linkman = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
					$job = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
					$mobile = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
					$qq = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
					$type = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
					$start = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
					$source = $worksheet->getCellByColumnAndRow(7, $row)->getValue();
					$trade = $worksheet->getCellByColumnAndRow(8, $row)->getValue();
					$area1 = $worksheet->getCellByColumnAndRow(9, $row)->getValue();
					$area2 = $worksheet->getCellByColumnAndRow(10, $row)->getValue();
					$address = $worksheet->getCellByColumnAndRow(11, $row)->getValue();
					$email = $worksheet->getCellByColumnAndRow(12, $row)->getValue();
					$website = $worksheet->getCellByColumnAndRow(13, $row)->getValue();
					$weixin = $worksheet->getCellByColumnAndRow(14, $row)->getValue();
					$tel = $worksheet->getCellByColumnAndRow(15, $row)->getValue();
					$remark = $worksheet->getCellByColumnAndRow(16, $row)->getValue();

					$adddate=date('Y-m-d');
					$addtime=date('Y-m-d H:i:s');

					$data[] = array(
						'name'		=>	$name,
						'area1'			=>	$area1,
						'area2'				=>	$area2,
						'address'		=>	$address,
						'source'			=> $source,
						'trade'		=>	$trade,
						'email'			=>	$email,
						'start'				=>	$start,
						'linkman'		=>	$linkman,
						'job'			=>	$job,
						//'zip'		=>	$zip,
						'qq'			=>	$qq,
						'mobile'				=>	$mobile,
						'website'		=>	$website,
						'weixin'			=>	$weixin,
						'tel'		=>	$tel,
						'remark'			=>	$remark,
						'adduser'				=>	$addusers,
						'adddate'		=>	$adddate,
						'addtime'			=>	$addtime,
					);






				}
			}
            if (is_array($data)&&count($data)>0){


                $names = $this->mysql_model->get_count('customer',array('name'=>$name));

              //  var_dump($name);

                if ( $names >0 ){
                   // $names=$data['name'];
                    echo $name ; echo '客户名称已存在';
                   // $this->load->view('Import/index');
                } else {
                    $this->excel_import_model->insert($data);
                    echo '导入成功';
                   // $this->load->view('Import/index');
                }


            }else {
                str_alert('导入失败');
                //$this->load->view('Import/index');
            }


		  
			
		}	

	}
	

	
}

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