<?php $this->view("includes/Inc_pagehead");?>

<!-- Page Styles Goes Here-->
<style>
.m-form .m-form__group {
  padding-top: 0px;
  padding-bottom: 0px;
}
</style>
<?php $this->view("includes/Inc_navigation");?>
<div class="m-grid__item m-grid__item--fluid m-wrapper">
  <!-- BEGIN: Subheader -->
  <div class="m-subheader ">
    <div class="d-flex align-items-center">
      <div class="mr-auto">
        <h3 class="m-subheader__title m-subheader__title--separator">
          Add New Delivery Challan
        </h3>
        <ul class="m-subheader__breadcrumbs m-nav m-nav--inline">
          <li class="m-nav__item m-nav__item--home">
            <a href="#" class="m-nav__link m-nav__link--icon">
              <i class="m-nav__link-icon la la-home"></i>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'dashboard'?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Home
              </span>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'purchase_order/view' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Add New Delivery Challan
              </span>
            </a>
          </li>


        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <form autocomplete="off" id="form_order_add" data-form-type="order_create" method="post" action="<?php echo base_url()?>purchases/create_delivery_challan" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
      <div class="m-portlet m-portlet--full-height">
        <div class="m-portlet__head">
          <div class="m-portlet__head-caption">
            <div class="m-portlet__head-title">
              <h3 class="m-portlet__head-text">
                Add New Delivery Challan <?php if($purchase_order_id!=''){ echo ',    For Purchase Order ID : #'.$purchase_order_id;}?>
                </h3>
              </div>
            </div>
            <div class="m-portlet__head-tools">

            </div>
          </div>
          <div class="row">
            <div class="col-md-12">
              <div class="modal-body">
                <div class="m-form__content">
                  <div class="m-alert m-alert--icon alert alert-warning m--hide" role="alert" id="form_order_add_msg">
                    <div class="m-alert__icon">
                      <i class="la la-warning"></i>
                    </div>
                    <div class="m-alert__text">
                      Oh snap! Change a few things up and try submitting again.
                    </div>
                    <div class="m-alert__close">
                      <button type="button" class="close" data-close="alert" aria-label="Close"></button>
                    </div>
                  </div>

                  <div class="row">
                    <div class="col-lg-3">
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Ordered to
                        </label>
                        <div class="input-group m-input-group--pill m-input-group--air date">
                          <input disabled type="text" class="form-control m-input" value="<?php echo $purchase_order_data->location_name?>" >
                        </div>
                        <input  type="hidden" name="order_id" value="<?php echo $purchase_order_data->order_id?>" >
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-3">
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Supplier Name
                        </label>
                        <div class="input-group m-input-group--pill m-input-group--air date">
                          <input disabled type="text" class="form-control m-input" value="<?php echo $purchase_order_data->supplier_name?>" >
                        </div>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-2">
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Order Date
                        </label>
                        <div class="input-group m-input-group--pill m-input-group--air date">
                          <input disabled type="text" class="form-control m-input" value="<?php echo date('d-m-Y',strtotime($purchase_order_data->order_date))?>" >
                        </div>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                    <div class="col-lg-2">
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Reference No
                        </label>
                        <div class="input-group m-input-group--pill m-input-group--air date">
                          <input disabled type="text" class="form-control m-input" value="<?php echo $purchase_order_data->reference_no?>" >
                        </div>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-2">
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Vehicle No
                        </label>
                        <div class="input-group m-input-group--pill m-input-group--air date">
                          <input disabled type="text" class="form-control m-input" value="<?php echo $purchase_order_data->vehicle_no?>" >
                        </div>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                    <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
                  </div>
                </div>
              </div>
              <!--end::Form-->
              <input type="hidden" id="purchase_order_id" name="purchase_order_id" value="<?php echo $purchase_order_id ?>">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-lg-9" >
            <div class="m-portlet ">
              <div class="m-portlet__head">
                <div class="m-portlet__head-caption">
                  <div class="m-portlet__head-title">
                    <h4 class="m-portlet__head-text">Ordered Items</h4>
                  </div>
                </div>
              </div>
              <div class="m-portlet__body">
                <div class="row">
                  <div class="col-lg-12">
                    <table style="width: 100%;max-width: 100%"  id="table_items" class="table m-table m-table--head-bg-success order-list table-striped table-bordered table-advance table-hover">
                      <thead>
                        <tr>
                          <th style="width:10%">Sl No</th>
                          <th style="width:25%">Material</th>
                          <th style="width:9%">Unit</th>
                          <th style="width:12%">Ordered Qty</th>
                          <th style="width:13%">Unit Price</th>
                          <th style="width:12%">Delivery Qty</th>
                        </tr>
                      </thead>
                      <tbody id="table_body">
                        <?php
                        $i=0;

                        foreach ($purchase_order_items as $key=>$value){ ?>
                          <tr class="row_<?php echo $i ?>">
                            <td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input style="padding: 10px" readonly type="text" class="form-control m-input item_slno" data-sl-no="<?php echo $i+1 ?>" name="item[sl_no][<?php echo $i ?>]" value="<?php echo $i+1 ?>"/></div></div></td>
                            <td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air">
                              <input type="text" readonly class="form-control m-input" name="item[material_name][<?php echo $i ?>]" value='<?php echo $value->material_name ?>' id="material_name_<?php echo $i+1 ?>"/></div>
                              <input type="hidden" name="item[item_id][<?php echo $i ?>]" value="<?php echo $value->item_id ?>" id="item_id_<?php echo $i+1 ?>"/>
                            </div></td>
                            <td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air">
                              <input type="text" readonly class="form-control m-input" value="<?php echo $value->unit_name ?>" name="item[unit_name][<?php echo $i ?>]" id="unit_name_<?php echo $i+1 ?>"/></div>
                              <input type="hidden" name="item[unit_id][<?php echo $i ?>]" value="<?php echo $value->unit_id ?>" id="unit_id_<?php echo $i+1 ?>"/>
                            </div></td>
                            <td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" readonly class="form-control m-input numeric_fields item_rate_fields quantity_field" data-sl-no="<?php echo $i+1 ?>" value="<?php echo $value->quantity ?>" /></div></div></td>
                            <td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" readonly class="form-control m-input numeric_fields item_rate_fields unit_price_field" data-sl-no="<?php echo $i+1 ?>" value="<?php echo $value->unit_price ?>" name="item[unit_price][<?php echo $i ?>]" id="unit_price_<?php echo $i+1 ?>"/></div></div></td>
                            <td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" required class="form-control m-input numeric_fields item_rate_fields quantity_field" data-sl-no="<?php echo $i+1 ?>" value="<?php echo $value->quantity-$value->received_qty ?>" name="item[quantity][<?php echo $i ?>]" id="quantity_<?php echo $i+1 ?>"/></div></div></td>
                          </tr>
                          <tr class="row_<?php echo $i ?>">
                            <td></td>
                            <td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input readonly placeholder="Item Code" type="text" class="form-control m-input" data-sl-no="<?php echo $i+1 ?>" name="item[item_code][<?php echo $i ?>]" value="<?php echo $value->item_code ?>" id="item_code_<?php echo $i+1 ?>"/></div></div></td>
                            <td colspan="4"><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input readonly placeholder="Description Here" type="text" class="form-control m-input" data-sl-no="<?php echo $i+1 ?>" name="item[description][<?php echo $i ?>]" value="<?php echo $value->description ?>" id="description_<?php echo $i+1 ?>"/></div></div></td>
                          </tr>
                          <?php
                          $i++;
                        }
                        ?>
                      </tbody>
                    </table>
                    <input type="hidden" value="<?php echo $i ?>" id="current_row_count" name="current_row_count" >
                    <input type="hidden" value="<?php echo $i ?>" id="actual_row_count" name="actual_row_count" >
                  </div>
                </div>
              </div>
            </div>

          </div>
          <div class="col-lg-3">
            <div class="m-portlet m-portlet--full-height">
              <div class="m-form__content">
                <br/>
                <div class="form-group m-form__group" >
                  <label for="user_name">
                    Challan No
                  </label>
                  <div class="m-input-icon m-input-icon--right">
                    <input required type="text" name="challan_no" class="form-control m-input m-input--pill m-input--air" placeholder="Challan No" >
                    <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                  </div>
                  <span class="m-form__help"></span>
                </div>
                <div class="form-group m-form__group" >
                  <label for="user_name">
                    Challan Date
                  </label>
                  <div class="m-input-icon m-input-icon--right">
                    <input data-date-format="dd-mm-yyyy" id="challan_date" required type="text" name="challan_date" class="form-control m-input m-input--pill m-input--air" placeholder="Challan Date" >
                    <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-calendar"></i></span></span>
                  </div>
                  <span class="m-form__help"></span>
                </div>
                <div class="form-group m-form__group" >
                  <label for="user_name">
                    Delivery Date
                  </label>
                  <div class="m-input-icon m-input-icon--right">
                    <input data-date-format="dd-mm-yyyy" id="delivery_date" required type="text" name="delivery_date" class="form-control m-input m-input--pill m-input--air" placeholder="Delivery Date" >
                    <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-calendar"></i></span></span>
                  </div>
                  <span class="m-form__help"></span>
                </div>
                <div class="form-group m-form__group">
                  <label>
                    Gate Entry
                  </label>
                  <div class="m-radio-inline" style="padding-top: 10px;">
                    <label class="m-radio m-radio--solid m-radio--state-danger">
                      <input required type="radio" class="form_fields_radio"  name="gate_entry" value="YES"> YES
                      <span></span>
                    </label>
                    <label class="m-radio m-radio--solid m-radio--state-warning">
                      <input type="radio" class="form_fields_radio" name="gate_entry"  value="NO"> NO
                      <span></span>
                    </label>
                  </div>
                  <span class="m-form__help"></span>
                </div>
                <div class="form-group m-form__group">
                  <label>
                    QC Stamp
                  </label>
                  <div class="m-radio-inline" style="padding-top: 10px;">
                    <label class="m-radio m-radio--solid m-radio--state-danger">
                      <input required type="radio" class="form_fields_radio"  name="qc_stamp" value="YES"> YES
                      <span></span>
                    </label>
                    <label class="m-radio m-radio--solid m-radio--state-warning">
                      <input type="radio" class="form_fields_radio" name="qc_stamp"  value="NO"> NO
                      <span></span>
                    </label>
                  </div>
                  <span class="m-form__help"></span>
                </div>
              </div>
              <div class="m-portlet__foot m-portlet__foot--fit">
                <div class="m-form__actions m-form__actions">
                  <div class="row">
                    <div class="ml-lg-auto">
                      <button type="submit" class="btn btn-brand form_submit_btn">
                        Save & Submit Delivery Challan
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
    <!-- Include Modal -->
  </div>
