load->library('session');
		$this->load->_model('Model_productprice','productprice');
		$this->load->_model('Model_typeclass','typeclass');
	}
	//定义方法的调用规则 获取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
		{
			 $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);
			$supplier = $this->input->post('supplier',true);
			$where = "1=1 ";
			if($name)
            {
                $where  .= " and name = '$name'";
            }
			if($supplier)
            {
                $where  .= " and supplier = '$supplier'";
            }
            //数据排序
            $order_str = "id asc";
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
            //取得信息列表
            $info_list = $this->productprice->find_all($where,'id,name,supplier,purchase,cost,salesprice,time,class',$order_str,$start,$perpage);
			foreach ($info_list as $key=>$value) 
		    {
				$purchase = $this->typeclass->read($value['purchase']);
				$info_list[$key]['purchase'] = $purchase['title'];
				
				$cost = $this->typeclass->read($value['cost']);
				$info_list[$key]['cost'] = $cost['title'];
				
				$salesprice = $this->typeclass->read($value['salesprice']);
				$info_list[$key]['salesprice'] = $salesprice['title'];
				$info_list[$key]['name'] = "".$value['name']."";
			    $info_list[$key]['time'] = date('Y-m-d',$value['time']);
				$info_list[$key]['class'] = '价格表明细配置复制价格表';
		    }
		    $total = $this->productprice->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('productprice',$this->data);
	}
	//添加
	public function _add()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['name']))
		{
			$post['name'] = $this->input->post('name',true);
			$post['supplier'] = $this->input->post('supplier',true);
			$post['user'] = $this->input->post('user',true);
			$post['phone'] = $this->input->post('phone',true);
			$post['purchase'] = $this->input->post('purchase',true);
			$post['cost'] = $this->input->post('cost',true);
			$post['salesprice'] = $this->input->post('salesprice',true);
			$time = $this->input->post('time',true);
			$post['time'] = strtotime($time);
			$post['operation'] = time().rand(1000,9999);
			if($this->productprice->insert($post))
        	{
				$productprice = $this->productprice->get_operation($post['operation']);
				if($this->productprice->save(array('class'=>$productprice['id']),$productprice['id']))
				{
					echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
				}
         		else
				{
					$this->productprice->remove($productprice['id']);
					echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
				}
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
        	}
		}
		$this->_Template('productprice_add',$this->data);
	}
	//修改
	public function _edit($arg_array)
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['id']))
		{
			$id = $this->input->post('id',true);
			$post['name'] = $this->input->post('name',true);
			$post['supplier'] = $this->input->post('supplier',true);
			$post['user'] = $this->input->post('user',true);
			$post['phone'] = $this->input->post('phone',true);
			$post['purchase'] = $this->input->post('purchase',true);
			$post['cost'] = $this->input->post('cost',true);
			$post['salesprice'] = $this->input->post('salesprice',true);
			$time = $this->input->post('time',true);
			$post['time'] = strtotime($time);
			if($this->productprice->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];
		$productprice = $this->productprice->read($arg_array);
		$this->data['productprice'] = $productprice;
		$this->_Template('productprice_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->productprice->remove($v);
            }
            echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
		}
    }
	//拷贝价格
	public function _rows()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['copyid']))
		{
			$copyid = $this->input->post('copyid',true);
			$productprice = $this->productprice->read($copyid);
			$post['name'] = $productprice['name']."副本";
			$post['supplier'] = $productprice['supplier'];
			$post['user'] = $productprice['user'];
			$post['phone'] = $productprice['phone'];
			$post['purchase'] = $productprice['purchase'];
			$post['cost'] = $productprice['cost'];
			$post['salesprice'] = $productprice['salesprice'];
			$post['class'] = $productprice['class'];
			$post['time'] = $productprice['time'];
			if($this->productprice->insert($post))
        	{
				echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
        	}
		}
	}
}