  <?php $__env->startSection('content'); ?>
 <?php Fun::lang() ?>
 <div class="title-page">
     <div class="page-bar">
         <ul class="page-breadcrumb">
             <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
             <li><a href="#">Manage Stock</a></li>
             <li><a href="#">Update Commission</a></li>
         </ul>
     </div>
 </div>

 <div class="page">
     <div class="top-content">
         <div class="row">
             <div class="col-sm-12 col-xs-12">
                 <div class="pull-left">
                     <h3>Update Commission</h3>
                 </div>
                 <div class="pull-right">
                     <a class="button_sm button_primary" href="<?php echo e(route('items.index')); ?>"><span class="fa fa-reply"></span> Back</a>
                 </div>
             </div>
         </div>
     </div>
     <div class="row">
         <div class="col-sm-12 col-xs-12">

             <div class="form-wrapper">
                 <?php echo Form::open(array('route' => 'items.update_commission','method'=>'POST','enctype'=>'multipart/form-data')); ?>

                 <?php echo csrf_field(); ?>
                 <div class="row">
                     <div class="form-group col-sm-3 col-xs-12">
                         <label class="required">Category</label>
                         <?php echo Form::select('item_categoryid', $categories, null, ['class' => 'form-control category_id select2 change_group_code selectBox','id'=>'category_id']); ?>

                     </div>
                     <div class="form-group col-sm-3 col-xs-12">
                         <label class="required">Project</label>
                         <?php echo Form::select('item_projectid', $projects, null, ['class' => 'form-control change_group_code select2 selectBox','required','id'=>'project_id']); ?>

                     </div>
                     <div class="form-group col-sm-3 col-xs-12">

                         <label>Group Item</label>
                         <?php echo Form::select('item_groupid', $item_group, null, ['class' => 'form-control group_id project_id_comm select2 selectBox','id'=>'group_id']); ?>


                     </div>
                     <div class="form-group col-sm-3 col-xs-12">

                         <label class="required">Item Zone</label>
                         <?php echo Form::select('item_zone_id', $item_zone, null, ['class' => 'form-control zone_id select2 project_id_comm selectBox', 'autocomplete' => 'off','id'=>'zone_id']); ?>


                     </div>
                     <div class="form-group col-sm-3 col-xs-12">

                         <label class="">item</label>
                         <?php echo Form::select('item_name[]', $items, null, array('class' => 'form-control item_id input-sm select2', 'multiple'=>'multiple', 'autocomplete' => 'off','id'=>'item_id' )); ?>


                     </div>

                     <div class="form-group col-sm-3 col-xs-12">

                         <label class="">Commission type</label>
                         <?php echo Form::select('commission_type', array('1'=>'Amount($)','2'=>" Percentage(%)"), null, ['class' => 'form-control comm_type select2','id'=>'commission_type']); ?>


                     </div>
                     <div class="form-group col-sm-3 col-xs-12 commission_amount_con">

                         <label class="commission_amount_con">Commission Amount($)</label>
                         <?php echo Form::text('amount_commission', 0, array('placeholder' => 'commission amount','class' => 'form-control commission_amount commission_amount_con number-only','')); ?>


                     </div>
                     <div class="form-group col-sm-3 col-xs-12 commission_con">

                         <label class="commission_con">Commission(%)(/sale price)</label>
                         <?php echo Form::text('commission_rate', 0, array('placeholder' => 'commission percentage','class' => 'form-control commission commission_con number-only','')); ?>


                     </div>

                 </div>
                 <div class="form-group col-sm-12 col-xs-12">
                     <button type="button" class="selectBox btn btn-warning btn-sm fa fa-arrow-right pull-right"> Generate</button>
                 </div>
                 <table id="datatable_item_price" class="table table-striped table-bordered table-hover nowrap">
                     <thead>
                         <tr>
                             <th>NO</th>
                             <th>Project Name</th>
                             <th>Category Name</th>
                             <th>Group</th>
                             <th>Block</th>
                             <th>Item Name</th>
                             <th>Old Commission</th>
                             <th>New Commission</th>
                         </tr>
                     </thead>
                     <tbody class="tbody">

                     </tbody>
                 </table>
                 <div class="row">
                     <div class="col-sm-12 col-xs-12">
                         <div class="form-group">
                             <button type="submit" class="button_primary pull-right"><i class="fa fa-edit"></i> update</button>
                         </div>
                     </div>
                 </div>
                 <?php echo Form::close(); ?>

             </div>
         </div>
     </div>
 </div>
 <?php $__env->stopSection(); ?>
 <?php $__env->startSection('js'); ?>
 <script type="text/javascript">
    var fix = null;
     $(document).ready(function() {
         $(".commission_con").hide();
     });
     $(document).on('change', '.selectBox', function() {
         var projectID = $('#project_id').find(':selected').val();
         var categoryID = $('#category_id').find(':selected').val();
         var zoneID = $('#zone_id').find(':selected').val();
         var groupID = $('#group_id').find(':selected').val();
         var newAmount = $('#new_price').val();
         var id = $(this).attr('id');
         var tr = '';
         var type = parseInt($('#commission_type').val());
         if (projectID != "" && categoryID != "") {
             $.ajax({
                 type: "POST",
                 dataType: "JSON",
                 url: "<?php echo e(route('filter-item-properties')); ?>",
                 data: {
                     projectID: projectID,
                     categoryID: categoryID,
                     groupID: groupID,
                     zoneID: zoneID,
                     _token: "<?php echo e(csrf_token()); ?>"
                 },
                 success: function(response) {
                     var zone = response['zone'];
                     var group = response['group'];
                     var item = response['item'];
                     fix = item;
                     if (id != "zone_id" && id != "group_id") {
                         $('#zone_id,#group_id').empty().append(`<option value="">Please Select</option>`);
                         $.each(zone, function(i, z) {
                             $('#zone_id').append(`<option value="${z.id}">${z.item_zone_name}</option>`);
                         });
                         $.each(group, function(i, g) {
                             $('#group_id').append(`<option value="${g.id}">${g.item_group_name}</option>`);
                         });
                     }
                     $('#item_id').empty();
                     $.each(item, function(i, it) {
                         var td = '';
                         var newCommission = 0;
                         $('#item_id').append(`<option value="${it.id}">${it.item_house}</option>`);
                         var commission = 0;
                         if (!(it.commission_amount) || it.commission_amount == 0) {
                             commission = parseFloat(it.commission).toFixed(2) + " %";
                         } else {
                             commission = "$ " + parseFloat(it.commission_amount).toFixed(2);
                         }
                         if (type == 1) {
                             newCommission = parseFloat($('.commission_amount').val());
                             td = `<td><input class="form-control number-only" type="text" value="${newCommission}" name="newCom_amount[]" required></td>`;
                         } else {
                             newCommission = parseFloat($('.commission').val());
                             td = `<td><input class="form-control number-only" type="text" value="${newCommission}" name="newCom_percent[]" required></td>`;
                         }
                     });
                 }
             });
         }
     });
     $(document).on('click', '.selectBox', function() {
         var projectID = $('#project_id').find(':selected').val();
         var categoryID = $('#category_id').find(':selected').val();
         var zoneID = $('#zone_id').find(':selected').val();
         var groupID = $('#group_id').find(':selected').val();
         var newAmount = $('#new_price').val();
         var itemID = $('#item_id').val();
         var id = $(this).attr('id');
         var tr = '';
         var type = parseInt($('#commission_type').val());
         if (projectID != "" && categoryID != "") {
             $.ajax({
                 type: "POST",
                 dataType: "JSON",
                 url: "<?php echo e(route('filter-item-properties')); ?>",
                 data: {
                     projectID: projectID,
                     categoryID: categoryID,
                     groupID: groupID,
                     zoneID: zoneID,
                     itemID: itemID,
                     _token: "<?php echo e(csrf_token()); ?>"
                 },
                 success: function(response) {
                     var zone = response['zone'];
                     var group = response['group'];
                     var item = response['item'];

                     $.each(item, function(i, it) {
                         var td = '';
                         var newCommission = 0;
                         var commission = 0;
                         if (!(it.commission_amount) || it.commission_amount == 0) {
                             commission = parseFloat(it.commission).toFixed(2) + " %";
                         } else {
                             commission = "$ " + parseFloat(it.commission_amount).toFixed(2);
                         }
                         if (type == 1) {
                             newCommission = parseFloat($('.commission_amount').val());
                             td = `<td><input class="form-control number-only" type="text" value="${newCommission}" name="newCom_amount[]" required></td>`;
                         } else {
                             newCommission = parseFloat($('.commission').val());
                             td = `<td><input class="form-control number-only" type="text" value="${newCommission}" name="newCom_percent[]" required></td>`;
                         }
                         tr += `<tr>
                                    <td><input class="form-control" type="text" value="${(i+1)}" readonly></td>
                                    <td><input class="form-control" type="text" value="${it.project_name}" readonly></td>
                                    <td><input class="form-control" type="text" value="${it.category_name}" readonly></td>
                                    <td><input class="form-control" type="text" value="${it.group_name}" readonly></td>
                                    <td><input class="form-control" type="text" value="${it.zone_name}" readonly></td>
                                    <td><input type="hidden" name="item_id[]"   value="${it.id}"><input class="form-control" type="text" value="${it.item_house}" readonly></td>
                                    <td><input class="form-control" type="text" value="${commission}" readonly></td>` +
                             td +
                             `</tr>`;
                     });
                     $('.tbody').empty().append(tr);
                 }
             });
         }
     });
     $(document).on("change", ".comm_type", function() {
         var type = 1;
         type = $(this).val();
         if (type == 1) {
             $(".commission_amount_con").val(0).show();
             $(".commission_con").val(0).hide();
         } else {
             $(".commission_amount_con").val(0).hide();
             $(".commission_con").val(0).show();
         }
     });
 </script>
 <?php $__env->stopSection(); ?>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>