</div>

<!-- end:: Body -->
<!-- begin::Footer -->

<?php $this->view("includes/Inc_footer");?>
<!-- end::Footer -->

<!-- end:: Page -->

<?php $this->view("includes/Inc_sidebar");?>
<?php $this->view("includes/Inc_scripts");?>
<!--begin::Page Snippets -->

<script>


//    Toggle Sidebar
$("#m_aside_left_minimize_toggle").addClass("m-brand__toggler--active");
$("#body").addClass("m-brand--minimize m-aside-left--minimize");

$("#delivery_challan_menu").addClass('m-menu__item--active');

$("#challan_date,#delivery_date").datepicker({
  todayHighlight:!0, orientation:"bottom left", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  },
  // endDate: '+0d',
  <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
    startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
    <?php } ?>
  })

  </script>

  <script>


  jQuery(document).ready(function() {


    $(".form_submit_btn").click(function (e) {

      e.preventDefault();
      var a = $(this),
      l = $(this).closest("form"),
      action = l.attr('action');
      var form_id = l.attr('id');
      var item_count = $("#actual_row_count").val();
      var form_type = l.attr('data-form-type');
      var item_count_error = 0;

      l.validate(
        {
          rules: {
            order_paid_amount: {
              number: !0,
              required: !0
            },
            order_total_amount: {
              required: !0
            },
            bill_discount_amount: {
              number: !0,
              required: !0
            },
            commission_amount: {
              number: !0,
              required: !0
            }
          }, invalidHandler: function (e, r) {
            var i = $("#" + form_id + "_msg");
            i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
          }, submitHandler: function (e) {
          }
        }),// ADD validation to item fields
        $(".numeric_fields").each(function () {
          if(form_type=="order_create"){
            $("#"+$(this).attr("id")).rules('add', {
              number: !0
            });
          }
        });
        if(l.valid()==true){
          if(item_count==0 && form_type=='order_create'){
            // Show Confirm Alert
            swal({
              title: "Please Confirm Action",
              text: "Are you Sure to Submit Order Without Adding Items?",
              type: "warning",
              showCancelButton: !0,
              confirmButtonText: "Confirm & Proceed",
              cancelButtonText: "Close",
              confirmButtonColor: '#f4516c',
              cancelButtonColor: '#d33',
              reverseButtons: !0,
              closeOnClickOutside: false,
              allowOutsideClick: false,
            }).then(function(e) {
              e.value ? form_submit_function(l,a) : "cancel" === e.dismiss;
            })
          }else{
            form_submit_function(l,a);
          }
          function form_submit_function(l,a){
            a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0),
            l.ajaxSubmit({
              url: action,
              success: function (e, t, r, s) {
                var modal_id = l.attr('data-modal-id');
                var form_type = l.attr('data-form-type');
                $("#" + form_id + "_msg").addClass("m--hide");
                // Block UI
                mApp.blockPage({
                  overlayColor: "#000000",
                  type: "loader",
                  state: "success",
                  message: "Please wait..."
                }), setTimeout(function () {
                  mApp.unblockPage()
                }, 2e3);
                var obj = $.parseJSON(e);
                if (obj.status == 1) {
                  $("#"+modal_id).modal('toggle');
                  $("."+form_id+"_fields").val('');
                  $("."+form_id+"_select").val('').trigger("change");
                  a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);

                  swal({
                    width: 500,
                    title: obj.flashdata_title,
                    text: obj.flashdata_msg,
                    type: obj.flashdata_type,
                    showCancelButton: !0,
                    confirmButtonText: "Go to Work Profile",
                    cancelButtonText: "View Delivery Challans",
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    reverseButtons: !0,
                    closeOnClickOutside: false,
                    allowOutsideClick: false,
                    html: "<br>" +
                    '<a href="<?php echo base_url()?>purchases/delivery_challan/view/'+obj.challan_id+'"  class="SwalBtn1  btn btn-primary m-btn m-btn--custom customSwalBtn">' + 'View Delivery Challan Detail' + '</button>'
                  }).then(function(e) {
                    e.value ? window.location.replace("<?php echo base_url()?>work/profile/" + obj.work_id) : "cancel" === e.dismiss && window.location.replace("<?php echo base_url()?>purchases/delivery_challan/");
                  })
                }else {
                a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
              }
              if (obj == null) {
                setTimeout(function () {
                  a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
                  toastr["warning"]("Your Attempt is Timed Out. Please Try Again.", "Timed Out");
                }, 2e4)
              }
            },error: function() {
              a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
              toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
            }
          })
        }
      }
    });

  });


  </script>
