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

<style>
.item_table td{
  padding: .75rem !important;
}
.item_table th{
  white-space: nowrap;
  padding: .75rem !important;
}
.table thead th {
  border-top: 2px solid #f4f5f8 !important;
}
</style>
<style>

@media print {

  .table-bordered td{
    padding: .75rem !important;
  }
  .table-bordered th{
    white-space: nowrap;
    padding: .75rem !important;
  }
  .table-bordered .table thead th {
    border-top: 2px solid #f4f5f8 !important;
  }
  .m-invoice-2 .m-invoice__wrapper .m-invoice__head .m-invoice__container.m-invoice__container--centered {
    width: 97%;
  }
  .m-invoice-2 .m-invoice__wrapper .m-invoice__body.m-invoice__body--centered {
    width: 97%;
  }
  .m-invoice-2 .m-invoice__wrapper .m-invoice__body.m-invoice__body--centered {
    margin-left: 10px !important;
  }

  #print_view {
    margin-top: -20%;
  }
  #print, .print_hide_div
  {
    display: none !important;
  }
  body * {
    visibility: hidden;
  }
  .m-portlet__body * {
    visibility: visible;
    overflow: visible !important;
  }
  .letter_head_img{
    display: block !important;
  }
  body.m-content--skin-light2 {
    background-color: white !important;
  }
  .m-invoice-2 .m-invoice__wrapper .m-invoice__head .m-invoice__container.m-invoice__container--centered {
    width: 97% !important;
  }
  .m-invoice-2 .m-invoice__wrapper .m-invoice__head .m-invoice__container .m-invoice__items .m-invoice__item {
    display: table-cell;
  }
}
.m-invoice-2 .m-invoice__wrapper .m-invoice__head .m-invoice__container .m-invoice__items .m-invoice__item {
  display: table-cell !important;
}
.col-sm-4 {
  -webkit-box-flex: 0;
  -ms-flex: 0 0 33.33333%;
  flex: 0 0 33.33333%;
  max-width: 33.33333%;
}
.col-sm-8 {
  -webkit-box-flex: 0;
  -ms-flex: 0 0 66.66667%;
  flex: 0 0 66.66667%;
  max-width: 66.66667%;
}
</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">
          Purchase Request Details
        </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_request' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Purchase Request Details
              </span>
            </a>
          </li>


        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <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">

            </h3>
          </div>
        </div>
        <div class="m-portlet__head-tools">
          <?php if($request_data->approval_status_1==1 && ($this->config->item('Purchase_purchase_request_second_approval')==0 || ($this->config->item('Purchase_purchase_request_second_approval')==1 && $request_data->approval_status_2==1))){ ?>
            <a href="javascript:;" onclick="window.print()" id="btn_print_bill" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-danger m-btn--gradient-to-info  m-btn  m-btn--icon m-btn--air m-btn--pill">
              <span>
                <i class="fa fa-print"></i>
                <span>
                  Print Request
                </span>
              </span>
            </a>
            <a href="<?php echo base_url().'purchase_order/add?request_id='.$request_data->request_id?>" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-info m-btn--gradient-to-success  m-btn  m-btn--icon m-btn--air m-btn--pill">
              <span>
                <i class="fa fa-plus-circle"></i>
                <span>
                  Create Purchase Order
                </span>
              </span>
            </a>
          <?php }
          $purchase_request_1st_level_approval_roles = explode(',',$this->config->item('Approvals_purchase_request_1st_level'));
          $purchase_request_2nd_level_approval_roles = explode(',',$this->config->item('Approvals_purchase_request_2nd_level'));
          if(in_array($this->session->userdata('user_role'), $purchase_request_1st_level_approval_roles)){
            if($this->config->item('Purchase_purchase_request_second_approval')==0 || ($this->config->item('Purchase_purchase_request_second_approval')==1 && $request_data->approval_status_2==0))?>
            <button id="purchase_request_approve_btn"  data-approval-status="<?php echo $request_data->approval_status_1?>" data-approval-level="1" data-toggle="modal" data-target="#purchase_request_approve_modal" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-warning m-btn--gradient-to-danger m-btn m-btn--icon  m-btn--pill m-btn--air">
              <i class="la la-check"></i> Approve/Reject Request <?php if($this->config->item('Purchase_purchase_request_second_approval')==1){ echo "(1st)";}?>
            </button>
          <?php }
          if($this->config->item('Purchase_purchase_request_second_approval')==1 && $request_data->approval_status_1==1 && in_array($this->session->userdata('user_role'), $purchase_request_2nd_level_approval_roles)){?>
            <button id="purchase_request_approve_btn"  data-approval-status="<?php echo $request_data->approval_status_2?>" data-approval-level="2" data-toggle="modal" data-target="#purchase_request_approve_modal" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-warning m-btn--gradient-to-danger m-btn m-btn--icon  m-btn--pill m-btn--air">
              <i class="la la-check"></i> Approve/Reject Request (2nd)
            </button>
          <?php }
          if(($this->session->userdata('user_role')!='supervisor') || ($this->session->userdata('user_role')=='supervisor' && $request_data->approval_status_1==0 && $request_data->created_by==$this->session->userdata('user_id')) ){ ?>
            <a href="<?php echo base_url().'purchase_request/edit/'.$request_data->request_id?>" style="margin-left: 5px;margin-right: 5px" class="btn m-btn--gradient-from-focus m-btn--gradient-to-danger  m-btn  m-btn--icon m-btn--air m-btn--pill">
              <span>
                <i class="fa fa-edit"></i>
                <span>
                  Edit Purchase Request
                </span>
              </span>
            </a>
          <?php } ?>
        </div>
      </div>
      <div class="m-portlet__body m-portlet__body--no-padding" id="print_view">
        <div class="m-invoice-2" style="padding-bottom: 20px">
          <div  class="m-invoice__wrapper">
            <div class="m-invoice__head" >
              <div class="m-invoice__container m-invoice__container--centered">
                <div class="m-invoice__logo" style="padding-top: 3rem;">
                  <a href="#">

                  </a>
                </div>
                <div class="row">
                  <div class="col-md-8 col-sm-8" style="border-right: 1px solid #ebedf2;">
                    <span class="m-invoice__desc" style="text-align: left;padding: 1rem 0 1rem 0 !important;">
                      <span class="m-invoice__subtitle">Requested By.</span>
                      <span><strong><?php echo $request_data->created_user?></strong></span>
                    </span>
                  </div>
                  <div class="col-md-4 col-sm-4">
                    <div class="m-invoice__logo" style="padding-top: 0px;">
                      <a href="#">
                        <h2>PURCHASE REQUEST<br/>#<?php echo $request_data->request_id ?></h2>
                      </a>
                    </div>
                  </div>
                </div>
                <div class="m-invoice__items" style="padding: 2rem 0 3rem 0;">
                  <div class="m-invoice__item">
                    <span class="m-invoice__subtitle">REQUESTED TO </span>
                    <span class="m-invoice__text"><strong><?php echo $request_data->location_name?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: center">
                    <span class="m-invoice__subtitle">REQUESTED DATE</span>
                    <span class="m-invoice__text"><strong><?php echo date('d-m-Y',strtotime($request_data->request_date))?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: right">
                    <span class="m-invoice__subtitle">CREATED BY</span>
                    <span class="m-invoice__text"><strong><?php echo $request_data->created_user?></strong></span>
                  </div>
                  <div class="m-invoice__item" style="text-align: right">
                    <span class="m-invoice__subtitle">APPROVAL STATUS <?php if($this->config->item('Purchase_purchase_request_second_approval')==1){ echo "1";}?></span>
                    <?php
                    if($request_data->approval_status_1==1){
                      $approval_status_1="<span class='m-badge m-badge--success m-badge--wide'><i class='fa fa-check-double'></i> Approved"."<br/> By: ".$request_data->approved_1_user."<br/> On: ".date('d-m-Y h:i A',strtotime($request_data->approved_date_1))."</span>";
                    }elseif($request_data->approval_status_1==2){
                      $approval_status_1="<span class='m-badge m-badge--danger m-badge--wide'><i class='fa fa-times-cirecle'></i> Rejected"."<br/> By: ".$request_data->approved_1_user."<br/> On: ".date('d-m-Y h:i A',strtotime($request_data->approved_date_1))."</span>";
                    }else{
                      $approval_status_1="<span class='m-badge m-badge--info m-badge--wide'><i class='fa fa-ellipsis-h'></i> Pending</span>";
                    }

                    if($request_data->approval_status_2==1){
                      $approval_status_2="<span class='m-badge m-badge--success m-badge--wide'><i class='fa fa-check-double'></i> Approved"."<br/> By: ".$request_data->approved_2_user."<br/> On: ".date('d-m-Y h:i A',strtotime($request_data->approved_date_2))."</span>";
                    }elseif($request_data->approval_status_2==2){
                      $approval_status_2="<span class='m-badge m-badge--danger m-badge--wide'><i class='fa fa-times-cirecle'></i> Rejected"."<br/> By: ".$request_data->approved_2_user."<br/> On: ".date('d-m-Y h:i A',strtotime($request_data->approved_date_2))."</span>";
                    }else{
                      $approval_status_2="<span class='m-badge m-badge--info m-badge--wide'><i class='fa fa-ellipsis-h'></i> Pending</span>";
                    }
                    ?>
                    <span class="m-invoice__text"><strong><?php echo $approval_status_1?></strong></span><br/>
                    <?php if($this->config->item('Purchase_purchase_request_second_approval')==1){?>
                      <span class="m-invoice__subtitle">APPROVAL STATUS 2</span>
                      <span class="m-invoice__text"><strong><?php echo $approval_status_2?></strong></span><br/>
                    <?php } ?>
                  </div>

                </div>
              </div>
            </div>
            <div class="m-invoice__body m-invoice__body--centered" style="min-height: 200px">
              <div class="table-responsive">
                <table class="table m-table table-bordered item_table">
                  <thead>
                    <tr>
                      <th>Sl No</th>
                      <th style="text-align: left">Material Name</th>
                      <th>Unit</th>
                      <th>Unit Price</th>
                      <th>Requested Qty</th>
                      <th>Est. Amount</th>
                      <th class="print_hide_div">Ordered Qty</th>
                      <th class="print_hide_div">Billed Qty</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $n = 1;
                    foreach($request_items as $row):
                      ?>
                      <tr>
                        <td><?php echo $n++?></td>
                        <td style="text-align: left"><?php echo $row->material_name?></td>
                        <td><?php echo $row->unit_name?></td>
                        <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($row->unit_price,$this->config->item('Currency_currency_shortname'));?></td>
                        <td><?php echo $row->quantity?></td>
                        <td><?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($row->total_amount,$this->config->item('Currency_currency_shortname'));?></td>
                        <td class="print_hide_div"><?php echo $row->ordered_qty?></td>
                        <td class="print_hide_div"><?php echo $row->received_qty?></td>
                      </tr>
                      <tr>
                        <td style="border-bottom: 1px solid #c5c5c5"></td>
                        <td colspan="7" style="text-align: left;font-weight: 300;border-bottom: 1px solid #c5c5c5">Description: <?php echo $row->description?></td>
                      </tr>
                      <?php
                    endforeach;
                    ?>
                  </tbody>
                </table>
              </div>
            </div>
            <div class="m-invoice__body m-invoice__body--centered">
              <div class="m-invoice__table  m-invoice__table--centered table-responsive">
                <table class="table">
                  <thead>
                    <tr>
                      <th style="text-align: right"><strong>TOTAL ESTIMATED AMOUNT : <?php echo $this->config->item('Currency_currency_format')." ".$this->config->item('currency_format')->formatCurrency($request_data->total_est_amount,$this->config->item('Currency_currency_shortname'));?></strong></th>
                    </tr>
                  </thead>
                </table>
              </div>
            </div>
            <div class="" style="width: 70%;margin: 0 auto;padding: 2rem 0 0 0;">
              <div class="m-invoice__table  m-invoice__table--centered table-responsive" style="margin-bottom: 100px">
                <table class="table">
                  <thead>
                    <tr>
                      <th width="60%">Remarks : <?php echo $request_data->remarks;?>
                      </th>
                      <th width="40%">
                        Purchase Order Status :
                        <?php if(sizeof($order_created)>0){ ?>
                          <span class='m-badge m-badge--success m-badge--wide'><i class='fa fa-check-circle'></i> <?= sizeof($order_created)?> Order Created</span>
                        <?php }else{?>
                          <span class='m-badge m-badge--danger m-badge--wide'><i class='fa fa-times-circle'></i> Order Not Created</span>
                        <?php } ?>
                      </th>
                      <?php if($this->config->item('Purchase_request_payment_released_status')==1){ ?>
                        <tr>
                          <th></th>
                          <th>
                            Payment Status :
                            <?php if($request_data->payment_released_status==1){ ?>
                              <span class='m-badge m-badge--success m-badge--wide'><i class='fa fa-check-circle'></i> Released On <?php echo date('d-m-Y H:i:A',strtotime($request_data->payment_released_date)) ?></span>
                            <?php }elseif($request_data->payment_released_status==2){ ?>
                              <span class='m-badge m-badge--danger m-badge--wide'><i class='fa fa-times-circle'></i> Cancelled On <?php echo date('d-m-Y H:i:A',strtotime($request_data->payment_released_date)) ?></span>
                            <?php }else{?>
                              <span class='m-badge m-badge--warning m-badge--wide'><i class='fa fa-times-circle'></i> Pending</span>
                            <?php } ?>
                          </th>
                        <?php } ?>
                      </tr>
                      <?php
                      if($request_data->request_id != 0){ ?>
                        <tr>
                          <th>

                          </th>
                          <th>
                            <?php foreach ($order_created as $key => $value) { ?>
                              <a class="print_hide_div" target="_blank" href="<?php echo base_url().'purchase_order/profile/'.$value->order_id ?>"><span class='m-badge m-badge--focus m-badge--wide'><i class='fa fa-check-circle'></i>  View Purchase Order #<?= $value->order_id?></span></a><br/>
                            <?php } ?>
                          </th>
                        </tr>
                      <?php } ?>
                    </thead>
                  </table>
                </div>
                <!-- Bills created -->
              </div>
            </div>

          </div>
        </div>
      </div>
      <?php $this->view("modules/include_modals/modal_bills");?>
      <?php $this->view("modules/include_modals/modal_purchase_requests");?>
    </div>
  </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 -->
