 <?php $__env->startSection('content'); ?>
    <div class="common-content">
        <div class="page-content">
            <div class="page-heading">
                <div class="headings">
                    <h6>Pre-Requisites Order</h6>
                </div>
            </div>
            <div class="boxings">
                <?php if(count($preorder) != 0): ?>
                    <?php $__currentLoopData = $preorder; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $summary): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="purchase-order pre-order">
                            <div class="order-id d-flex justify-content-end p-3">
                                <h4>Order ID <span class="green-order"><?php echo e($summary->po_id); ?></span></h4>
                            </div>
                            <div class="product_details-grd custom">
                                <div class="product-image custom pre-product-image">
                                    <?php if(!empty($summary->photo)): ?>
                                        <img src="<?php echo e(url('product_files')); ?>/<?php echo e($summary->photo); ?>">
                                    <?php else: ?>
                                        <img src="<?php echo e(url('assets/images/1200px-No-Image-Placeholder.svg.png')); ?>">
                                    <?php endif; ?>
                                </div>
                                <div class="product-details custom">
                                    <ul>
                                        <li>
                                            <h4 class="product-name"><?php echo e(ucfirst($summary->product_name)); ?></h4>
                                        </li>
                                    </ul>
                                    <ul class="">
                                        
                                        <li class="pre-list"><?php echo e(ucfirst($summary->prerequites_items)); ?></li>
                                        <li class="lab-pre">Tentative date:
                                        <?php if($summary->change_date_status==2): ?>
                                        <?php echo e($summary->change_date); ?>

                                        <?php
                                        $delivery_date = $summary->change_date;
                                        $current_date = date('d-m-Y');
                                        ?>
                                        <?php
                                        $datediff = App\Helpers\CustomHelper::dateDiff($current_date, $delivery_date);
                                        ?>
                                        <?php else: ?>
                                        <?php echo e($summary->delivery_date); ?></li>

                                        
                                        <?php
                                        $delivery_date = $summary->delivery_date;
                                        $current_date = date('d-m-Y');
                                        ?>
                                        <?php
                                       
                                        $datediff = App\Helpers\CustomHelper::dateDiff($current_date, $delivery_date);
                                        ?>
                                        <?php endif; ?>
                                        
                                       
                                       
                                        <?php if($datediff->invert == 0): ?>
                                            <li class="remaining">Remaining <?php echo e($datediff->days); ?> days</li>
                                        <?php else: ?>
                                            <li class="remaining">Expired</li>
                                        <?php endif; ?>
                                    </ul>
                                     <?php
                              $preRequisites = App\Helpers\CustomHelper::getPreRequisites($summary->id);
                              // var_dump($preRequisites); die();
                            ?>
                            <?php if($preRequisites != false): ?>
                            <h5>Pre-Requisites items</h5>
                              <div class="d-flex justify-content">
                                <ul class="Pre-Requisites-list">
                                  <?php $__currentLoopData = $preRequisites; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <li>
                                      <div class="form-check">
                                        <input class="form-check-input prerequisite<?php echo e($summary->id); ?>" type="checkbox" name="updatePrerequisites" data-orderid = '<?php echo e($summary->id); ?>' data-requisitesid = '<?php echo e($value->id); ?>' id="prerequisite<?php echo e($value->id); ?>" <?php if($value->status == 1): ?> checked disabled <?php endif; ?>>
                                        <label class="form-check-label" for="flexCheckDefault">
                                            <?php echo e($value->items); ?>

                                        </label>
                                      </div>
                                    </li>
                                  <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                  <li>
                                    
                                   
                                    <?php if($summary->requites_ready == true): ?>
                                      <button type="button" onclick="remindPrerequisite('<?php echo e($summary->id); ?>')" class="btn btn-primary">All pre-requisites items are ready</button>
                                      <?php else: ?>
                                    <button type="button" onclick="updatePrerequisiteStatus('<?php echo e($summary->id); ?>')"   disabled class="btn btn-primary" id="updatePrerequisiteStatusBtn<?php echo e($summary->id); ?>">Ready</button>
                                    

                                    <?php endif; ?>
                                  </li>
                                </ul>
                              </div>
                            <?php endif; ?>
                                </div>
                              </div>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    <?php echo e($preorder->links()); ?>

                <?php else: ?>
                                </div>
                            </div>
                           
                    <div class="col-lg-12 col-md-6 col-sm-12 col-xs-12">
                        <div class="product-display">
                            <ul>
                                <li>
                                    <div class="details">
                                        <div>
                                            <h6>
                                                No Datas available
                                            </h6>
                                        </div>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div> <?php $__env->stopSection(); ?> <?php $__env->startSection('scripts'); ?>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
        <script src="https://cdn.ckeditor.com/4.5.1/standard/ckeditor.js"></script>
        <script src="<?php echo e(URL::to('assets/js/buyernew.js')); ?>"></script>
        <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
        <script type="text/javascript">
            <?php if(Session::has('message')): ?>
                swal({
                    title: "",
                    text: "<?php echo e(Session::get('message')); ?>",
                    type: "success",
                    showCancelButton: false,
                    dangerMode: false,
                    confirmButtonText: 'OK',
                });
            <?php endif; ?>
            // Replace the <textarea id="editor1"> with a CKEditor
            // instance, using default configuration.

            // function checkAll(orderId) {
            //   $('.updatePrerequisites'+orderId).prop('checked', true);
            //   $('#checkAll'+orderId).hide();
            //   $('#unCheckAll'+orderId).show();
            //   $('#updatePrerequisiteStatus'+orderId).show();
            // }

            // function unCheckAll(orderId) {
            //   $('.updatePrerequisites'+orderId).prop('checked', false);
            //   $('#unCheckAll'+orderId).hide();
            //   $('#checkAll'+orderId).show();
            //   $('#updatePrerequisiteStatus'+orderId).hide();
            // }

            // function updatePrerequisiteStatus(dataOrderId) {
            //   Swal.fire({
            //     title: 'Are you sure?',
            //     text: "You won't be able to revert this!",
            //     icon: 'warning',
            //     showCancelButton: true,
            //     confirmButtonColor: '#3085d6',
            //     cancelButtonColor: '#d33',
            //     confirmButtonText: 'Yes, pre-requisites items are ready!'
            //   }).then((result) => {
            //     if (result.isConfirmed) {
            //       $.ajax({
            //         type: "POST", 
            //         dataType: "json", 
            //         url: "<?php echo e(url('buyers/update-pre-requisites')); ?>",
            //         data: {
            //           orderId : dataOrderId,
            //           status : true,
            //           _token: '<?php echo e(csrf_token()); ?>'
            //         },
            //         success: function(response) {
            //           $('#updatePrerequisiteStatusBtn'+dataOrderId).hide();
            //           $('.prerequisite' + dataOrderId + ':checkbox:checked').attr("disabled", true);
            //           Swal.fire(
            //             'Done!',
            //             'The pre-requisites items are updated ready.',
            //             'success'
            //           )
            //         }
            //       });
            //     }
            //   })
            // }

            var orderId = '';
            var preRequisites = [];
            var mainPreRequisitesArray = [];
            // console.log('preRequisites initial');
            // console.log(preRequisites);

            $(document).ready(function () {


             



              $('input[name="updatePrerequisites"]').change(function() {
                var requisitesId = $(this).data('requisitesid');

                
               
                orderId = $(this).data('orderid');
                $('#updatePrerequisiteStatusBtn' + orderId).prop('disabled', false);
                $('#updatePrerequisiteStatusBtn'+orderId).show();

                if (mainPreRequisitesArray[orderId] !== undefined) {
                  preRequisites = mainPreRequisitesArray[orderId];
                } else {
                  preRequisites = [];
                }
             
                if($('input#prerequisite'+requisitesId).is(":not(:checked)")) {
                  preRequisites = jQuery.grep(preRequisites, function(value) {
                    return value != requisitesId;
                  });
                } else {
                  preRequisites.push(requisitesId);
                }
                mainPreRequisitesArray[orderId] = preRequisites;

                if (mainPreRequisitesArray[orderId].length === 0) {
                  $('#updatePrerequisiteStatusBtn' + orderId).prop('disabled', true);
                } else {
                  $('#updatePrerequisiteStatusBtn' + orderId).prop('disabled', false);
                }

                 
              });


            });

            // function insertPrerequisite() {
            //   orderId = $(this).data('orderid');
            // }

            function updatePrerequisiteStatus(po_product_id){

              $(".pageloader").show();
              $.ajax({
                type: "POST", 
                dataType: "json", 
                url: "<?php echo e(url('buyers/update-pre-requisites')); ?>",
                data: {
                  po_product_id : po_product_id,
                  requisitesId : mainPreRequisitesArray[po_product_id],
                  status : true,
                  _token: '<?php echo e(csrf_token()); ?>'
                },
                success: function(response) {
                  $(".pageloader").hide();
                  mainPreRequisitesArray[po_product_id].forEach(function(item) {
                    $('#updatePrerequisiteStatusBtn'+po_product_id).hide();
                    $('#updatePrerequisiteStatusBtn' + orderId).prop('disabled', true);
                    $('#prerequisite' + item + ':checkbox:checked').attr("disabled", true);
                  });
                  Swal.fire(
                    'Done!',
                    'The pre-requisites items are updated ready.',
                    'success'
                  )
                }
              });
            }
            function remindPrerequisite(po_product_id) {

              $(".pageloader").show();
              $.ajax({
                type: "POST", 
                dataType: "json", 
                url: "<?php echo e(url('buyers/remind-pre-requisites')); ?>",
                data: {
                  po_product_id : po_product_id,
                  _token: '<?php echo e(csrf_token()); ?>'
                },
                success: function(response) {
                  $(".pageloader").hide();
                  Swal.fire(
                    'Done!',
                    'The store has been reminded again all pre-requisites are ready',
                    'success'
                  )
                }
              });
            }
        </script>
    <?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.main', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/meddbot/public_html/resources/views/buyers/prerequisites.blade.php ENDPATH**/ ?>