<table class="table table-condensed table-bordered">

	<thead><tr bgcolor="#FFCC00"><th>S.No.</th><th>Packing</th><th>Pieces</th><th>MRP</th><th>Unit Rate</th><th>Case Rate</th><th>Action</th></tr></thead>

    <tbody>

    	<?php $i=1;foreach($pvars as $pv){?>

        <tr>

            <input type="hidden" id="pvid<?php echo $pv->pvid;?>" value="<?php echo $pv->pvid;?>" />

        	<th><?php echo $i;?></th>

        	<th><input type="text" class="form-control" id="pptype<?php echo $pv->pvid;?>" value="<?php echo $pv->pptype;?>" required /></th>


        	<th><input type="text" class="form-control" id="ppieces<?php echo $pv->pvid;?>" value="<?php echo $pv->ppieces;?>" /></th>
        	<th><input type="text" class="form-control" id="pmrp<?php echo $pv->pvid;?>" value="<?php echo $pv->pmrp;?>" required /></th>
        	<th><input type="text" class="form-control" id="unit_rate<?php echo $pv->pvid;?>" value="<?php echo $pv->unit_rate;?>" /></th>
        	<th><input type="text" class="form-control" id="case_rate<?php echo $pv->pvid;?>" value="<?php echo $pv->case_rate;?>" /></th>

            <th>

            <button type="submit" class="btn btn-sm btn-success submitform" id="<?php echo $pv->pvid;?>"><i class="fa fa-check"></i></button>

            <button type="button" id="<?php echo $pv->pvid;?>" class="btn btn-sm btn-danger delete_variant"><i class="fa fa-times"></i></button>

            </th>

        </tr>

        <?php $i++;}?>

        <tr>

            

            <td><?php echo $i;?></td>

            <td><input type="text" id='pptype'  placeholder='1 kg' class="form-control"/></td>

            <td><input type="text" id='ppieces' placeholder='80' class="form-control"/></td>
            <td><input type="text" id='pmrp' placeholder='100' class="form-control"/></td>

            <td><input type="text" id='unit_rate' placeholder='80' class="form-control"/></td>
            <td><input type="text" id='case_rate' placeholder='80' class="form-control"/></td>

            <td><button type="submit" class="btn btn-sm btn-success save_new" id="<?php echo $pid;?>"><i class="fa fa-check"></i></button></td>

			

        </tr>

    </tbody>

</table>

<script>

$(document).ready(function(){ 

	$(".delete_variant").click(function(e){e.preventDefault();

		if (!confirm("Sure you want to delete?")){

			  return false;

			}

		var id=$(this).attr("id");

		$(this).closest("tr").remove();

		$.ajax({

			 type: "POST",

			 url: '<?php echo base_url();?>admin/variant_delete',

			 data: {id:id},

			 success: function(res){

				// alert(res);

			 }

		});

	});

	$(".submitform").click(function(){

		var id=$(this).attr("id");

		var pvid=$("#pvid"+id).val();

		var pptype=$("#pptype"+id).val();

		var pmrp=$("#pmrp"+id).val();

		var ppieces=$("#ppieces"+id).val();
		var unit_rate=$("#unit_rate"+id).val();
		var case_rate=$("#case_rate"+id).val();

		$.ajax({

			 type: "POST",

			 url: '<?php echo base_url();?>admin/ajax_update_form',

			 data: {id:id,pvid:pvid,pptype:pptype,pmrp:pmrp,ppieces:ppieces,unit_rate:unit_rate,case_rate:case_rate},

			 success: function(response){

				 alert(response);

			 }

		});

		//var 

	});

	$(".save_new").click(function(){

		var pid=$(this).attr("id");

		var pptype=$("#pptype").val();

		var pmrp=$("#pmrp").val();

		var ppieces=$("#ppieces").val();
		var unit_rate=$("#unit_rate").val();
		var case_rate=$("#case_rate").val();

		$.ajax({

			 type: "POST",

			 url: '<?php echo base_url();?>admin/ajax_add_form',

			 data: {pid:pid,pptype:pptype,pmrp:pmrp,ppieces:ppieces,case_rate:case_rate,unit_rate:unit_rate},

			 success: function(response){

				 reload();

			 }

		});

		//var 

	});

	function reload(){

		$.ajax({

			 type: "POST",

			 url: '<?php echo base_url();?>admin/ajax_pvariants',

			 data: {pid:<?php echo $pid;?>},

			 success: function(response){

				 //alert(response);

				 $(".modal-body").html(response);

			 }

		});	

	}

	//  $(".submitform").click(function(e){e.preventDefault();

	//		var formid=$(this).attr("id");

	//		var formdata=$("#form"+formid).serialize();

	//		alert(formdata);

	//	});

	//	$("#save_new").submit(function(e){e.preventDefault();

	//		var formdata=$(this).serialize();

	//		alert(formdata);

	//	});

});

</script>