<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->

<?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">
          <?php echo $this->config->item('Quotation_sales_quotation_title') ?>
        </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 $this->session->userdata("dashboard_link")?>" 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().'sales_quotation' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                <?php echo $this->config->item('Quotation_sales_quotation_title') ?>
              </span>
            </a>
          </li>


        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <div class="m-portlet m-portlet--mobile m-portlet--info m-portlet--head-solid-bg m-portlet--bordered m-portlet--rounded">
      <div class="m-portlet__head">
        <div class="m-portlet__head-caption">
          <div class="m-portlet__head-title">
            <h3 class="m-portlet__head-text">
              List of <?php echo $this->config->item('Quotation_sales_quotation_title') ?>s <?php if($this->config->item('Quotation_hide_work_order_created')==1){ echo " (Work Order Not Created)";} ?>
            </h3>
          </div>
        </div>
        <div class="m-portlet__head-tools">

          <?php if($this->session->userdata('user_role')!='auditor') { ?>
            <a href="<?php echo base_url()?>sales_quotation/add" class="btn btn-focus m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">
              <span>
                <i class="la la-plus"></i>
                <span>
                  Create New <?php echo $this->config->item('Quotation_sales_quotation_title') ?>
                </span>
              </span>
            </a>
          <?php } ?>

        </div>
      </div>
      <div class="m-portlet__body">
        <?php if (!empty($this->config->item('company_branches'))) { ?>
          <div class="m-form m-form--label-align-right m--margin-bottom-10">
            <div class="row">
              <div class="col-xl-12 order-2 order-xl-1">
                <div class="form-group m-form__group row ">
                  <div class="col-lg-3">
                    <div class="form-group m-form__group">
                      <label>
                        Company Branch
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select width="100%" required class="form-control m-select2 table_filter company-branch-select2" id="company_branch">
                          <option selected value="all"> All Branches</option>
                          <option value="0">NO BRANCH</option>
                          <?php foreach ($this->config->item('company_branches') as $key => $value) { ?>
                            <option value="<?php echo $value->branch_id ?>"><?php echo $value->branch_name ?></option>
                          <?php } ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        <?php } ?>
        <table width="100%" class="table m-table nowrap table-header-fixed table-striped table-bordered table-hover table-header-fixed" id="sales_quotation_view_table">
          <thead>
            <tr class="">
              <th>Sl No</th>
              <th>ID</th>
              <th>Quotation No</th>
              <th>Branch</th>
              <th>Subject</th>
              <th>Customer Name</th>
              <th>Address</th>
              <th>Contact Person</th>
              <th>Date</th>
              <th>Prepared By</th>
              <th>Quotation Details</th>
              <th>Net Total</th>
              <th>Status</th>
              <th>Creation</th>
              <th>Actions</th>
              <th>Status</th>
              <th>Remarks</th>
            </tr>
          </thead>
          <tbody>
          </tbody>
        </table>
        <div class="modal fade"  tabindex="-1" id="quotation_approve_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog " role="document" >
            <form autocomplete="off" method="post" data-modal-id="quotation_approve_modal" id="form_quotation_approve" action="<?php echo base_url().'sales_quotation/approve'?>" class="form-horizontal">
              <div class="modal-content">
                <div class="alert alert-info" role="alert" style="margin-bottom: 0px">
                  <button type="button" class="close" data-dismiss="modal"></button>
                  <br/>
                  <h4 class="alert-heading">Warning! Please Confirm Your Action</h4>
                  <br/>
                  <div class="alert m-alert--default" role="alert" style="margin-bottom: 10px">
                    <div class="form-group m-form__group" >
                      <label for="user_name" style="color: black">Approval Status</label>
                      <div class="m-radio-inline" style="padding-top: 10px;">
                        <label class="m-radio m-radio--solid m-radio--state-success">
                          <input required type="radio" class="form_fields_radio"  name="quotation_approval_status" value="1"> Approved
                          <span></span>
                        </label>
                        <label class="m-radio m-radio--solid m-radio--state-danger">
                          <input type="radio" class="form_fields_radio" name="quotation_approval_status"  value="2"> Rejected
                          <span></span>
                        </label>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Remarks
                      </label>
                      <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                        <textarea type="text" rows="3" name="remarks" id="quotation_approval_remarks" class="form-control m-input form_quotation_approve_fields" placeholder="Remarks " aria-describedby="basic-addon1"></textarea>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <p>
                    <button class="btn btn-danger form_submit_btn" type="submit" name="submit">Confirm & Proceed</button>
                    <button type="button" class="btn btn-primary modal-dismiss" data-dismiss="modal">Cancel</button>
                  </p>
                  <input type="hidden" name="quotation_id" id="quotation_id_appoval">
                </div>
              </div>
            </form>
          </div>
        </div>
        <div class="modal fade" tabindex="-1" id="quotation_delete_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog " role="document" >
            <form autocomplete="off" data-modal-id="quotation_delete_modal" method="post" action="<?php echo base_url().'sales_quotation/delete'?>" class="form-horizontal">
              <div class="modal-content">
                <div class="alert alert-danger" role="alert" style="margin-bottom: 0px">
                  <button type="button" class="close" data-dismiss="modal"></button>
                  <br/>
                  <h4 class="alert-heading">Warning! Please Confirm Your Action</h4>
                  <br/>
                  <p> Are You Sure to Delete this Data ?. All the Associated Data will Lost. </p>
                  <p>
                    <button class="btn btn-danger form_submit_btn" type="submit" name="submit">Confirm & Delete</button>
                    <button type="button" class="btn btn-primary modal-dismiss" data-dismiss="modal">Cancel</button>
                  </p>
                  <input type="hidden" name="quotation_id" id="quotation_id_delete">
                </div>
              </div>
            </form>
          </div>
        </div>

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

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

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

