<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
/**
 *----------------------------------------------------------------------------
 * 版权所有 2010-2016 HuCart.COM，并保留所有权利。
 * 网站地址: http://Www.HuCart.Com；
 * 作者: HuCart.COM
**/
class Ajax_pro_type extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	function __construct()
	{
		parent::__construct();
		$this->load_lang('adminsys/product');
		$this -> load('admin'); 
		$this -> admin -> online_admin();
		$this -> load_db();
	}
	
	function get_pro_type()
	{
		
		$pro_id_val = $this -> input -> req['pro_id'];
		//$prt_id_val = $this -> input -> req['prt_id'];
		//查询数据
		$prt_id_val = $this -> input -> req['prt_id'];
		//$prt_id_val = $this -> input -> post['prt_id'];

		if($prt_id_val){
			$sql_pra 	= " SELECT pra_id,pra_name,pra_input_type,pra_type,pra_values,pra_index,pra_is_link  FROM ".$this -> db -> prefix."pro_attribute WHERE prt_id = ".$prt_id_val." ORDER BY pra_list ASC , pra_id ASC ";
			$pra_detail = $this -> db -> select_info($sql_pra);
			foreach((array)$pra_detail as $pkey => $pval )
			{
				//echo $this-> lang['txt_select'];
				$pra_array[$pval['pra_id']] = $this -> get_show_value( $pval );	
			}	
		}
		
		if( $pro_id_val != '' ){
			//查询数据库
			//echo $pro_id_val;
			$sql_prv 	= " SELECT pav_id,pro_id,pra_id,prv_value,prv_price  FROM ".$this -> db -> prefix."pro_att_value WHERE pro_id = ".$pro_id_val." ORDER BY pav_id ASC , pra_id ASC ";
			$prv_detail = $this -> db -> select_info($sql_prv);
			
			//var_dump( $prv_detail );
			//$pra_detail = array();
			unset($prav_arr);
			foreach($prv_detail as $vdkey => $vdval)
			{
					if(!is_null($pra_array[$vdval['pra_id']])){
						$prav_arr[$vdval['pra_id']][$vdkey] = $this -> get_pra_value( $pra_array[$vdval['pra_id']], $vdval  );
						
						if(!empty($prav_arr[$vdval['pra_id']][$vdkey-1]['pra_type']) && $prav_arr[$vdval['pra_id']][$vdkey-1]['pra_type'] == 1)
						{
							$prav_arr[$vdval['pra_id']][$vdkey]['need_del'] = 1;
						}										
					}
			}
			//var_dump($prav_arr);
			
			
			$this -> view -> assign('pra_array',$prav_arr);
		}
		
		if(is_null($prav_arr)){
			
			$this -> view -> assign('pra_array',$pra_array);
		}else{
			$this -> view -> assign('need_arr','arr');
			$this -> view -> assign('pra_array',$prav_arr);
		}

		//header( 'Content-Type:text/html;charset=utf-8 ');
		
		$this -> view -> display('ajax_pro_type.tpl');
	}


	function get_pra_value( $prv_arr = array(), $pra_arr = array() )
	{
			
			//var_dump($prv_arr);
			//echo '<br>';
			$ret_str['show_value'] = "";
			
			if( $prv_arr['pra_input_type']==0 ){ 		//$type = 'input';
				
				$ret_str['show_value'] .= '<input type="input" name="pra_id['.$prv_arr['pra_id'].'][]" id="pra_id['.$prv_arr['pra_id'].'][]" value="'.$pra_arr['prv_value'].'" >';
			}else if( $prv_arr['pra_input_type']==1 ){ //$type = 'select';	
				
					
				$ret_str['show_value'] .= '<select   name="pra_id['.$prv_arr['pra_id'].'][]" id="pra_id['.$prv_arr['pra_id'].'][]">';
				
				if( $this-> lang['txt_select'] !='' ){
					$ret_str['show_value'] .= '<option value="" >'. $this-> lang['txt_select'] .'</option>';  //$this-> lang['txt_select']
				}
				//if($arr_val['pra_values']!=''){
				$val_arr = explode("\n", $prv_arr['pra_values']);
				
				//var_dump($val_arr);
				
					foreach((array)$val_arr as $v_key => $v_val )
					{
						if($v_val!=''){

							if( trim($v_val) == trim($pra_arr['prv_value'])){
								$ret_str['show_value'] .= '<option value="'.$v_val.'" selected="selected" >'.$v_val.'</option>';
							}else{
								$ret_str['show_value'] .= '<option value="'.$v_val.'" >'.$v_val.'</option>';
							}	
						}		
						/*
						if($arr_val['prv_value'] == $v_val){
							$ret_str .= '<option value="'.$v_val.'" >'.$v_val.'</option>';
						}else{
							$ret_str .= '<option value="'.$v_val.'" >'.$v_val.'</option>';
						}*/
					}
				//}
				
				
				$ret_str['show_value'] .= '</select>';
				
			}else if( $prv_arr['pra_input_type']==2 ){ //$type = 'textarea';	
				$ret_str['show_value'] .= '<textarea name="pra_id['.$prv_arr['pra_id'].'][]" id="pra_id['.$prv_arr['pra_id'].'][]" cols="18" rows="6">'.$pra_arr['prv_value'].'</textarea> ';
			}
			
		if( $prv_arr['pra_type'] == 1 ){ 
			if($pra_arr['prv_price']==''){ $pra_arr['prv_price'] = 0 ;  }
			$ret_str['show_price_value'] = '<input type="input" name="pra_id_price['.$prv_arr['pra_id'].'][]" id="pra_id_price['.$prv_arr['pra_id'].'][]" value="'.$pra_arr['prv_price'].'"  size="10" >'; 
		}
		
		$ret_str['pra_id'] 		   =  $prv_arr['pra_id'];
		$ret_str['pra_input_type'] =  $prv_arr['pra_input_type'];
		$ret_str['pra_type'] 	   =  $prv_arr['pra_type'];
		$ret_str['pra_name'] 	   =  $prv_arr['pra_name'];

		return $ret_str;
		
	}
	
	
	function get_show_value( $arr_val = array() )
	{
		
			$ret_str['show_value'] = "";
			
			if( $arr_val['pra_input_type']==0 ){ 		//$type = 'input';
				
				$ret_str['show_value'] .= '<input type="input" name="pra_id['.$arr_val['pra_id'].'][]" id="pra_id['.$arr_val['pra_id'].'][]" value="" >';
			}else if( $arr_val['pra_input_type']==1 ){ //$type = 'select';	
				
					
				$ret_str['show_value'] .= '<select   name="pra_id['.$arr_val['pra_id'].'][]" id="pra_id['.$arr_val['pra_id'].'][]">';
				
				if( $this-> lang['txt_select'] !='' ){
					$ret_str['show_value'] .= '<option value="" >'. $this-> lang['txt_select'] .'</option>';  //$this-> lang['txt_select']
				}
				//if($arr_val['pra_values']!=''){
				$val_arr = explode("\n", $arr_val['pra_values']);
				
				//var_dump($val_arr);
				
					foreach((array)$val_arr as $v_key => $v_val )
					{
						if($v_val!='')
						{
							$ret_str['show_value'] .= '<option value="'.$v_val.'" >'.$v_val.'</option>';		
						}		

					}
				
				$ret_str['show_value'] .= '</select>';
				
			}else if( $arr_val['pra_input_type']==2 ){ //$type = 'textarea';	
				$ret_str['show_value'] .= '<textarea name="pra_id['.$arr_val['pra_id'].'][]" id="pra_id['.$arr_val['pra_id'].'][]" cols="18" rows="6"></textarea> ';
			}
			
		if( $arr_val['pra_type'] == 1 ){ $ret_str['show_price_value'] = '<input type="input" name="pra_id_price['.$arr_val['pra_id'].'][]" id="pra_id_price['.$arr_val['pra_id'].'][]" value="0"  size="10" >'; }
		$ret_str['pra_type'] 		=  $arr_val['pra_type'];
		$ret_str['pra_values'] 		=  $arr_val['pra_values'];
		$ret_str['pra_id'] 			=  $arr_val['pra_id'];
		$ret_str['pra_input_type']  =  $arr_val['pra_input_type'];
		$ret_str['pra_name'] 	    =  $arr_val['pra_name'];

		return $ret_str;
		
	}




}