<?php $this->view("modules/include_scripts/bill_scripts");?>


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

$("#purchase_request_menu").addClass('m-menu__item--active m-menu__item--open');
$("#purchase_request_menu_2").addClass('m-menu__item--active');
$("#page_title").text("Purchase Request - #<?php echo $request_data->request_id.' ['.$request_data->work_name.']'?>");

</script>

<script>
$('body').on('click', '#purchase_request_approve_btn', function() {
  var request_id = "<?php echo $request_data->request_id?>";
  $('#purchase_request_id').val(request_id);
  var approval_level = $(this).data('approval-level');
  $("#approval_level").val(approval_level);
  var approval_status = $(this).data('approval-status');
  if(approval_status!=0){
    $('input[type=radio][name=approve_status][value='+approval_status+']').prop('checked',true);
  }else{
    $('input[type=radio][name=approve_status]').prop('checked',false);
  }
});

$('#payment_view_table').DataTable({
  "scrollX" : true,
  scrollX:!0, scrollCollapse:!0,
  dom:"<'row'<'col-sm-6 text-left'><'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
  "paging":   false,
  "ordering": false,
  "info":     false,
  searching: false
});
$(".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: {
      "paid_amount": {
        required: !0,
        min: 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);
        $("#"+modal_id).modal('toggle');
        toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
        location.reload();
      } 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>
