<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->
<style>
.note-editor {
  border-radius: 4px;
  background: white !important;
  border: 1px solid #bfc0c3 !important;
}
</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">
          BOQ - Edit 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().'work' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                BOQ Details
              </span>
            </a>
          </li>
        </ul>
      </div>
      <div>
        <?php if ($this->session->flashdata('flashdata_msg') != "")
        {?>
          <div class="alert alert-<?php echo $this->session->flashdata('alert_type')?> alert-dismissible fade show   m-alert m-alert--air m-alert--outline m-alert--outline-2x" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"></button>
            <strong>
              <?php echo $this->session->flashdata('flashdata_title')?>
            </strong>
            <?php echo $this->session->flashdata('flashdata_msg')?>
          </div>
        <?php } ?>
      </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">
              BOQ - Update Details
            </h3>
          </div>
        </div>
        <div class="m-portlet__head-tools">

        </div>
      </div>
      <form autocomplete="off"  id="form_boq_progress_add" method="post" action="<?php echo base_url().'boq/update_boq_details'?>" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right ">
        <div class="m-portlet__body" style="padding: 2.2rem 2.2rem !important;">
          <input type="hidden" value="<?php echo $boq_data->boq_id ?>" name="boq_id">
          <div class="m-form m-form--label-align-right m--margin-bottom-10">
            <div class="row">
              <div class="col-md-6">
                <div class="form-group m-form__group">
                  <label style="font-weight:900">Reference No</label>
                  <input type="text" value="<?php echo $boq_data->reference_no ?>" name="reference_no" class="form-control m-input m-input--air m-input--pill" >
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group m-form__group">
                  <label style="font-weight:900">Date</label>
                  <input required value="<?php if($boq_data->boq_date !='0000-00-00'){ echo date('d-m-Y',strtotime($boq_data->boq_date));}  ?>" data-date-format="dd-mm-yyyy" id="date" name="date" type="text"  class="form-control m-input  m-input--pill m-input--air" placeholder="Date" >
                </div>
              </div>
            </div>
            <br/>
            <div class="row">
              <div class="col-md-12">
                <div class="form-group m-form__group">
                  <label style="font-weight:900">BOQ Description</label>
                  <textarea class="summernote" name="boq_description" id="boq_description" ></textarea>
                </div>
              </div>
            </div>
            <div class="row" style="margin-top:20px">
              <div class="col-lg-12">
                <div class="form-group m-form__group">
                  <label style="font-weight:900">Materials Description</label>
                  <table style="width: 100%;max-width: 100%"  id="table_material_description" class="table m-table m-table--head-bg-info order-list table-striped table-bordered table-advance table-hover">
                    <thead>
                      <tr>
                        <th style="width:5%">Sl No</th>
                        <th style="width:20%">Material</th>
                        <th style="width:30%">Description</th>
                        <th style="width:6%">&nbsp;</th>
                      </tr>
                    </thead>
                    <tbody id="table_body_material_description">
                      <?php
                      $materials_count = 0;
                      $material_description = unserialize($boq_data->materials_description);
                      if($material_description!='' && $material_description!=null) {
                        $materials_count = sizeof($material_description);
                        $i = 0;
                        foreach ($material_description as $key => $value) {?>
                          <tr>
                            <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="material_description[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" class="form-control m-input" name="material_description[title][<?php echo $i ?>]" value='<?= $value['title'] ?>' id="material_title_<?php echo $i+1 ?>"/></div>
                              </div>
                            </td>
                            <td>
                              <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                <textarea rows="2" class="form-control m-input m-input--pill m-input--air"  name="material_description[description][<?php echo $i ?>]" id="material_description_<?php echo $i+1 ?>"/><?= $value['description'] ?></textarea>
                              </div>
                            </td>
                            <td><a href="javascript:" data-row-sl-no="<?= $i ?>" class="ibtnDel_material_description btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>
                            </tr>
                            <?php $i++; } } ?>
                          </tbody>
                          <tfoot>
                            <tr>
                              <td colspan="10"  style="text-align: left;border-bottom: 1px solid #e7ecf1;">
                                <button type="button"  class="btn btn-block " id="addrow_material_description"><i class="fa fa-plus-square"></i> Add new Material Description</button>
                              </td>
                            </tr>
                          </tfoot>
                        </table>
                        <input type="hidden" value="<?php echo $materials_count ?>" id="current_row_count_material_description" name="current_row_count_material" >
                      </div>
                    </div>
                  </div>
                  <div class="row" style="margin-top:20px">
                    <div class="col-lg-12">
                      <div class="form-group m-form__group">
                        <label style="font-weight:900">Terms & Conditions</label>
                        <table style="width: 100%;max-width: 100%"  id="table_terms" class="table m-table m-table--head-bg-success order-list table-striped table-bordered table-advance table-hover">
                          <thead>
                            <tr>
                              <th style="width:5%">Sl No</th>
                              <th style="width:20%">Title</th>
                              <th style="width:30%">Description</th>
                              <th style="width:6%">&nbsp;</th>
                            </tr>
                          </thead>
                          <tbody id="table_body_terms">
                            <?php
                            $terms_count=0;
                            $terms = unserialize($boq_data->terms_and_conditions);
                            if($terms!='' && $terms!=null) {
                              $terms_count = sizeof($terms);
                              $i = 0;
                              foreach ($terms as $key => $value) {?>
                                <tr>
                                  <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="terms[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" class="form-control m-input" name="terms[title][<?php echo $i ?>]" value='<?= $value['title'] ?>' id="terms_title_<?php echo $i+1 ?>"/></div>
                                    </div>
                                  </td>
                                  <td>
                                    <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                      <textarea rows="2" class="form-control m-input m-input--pill m-input--air"  name="terms[description][<?php echo $i ?>]" id="terms_description_<?php echo $i+1 ?>"/><?= $value['description'] ?></textarea>
                                    </div>
                                  </td>
                                  <td><a href="javascript:" data-row-sl-no="<?= $i ?>" class="ibtnDel_terms btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>
                                  </tr>
                                  <?php $i++; } } ?>
                                </tbody>
                                <tfoot>
                                  <tr>
                                    <td colspan="10"  style="text-align: left;border-bottom: 1px solid #e7ecf1;">
                                      <button type="button"  class="btn btn-block " id="addrow_terms"><i class="fa fa-plus-square"></i> Add new Terms and Condition</button>
                                    </td>
                                  </tr>
                                </tfoot>
                              </table>
                              <input type="hidden" value="<?php echo $terms_count ?>" id="current_row_count_terms_and_conditions" name="current_row_count" >
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="m-portlet__foot m-portlet__foot--fit">
                      <div class="m-form__actions m-form__actions">
                        <div class="row">
                          <div class="col-lg-9 ml-lg-auto">
                            <button type="submit" class="btn btn-brand form_submit_btn">
                              Submit
                            </button>
                            <button type="reset" class="btn btn-secondary">
                              Cancel
                            </button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </form>
                </div>
              </div>

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

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


        <!--begin::Page Snippets -->
        <script>
        $("#table_filter_status").selectpicker();
        $("[data-switch=true]").bootstrapSwitch()
      </script>
      <!--end::Page Snippets -->
      <script>

      $("#works_menu").addClass('m-menu__item--active m-menu__item--open');
      $("#works_menu_2").addClass('m-menu__item--active');

      //    Toggle Sidebar
      // $("#m_aside_left_minimize_toggle").addClass("m-brand__toggler--active");
      // $("#body").addClass("m-brand--minimize m-aside-left--minimize");
      $("#boq_description").summernote({height:500});
      <?php if($boq_data->boq_description==''){  ?>
        $("#boq_description").summernote('code','<p><b>To</b></p><p>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<b><?php echo $project_data->client_name ?></b></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><?php echo preg_replace("/\r\n|\r|\n/", '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $project_data->client_address); ?></b></p>');
        <?php }else{ ?>
      $("#boq_description").summernote('code','<?php echo str_replace(array("\r", "\n"), "'+'", str_replace("'","",$boq_data->boq_description))?>');
      <?php } ?>
      <?php if($terms!='' && $terms!=null) {
        $terms_count = sizeof($terms);
        $i = 0;
        foreach ($terms as $key => $value) {?>
          $("#terms_description_<?php echo $i+1 ?>").summernote({airMode: true});
          $("#terms_description_<?php echo $i+1 ?>").summernote('code','<?php echo $value['description'] ?>');
        <?php
        $i++;
      }
      } ?>
      $("#date").datepicker( {
        todayHighlight:!0, orientation:"bottom left", templates: {
          leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
        }
      })

      // Terms & Conditions
      $("#addrow_terms").on("click", function () {
        var i = parseInt($('#current_row_count_terms_and_conditions').val());
        var new_i = i + 1;
        var new_row = '<tr class="row_'+i+'">';
        new_row += '<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="'+new_i+'" name="terms[sl_no]['+i+']" value="'+new_i+'"/></div></div></td>';
        new_row += '<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" class="form-control m-input" name="terms[title]['+i+']" id="terms_title_'+new_i+'"/></div></div></td>';
        new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><textarea rows="2" class="form-control m-input m-input--pill m-input--air" name="terms[description]['+i+']" id="terms_description_'+new_i+'"/></textarea></div></td><td>';
        new_row += '<a href="javascript:" data-row-sl-no="'+i+'" class="ibtnDel_terms btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td></tr>';

        $('#table_body_terms').append(new_row);
        $('#current_row_count_terms_and_conditions').val(new_i);
        $("#terms_description_"+new_i).summernote({airMode: true});
      });

      $("#table_body_terms").on("click", ".ibtnDel_terms", function (event) {
        var sl_no = $(this).attr("data-sl-no");

        var counter = parseInt($("#current_row_count_terms_and_conditions").val());
        $(this).closest("tr").remove();
        counter -= 1;
        $("#current_row_count_terms_and_conditions").val(counter);
      });

      // Material DESCRIPTION
      $("#addrow_material_description").on("click", function () {
        var i = parseInt($('#current_row_count_material_description').val());
        var new_i = i + 1;
        var new_row = '<tr class="row_'+i+'">';
        new_row += '<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="'+new_i+'" name="material_description[sl_no]['+i+']" value="'+new_i+'"/></div></div></td>';
        new_row += '<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" class="form-control m-input" name="material_description[title]['+i+']" id="material_title_'+new_i+'"/></div></div></td>';
        new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><textarea rows="2" class="form-control m-input m-input--pill m-input--air" name="material_description[description]['+i+']" id="material_description_'+new_i+'"/></textarea></div></td><td>';
        new_row += '<a href="javascript:" data-row-sl-no="'+i+'" class="ibtnDel_material_description btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td></tr>';

        $('#table_body_material_description').append(new_row);
        $('#current_row_count_material_description').val(new_i);
      });

      $("#table_body_material_description").on("click", ".ibtnDel_material_description", function (event) {
        var sl_no = $(this).attr("data-sl-no");

        var counter = parseInt($("#current_row_count_material_description").val());
        $(this).closest("tr").remove();
        counter -= 1;
        $("#current_row_count_material_description").val(counter);
      });
      </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');
        l.validate({
          rules: {
            "paid_amount": {
              required: !0,
              min: 1,
              number: !0
            },
            "budget": {
              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) {
          }
        }),
        $(".numeric_fields").each(function () {
          $(this).rules('add', {
            number: !0
          });
        }),
        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);
              swal({
                width: 500,
                title: obj.flashdata_title,
                text: obj.flashdata_msg,
                type: obj.flashdata_type,
                // showCancelButton: !0,
                confirmButtonText: "OK",
                // cancelButtonText: "Add New Project",
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                reverseButtons: !0,
                closeOnClickOutside: false,
                allowOutsideClick: false,
              }).then(function(e) {
                window.location.replace("<?php echo base_url()?>boq/profile/<?php echo $boq_data->boq_id ?>");
              })
            } 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>
