<style>
table, td, th {
  border: 1px solid black;
}

table {
  width: 100%;
  border-collapse: collapse;
}
</style>


<?php
echo form_open('employees/save_permission/'.$person_info->person_id,array('id'=>'permission_form'));
?>
<fieldset id="employee_login_info" style="width:100%;">
<div class="field_row clearfix"  style="width:100%;">	
<table  style="width:100%;">
  <tr>
    <th>Item Code</th>
    <th >Item Description</th>
    <th>LOT NO</th>
    <th>Sale Price</th>
    <th>Sale QTY</th>
    <th>Total Sale Amount</th>
    <th>Current Discount(%)</th>
    <th>Reset Discount(%)</th>
    <th>LOT FOUND</th>
  </tr>

</tr>

<?php 
foreach ($sale_details as $key => $value) {
	$reset_index=$value['sale_id']."_".$value['line']."_".$value['stock_id']."_".$value['lot_no'];


	$live_data_str=stream_get_contents(fopen("http://office.grameenshamogree.com/services/includes/product_request.php?stock_id=".$value['stock_id']."&item_cost_price=".intval($value['item_cost_price']), "r"));

	$live_data_array=array();

	$live_data_array=explode("#",$live_data_str);
	
	$lot_found=$value['lot_no'];
	
	if(trim($live_data_array[0]) AND trim($live_data_array[0])!="NF"){
		$lot_found=$live_data_array[0];
	}

	$bg_color="";
	if(intval($value['sync_permission'])>0){
		$bg_color="background-color:#FFA5A5;";
	}else{

		$query = $this->db->query("
			SELECT st.description AS item_name,st.category_id,st.gender,cat.description AS cat_description
			FROM stock_master AS st
			LEFT JOIN stock_category AS cat on st.category_id=cat.category_id
			WHERE st.stock_id='".$value['stock_id']."'");


		$sales_info=array();
		foreach ($query->result() as $row) {
			$sales_info=$row;
		}

		$query = $this->db->query("SELECT *
			FROM pcr_check_data
			WHERE TRIM(item_name)='".trim($sales_info->item_name)."' AND TRIM(gender)='".trim($sales_info->gender)."' AND TRIM(category_name)='".trim($sales_info->cat_description)."' AND loc_code='".LOCATION_CODE."'
			GROUP BY item_name,category_name,gender");

		foreach ($query->result() as $row_found) {
			if(intval($value['discount_percent'])==0){
				$bg_color="background-color:#FFF450;";
			}
		}
	}

	$sync_permission_percentage=0;

	if(intval($value['sync_permission'])){
		$sync_permission_percentage=(intval($value['sync_permission'])+intval($value['discount_percent']));
	}

	?>
<tr>
<td style="text-align:center;<?php echo $bg_color;?>"> <?php echo $value['stock_id']; ?></td>
<td style="text-align:left;<?php echo $bg_color;?>"> <?php echo $value['description']; ?></td>
<td  style="text-align:center;<?php echo $bg_color;?>" > <?php echo $value['lot_no']; ?></td>
<td  style="text-align:right;<?php echo $bg_color;?>"> <?php echo number_format($value['item_cost_price'],2,'.',',');?></td>
<td  style="text-align:center;<?php echo $bg_color;?>"> <?php echo $value['quantity_purchased']; ?></td>
<td  style="text-align:right;<?php echo $bg_color;?>"> <?php echo number_format($value['item_cost_price']*$value['quantity_purchased'],2,'.',','); ?></td>
<td  style="text-align:center;<?php echo $bg_color;?>"> <?php echo $value['discount_percent']; ?></td>
<td  style="text-align:center;<?php echo $bg_color;?>"> 

<?php echo form_input(array(
'name'=>$reset_index,
'id'=>$reset_index,
'size'=>'3',
		'value'=>$sync_permission_percentage));?>
</td>
<td  style="text-align:center;<?php echo $bg_color;?>"> <?php echo $lot_found; ?></td>




</tr>	
<?php echo form_hidden($reset_index."_previous",$value['discount_percent']); ?>
<?php	
}
?>
</table>
<table style="border: 0px solid black;background-color:#79CCD5">
<tr>

<td  style="text-align:center;" colspan="9">
<?php
echo form_submit(array(
'name'=>'submit',
'id'=>'submit',
'value'=>$this->lang->line('common_submit'),
'class'=>'submit_button float_center')
);

?>
</td>
</tr>

</table>
</div>
</fieldset>

<?php 
echo form_close();
?>
<script type='text/javascript'>

//validation and submit handling
$(document).ready(function()
{
	$('#permission_form').validate({
		submitHandler:function(form)
		{
			$(form).ajaxSubmit({
				success:function(response)
				{
					tb_remove();
					post_person_form_submit(response);
				},
				dataType:'json'
			});

		},
		errorLabelContainer: "#error_message_box",
		wrapper: "li",
		rules:
		{
			password:
			{
				required:false,
				//minlength: 8
			}
		},
		messages:
		{
			username:
			{
				required: "<?php echo $this->lang->line('employees_username_required'); ?>",
				minlength: "<?php echo $this->lang->line('employees_username_minlength'); ?>"
			},

			old_password:
			{
				required:"<?php echo $this->lang->line('employees_old_password_required'); ?>",
			},
			password:
			{
				required:"<?php echo $this->lang->line('employees_password_required'); ?>",
				minlength: "<?php echo $this->lang->line('employees_password_minlength'); ?>"
			},
			repeat_password:
			{
				equalTo: "<?php echo $this->lang->line('employees_password_must_match'); ?>"
			}
		}
	});
});
</script>