<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/MVC/View/views/view.edit.php');

class AOS_ProductsViewEdit extends ViewEdit {
	function AOS_ProductsViewEdit(){
 		parent::ViewEdit();
 	}
	
	function display(){

		global $app_strings,$sugar_config;
		$temp = str_replace($sugar_config['site_url'].'/'.$sugar_config['upload_dir'],"", $this->bean->product_image);
	        $html = '<span id=\'new_attachment\' style=\'display:';
		if(!empty($this->bean->product_image)){
		$html .= 'none';
		}
        	$html .= '\'><input name="uploadimage" tabindex="3" type="file" size="60"/>
        	</span>
		<span id=\'old_attachment\' style=\'display:';
		if (empty($this->bean->product_image)){
		$html .= 'none';
		}
		$html .= '\'><input type=\'hidden\' id=\'deleteAttachment\' name=\'deleteAttachment\' value=\'0\'>
		'.$temp.'<input type=\'hidden\' name=\'old_product_image\' value=\''.$this->bean->product_image.'\'/><input type=\'hidden\' name=\'old_id\' value=\'{'.$fields.id.value.'}\'/>
		<input type=\'button\' class=\'button\' value=\''.$app_strings['LBL_REMOVE'].'\' onclick=\'deleteProductImage();\' >
		</span>';
		
		$this->ss->assign('PRODUCT_IMAGE',$html);
                $this->populateLineItems();
		parent::display();
                $this->displayJS();
	}
        
        function populateLineItems(){
         
            global $mod_strings,$app_strings;        
		
            $json = getJSONobj();
            
            $sql="          SELECT prki.product_children ,prki.name,prki.id 
                              FROM gea_products_kits prki
                             WHERE prki.product_parent = '".$this->bean->id."'  
                               AND prki.deleted = 0 ";
		

           
           
            $result = $this->bean->db->query($sql);
            
           
            
            $html = "";
            $html .= '<script language="javascript">setModule("AOS_Products");</script>';
		
            $html .= "<table border='0' cellspacing='4'width='37.5%' id='itemLine'>";
					
            $html .= "<tr id='itemHeader' style='display: none'>";
            $html .= "<td width='15%' class='dataLabel' style='text-align: left;' colspan='2'>".$mod_strings['LBL_NAME']."</td>";
            $html .= "<td width='10%' class='dataLabel' style='text-align: left;'>&nbsp;</td>";
            $html .= "</tr>";
            
            $i = 0;
            while ($row = $this->bean->db->fetchByAssoc($result)) {
		
              
                if($i == 0){
                    $html .= '<script language="javascript">showItemHeader(true);</script>';
                }

                $sqs_objects = array('item_name['.$i.']' =>
                    array(
                            'form' => 'EditView',
                            'method' => 'query',
                            'modules' => array('AOS_Products'),
                            'group' => 'or',
                            'field_list' => array('name', 'id'),
                            'populate_list' => array('item_name['.$i.']', 'item_id['.$i.']'),
                            'required_list' => array('item_id['.$i.']'),
                            'conditions' => array(array('name'=>'name','op'=>'like_custom','end'=>'%','value'=>'')),
                            'order' => 'name',
                            'limit' => '30',
                            'no_match_text' => $app_strings['ERR_SQS_NO_MATCH']
                       )
                );

               $quicksearch_js = '<script language="javascript">';
               $quicksearch_js.= 'if(typeof sqs_objects == \'undefined\'){var sqs_objects = new Array;} ;';
               foreach($sqs_objects as $sqsfield=>$sqsfieldArray){
                    $quicksearch_js .= "sqs_objects['$sqsfield']={$json->encode($sqsfieldArray)};";
               }
              

               $html .= $quicksearch_js . '</script>';
               $row['id'] = (isset($_REQUEST['Duplicate']) && trim($_REQUEST['Duplicate']) == 'Duplicate')?"":$row['id'];

               $html .= "<tr id='item_line$i'>";
               $html .= "<td class='dataField'><input class='sqsEnabled' autocomplete='on' tabindex='116' type='text' name='item_name[$i]' id='item_name$i' size='16' maxlength='50' value='".$row['name']."' title=''><input type='hidden' name='item_id[$i]' id='item_id$i' value='".$row['product_children']."'></td>";
               $html .= "<td class='dataField'><button title='".$app_strings['LBL_SELECT_BUTTON_TITLE']."' accessKey='".$app_strings['LBL_SELECT_BUTTON_KEY']."' type='button' tabindex='116' class='button' value='".$app_strings['LBL_SELECT_BUTTON_LABEL']."' name='btn1' onclick='openItemPopup($i)'><img src='themes/default/images/id-ff-select.png' alt='".$app_strings['LBL_SELECT_BUTTON_LABEL']."'></button></td>";
               $html .= "<td class='dataField'><input type='hidden' name='item_deleted[]' id='item_deleted$i' value='0'><input type='hidden' name='itemKit_id[]' value='".$row['id']."'><button type='button' class='button' value='".$mod_strings['LBL_REMOVE_PRODUCT_LINE']."' tabindex='116' onclick='markItemLineDeleted($i)'><img src='themes/default/images/id-ff-clear.png' alt='".$mod_strings['LBL_REMOVE_PRODUCT_LINE']."'></button></td>";
               $html .= "</tr>";               
               $i++;
			
            }
            $html .= '<script language="javascript">itemLine = '.$i.';</script>';
            $html .= "</table>";
            $html .= "<div style='padding-top: 10px; padding-bottom:10px;'>";
            $html .= "<input type=\"button\" tabindex=\"116\" class=\"button\" value=\"".$mod_strings['LBL_ADD_PRODUCT_KIT']."\" id=\"addItemLine\" onclick=\"insertItemLine(".$i.")\" />";
            $html .= "</div>";
            
            $this->ss->assign('LINE_ITEMS',$html);
		
            
        }
        
        function displayJS(){
            global $app_strings, $mod_strings;
            echo "
			<script type=\"text/javascript\">
				var selectButtonTitle = '". $app_strings['LBL_SELECT_BUTTON_TITLE'] . "';
				var selectButtonKey	  = '". $app_strings['LBL_SELECT_BUTTON_KEY'] . "';
				var selectButtonValue = '". $app_strings['LBL_SELECT_BUTTON_LABEL'] . "';
				var deleteButtonValue = '". $mod_strings['LBL_REMOVE_PRODUCT_LINE'] . "';";
		
		$js = <<<JS
			if(typeof sqs_objects == 'undefined'){var sqs_objects = new Array;}
	
JS;
        
		echo $js;				
		echo "</script>";
	}
}
?>