<script>
$("#sales_quotation_menu").addClass('m-menu__item--active m-menu__item--open');
$("#sales_quotation_menu_2").addClass('m-menu__item--active');
</script>
<script>
$('body').on('click', '#btn_approve_quotation', function() {
  var no = $(this).closest('tr').children('td');
  $('#quotation_id_appoval').val(no.eq(1).text());
  var approval_status = no.eq(15).text();
  $("input[name=quotation_approval_status][value=" + approval_status + "]").prop('checked', true);
  $('#quotation_approval_remarks').val(no.eq(16).text());
})
$('body').on('click', '#quotation_delete_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#quotation_id_delete').val(no.eq(1).text());
})
var Datatable_sales_quotation_view_table=function() {
  var t=function() {
    function get_branch_id() {
      return $('#company_branch :selected').val();
    }
    var sales_quotation_view_table = $('#sales_quotation_view_table').DataTable({
      "ajax":{
        url :"<?php echo base_url().'sales_quotation/select_sales_quotation' ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d){
          <?php if (!empty($this->config->item('company_branches'))) { ?>
            d.branch_id = get_branch_id();
          <?php } ?>
          }
        },
        "bProcessing": true,
        "deferRender": true,
        "bDeferRender": true,
        "scrollX" : true,
        buttons: [
          { extend: 'print', className: 'btn dark btn-outline',title: '<?php echo $this->config->item('Quotation_sales_quotation_title') ?>' },
          { extend: 'csv', className: 'btn purple btn-outline ' },
          { extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
        ],
        dom:"<'row'<'col-sm-6 text-left'B><'col-sm-6 text-right'f>>\n\t\t\t<'row'<'col-sm-12'tr>>\n\t\t\t<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>", // horizobtal scrollable datatable
        "lengthMenu": [
          [25, 50, 100, -1],
          [25, 50, 100, "All"] // change per page values here
        ],
        "columnDefs": [
          {
            "targets": [ 1,10,15,16<?php if (empty($this->config->item('company_branches'))) { echo ",3"; }?><?php if($this->config->item('Quotation_template')==2){ echo",9,11";}?> ],
            className: "hidden"
          },
          {
            // render: function (data, type, full, meta) {
            //   return "<div class='text-wrap width-300'>" + data + "</div>";
            // },
            // targets: 5
          }
        ],
        'order': false
      });
      $(".table_filter").on("change", function() {
        sales_quotation_view_table.ajax.reload();
      });

    };
    return {
      init:function() {
        t()
      }
    }
  }
  ();
  jQuery(document).ready(function() {
    Datatable_sales_quotation_view_table.init();
    $(".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');
      l.validate({
        rules: {
          "amount": {
            required: !0,
            number: !0
          }
        }, invalidHandler: function (e, r) {
          var i = $("#" + form_id + "_msg");
          i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
        }, submitHandler: function (e) {
        }
      }), l.valid() && (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) {
            a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
            toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
            $("#"+modal_id).modal('toggle');
            $("."+form_id+"_fields").val('');
            $('#sales_quotation_view_table').dataTable().fnDestroy();
            Datatable_sales_quotation_view_table.init();
          } 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>
