<?php include "common/user_log.php";
date_default_timezone_set('Asia/Colombo');
$date = date("Y-m-d");
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>NWP Engineering Department</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="dist/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="dist/css/ionicons.min.css">
    <!-- daterange picker -->
    <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker-bs3.css">
    <!-- iCheck for checkboxes and radio inputs -->
    <link rel="stylesheet" href="plugins/iCheck/all.css">
    <!-- Bootstrap Color Picker -->
    <link rel="stylesheet" href="plugins/colorpicker/bootstrap-colorpicker.min.css">
    <!-- Bootstrap time Picker -->
    <link rel="stylesheet" href="plugins/timepicker/bootstrap-timepicker.min.css">
    <!-- Select2 -->
    <link rel="stylesheet" href="plugins/select2/select2.min.css">
	<!-- DataTables -->
    <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="bootstrap-sweetalert/lib/sweet-alert.css" />
  </head>
  <body class="hold-transition skin-blue sidebar-mini sidebar-collapse">
    <div class="wrapper">

      <header class="main-header">
        <!-- Logo -->
         <a href="home.php" class="logo">
          <!-- mini logo for sidebar mini 50x50 pixels -->
         <span class="logo-mini"><img src="img/logo.png" width="100"></span>
          <!-- logo for regular state and mobile devices -->
        <span class="logo-lg"><b><img src="img/logo.png" width="100"></b></span>
        </a>
        <!-- Header Navbar: style can be found in header.less -->
        <nav class="navbar navbar-static-top" role="navigation">
          <!-- Sidebar toggle button-->
          <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
			 <span class="logo-lg">
             &nbsp;&nbsp;<font face="Lucida Sans Unicode, Lucida Grande, sans-serif">NWP Engineering Department</font></span>
          </a>
		
          <div class="navbar-custom-menu">
            <ul class="nav navbar-nav">

			  <li class="dropdown messages-menu">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                  <i class="fa fa-bell-o"></i>
                  <span class="label label-warning"><?php
				 $sql20 = mysqli_query($conn, "SELECT
contractor_reg.name,
aworded_contractors.pub_supplier,
projects_reg.pro_name,
aworded_contractors.bond_exp,
DATEDIFF(aworded_contractors.bond_exp, '$date') AS date_diff,
aworded_contractors.c_num
FROM
aworded_contractors
Inner Join projects_reg ON aworded_contractors.project = projects_reg.id
Inner Join contractor_reg ON contractor_reg.id = aworded_contractors.supplier
HAVING
date_diff <=14 AND date_diff>=0 order by date_diff ASC"); 
				echo mysqli_num_rows($sql20); ?></span>
                </a>
                <ul class="dropdown-menu">
                  <li class="header">You have <?php echo mysqli_num_rows($sql20); ?> Advance Bond Expiration Reminder(s)</li>
                  <li>
                    <!-- inner menu: contains the actual data -->
                    <ul class="menu">
                    <?php
					
					while($row20 = mysqli_fetch_array($sql20)) { ?>
					  <li><!-- start message -->
                        <a href="#">
                          <h4 style="margin-left:0">
                            <?php echo $row20['pro_name']; ?> <font color="red" size="2">(<?php echo $row20['date_diff']; ?> Days)</font>
                            <small><i class="fa fa-clock-o"></i> <?php echo $row20['bond_exp']; ?></small>
                          </h4>
                          <p style="margin-left:0">Contract No: <?php echo $row20['c_num']; ?></p>
                          <p style="margin-left:0">Contractor: <?php echo $row20['name']; ?></p>
                        </a>
                      </li><!-- end message -->                    
					 <?php } ?>   
                    </ul>
                  </li>
                  <!--<li class="footer"><a href="oparation_income.php?flag=2">See All Operation(s)</a></li>-->
                </ul>
              </li>
              <!-- Tasks: style can be found in dropdown.less -->
			  
			  <!-- User Account: style can be found in dropdown.less -->
              <li class="dropdown user user-menu">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                  <img src="dist/img/user2-160x160.png" class="user-image" alt="User Image">
                  <span class="hidden-xs"><?php echo $name; ?></span>
                </a>
                <ul class="dropdown-menu">
                  <!-- User image -->
                  <li class="user-header">
                    <img src="dist/img/user2-160x160.png" class="img-circle" alt="User Image">
                    <p>
             NWP Engineering Department
                    
                    </p>
                  </li>
                  <!-- Menu Body -->
                 
                  <!-- Menu Footer-->
                  <li class="user-footer">
                    <div class="pull-left">
                      
                    </div>
                    <div class="pull-right">
                      <a href="distroy.php" class="btn btn-default btn-flat">Sign out</a>
                    </div>
                  </li>
                </ul>
              </li>
              <!-- Control Sidebar Toggle Button -->
              <li>
                <a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a>
              </li>
            </ul>
          </div>
        </nav>
      </header>
      <!-- Left side column. contains the logo and sidebar -->
      <aside class="main-sidebar">
        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">
          <!-- Sidebar user panel -->
    <?php include "common/sidebar.php"; ?>  
     
        </section>
        <!-- /.sidebar -->
      </aside>

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Awarded Contractors
          </h1>
    <ol class="breadcrumb">
      <li><a href="home.php"><i class="fa fa-home"></i> Home</a></li>
    </ol>
        </section>

        <!-- Main content -->
        <section class="content">

<!-- ************************************************* -->
  <div class="box box-info only_eng"> 
    <div class="box-header with-border"> 
      <h3 class="box-title">Awarded Contractors Details</h3>
    </div>

    <div class="box-body"> 
      <form id="form_projects" action="javascript:void(0);">

      	<div class="form-group"> 
      	  <label>Select Project:<font color="#FF0000"><strong>*</strong></font></label>
      	  <select class="form-control select2 error" id="project" name="project" style="width: 100%;" required>
      	  	<option value="">Select Project.....</option>
      		<?php 
            $sql="SELECT * FROM projects_reg where id not in (select project from aworded_contractors) ORDER BY pro_name ASC";
            $result=mysqli_query($conn, $sql);

            while ($row = mysqli_fetch_array($result)){

          ?>

            <option value="<?php echo $row['id']; ?>"><?php echo $row['pro_name']; ?></option>

          <?php } ?>
      	  </select>
      	</div>
		
		  <div class="form-group">
			<input name="con_type" type="radio" id="reg_con" class="with-gap radio-col-indigo" value="1" required>
			<label> &nbsp;&nbsp;Registered Contractor</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<input name="con_type" type="radio" id="pub_con" class="with-gap radio-col-indigo" value="2" required>
			<label class="m-l-20"> &nbsp;&nbsp;Public Contractor</label>
		  </div>		

      	<div class="form-group" style="display:none" id="con_list"> 
      	  <label>Select Contractor:<font color="#FF0000"><strong>*</strong></font></label>
      	  <select class="form-control select2 error" id="supplier" name="supplier" style="width: 100%;">
      	  	<option value="">Select Contractor.....</option>
      		<?php 
            $sql="SELECT * FROM contractor_reg WHERE del=0 AND id>0 ORDER BY id ASC";
            $result=mysqli_query($conn, $sql);

            while ($row = mysqli_fetch_array($result)){

          ?>

            <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option>

          <?php } ?>
      	  </select>
      	</div>	  
    	
    	<div class="form-group" style="display:none" id="con_input"> 
          <label>Contractor Name:<font color="#FF0000"><strong>*</strong></font></label>
          <input type="text" class="form-control error" id="con_name" name="con_name" autocomplete="off">
        </div>
		
		<div class="form-group"> 
          <label>Contract No:<font color="#FF0000"><strong>*</strong></font></label>
          <input type="text" class="form-control error" id="c_num" name="c_num" autocomplete="off" required>
        </div>
		
		<div class="form-group"> 
          <label>Allocation Amount:<font color="#FF0000"><strong>*</strong></font></label>
          <input type="text" class="form-control" id="allo_amount" name="allo_amount" autocomplete="off" readonly>
        </div>
		
		<div class="form-group"> 
          <label>Contract Amount:<font color="#FF0000"><strong>*</strong></font></label>
          <input type="text" class="form-control error" id="c_amount" name="c_amount" autocomplete="off" required>
        </div>

        <div class="form-group">
          <label>Started Date:<font color="#FF0000"><strong>*</strong></font></label>            
          <div class="input-group"> 
            <div class="input-group-addon"> <i class="fa fa-calendar"></i> </div>
            <input type="text" class="form-control date" id="start_date" name="start_date" autocomplete="off" required>
          </div>
        </div>
		
		<div class="form-group">
          <label>Date of Awarded:<font color="#FF0000"><strong>*</strong></font></label>            
          <div class="input-group"> 
            <div class="input-group-addon"> <i class="fa fa-calendar"></i> </div>
            <input type="text" class="form-control date" id="date_aworded" name="date_aworded" autocomplete="off" required>
          </div>
        </div>

        <div class="form-group">
          <label>Date of Completion:<font color="#FF0000"><strong>*</strong></font></label>            
          <div class="input-group"> 
            <div class="input-group-addon"> <i class="fa fa-calendar"></i> </div>
            <input type="text" class="form-control date" id="date_comp" name="date_comp" autocomplete="off">
          </div>
        </div>
		
		<div class="form-group">
          <label>Date of Extension:</label>            
          <div class="input-group"> 
            <div class="input-group-addon"> <i class="fa fa-calendar"></i> </div>
            <input type="text" class="form-control date" id="date_ext" name="date_ext" autocomplete="off">
          </div>
        </div>

        <div class="form-group">
          <label>Completed Date:</label>            
          <div class="input-group"> 
            <div class="input-group-addon"> <i class="fa fa-calendar"></i> </div>
            <input type="text" class="form-control date" id="date_last" name="date_last" autocomplete="off">
          </div>
        </div>
		
		<div class="form-group">
          <label>Advance Bond Expire:</label>            
          <div class="input-group"> 
            <div class="input-group-addon"> <i class="fa fa-calendar"></i> </div>
            <input type="text" class="form-control date" name="bond_exp" autocomplete="off">
          </div>
        </div>
		
		<div class="form-group">
          <label>Performance Security Expire:</label>            
          <div class="input-group"> 
            <div class="input-group-addon"> <i class="fa fa-calendar"></i> </div>
            <input type="text" class="form-control date" name="sec_exp" autocomplete="off">
          </div>
        </div>

        <span style="color: red;" class="pull-right">* Required Fields</span>

        <div class="box-footer">
          <button class="btn btn-primary">SUBMIT</button>
        </div>
      </form>
    </div>	
  </div>

 <!-- /.table -->
        
    <div class="box box-info"> 
      <div class="box-header"> 
        <h3 class="box-title">Awarded Contractors</h3>
      </div>
      <!-- /.box-header -->
      <div class="box-body table-responsive"> 
        <table id="example1" class="table table-bordered table-striped">
          <thead>
            <tr>
            	<th>ID</th>
              	<th>Project</th>
              	<th>Supplier</th>
              	<th>Public Supplier</th>
              	<th>Contract No</th>
              	<th>Contract Amount</th>
              	<th>Started Date</th>
              	<th>Date Awarded</th>
              	<th>Date of Completion</th>
              	<th>Extended Date</th>
              	<th>Completed Date</th>
              	<th>Advance Bond Expire</th>
              	<th>Perf. Security Expire</th>
				<!--<th class="only_eng">Edit</th>-->
				<th class="only_eng">Remove</th>
            </tr>
          </thead>
          <tbody>
            <?php 
              $sql="SELECT
aworded_contractors.c_amount,
aworded_contractors.date_comp,
aworded_contractors.date_aworded,
aworded_contractors.started_date,
aworded_contractors.c_num,
aworded_contractors.pub_supplier,
aworded_contractors.id,
aworded_contractors.ext_date,
aworded_contractors.com_date,
aworded_contractors.bond_exp,
aworded_contractors.security_exp,
projects_reg.pro_name,
contractor_reg.name
FROM
aworded_contractors
Inner Join projects_reg ON aworded_contractors.project = projects_reg.id
Inner Join contractor_reg ON aworded_contractors.supplier = contractor_reg.id
 ORDER BY aworded_contractors.id ASC";

              $result=mysqli_query($conn, $sql);
              $x = 0;
              while ($row = mysqli_fetch_array($result)){ $x++;
            ?>

            <tr>
              <td><?php echo $x; ?></td>
              <td><?php echo $row['pro_name']; ?></td>
              <td><?php echo $row['name']; ?></td>
              <td><?php echo $row['pub_supplier']; ?></td>
              <td><?php echo $row['c_num']; ?></td>
              <td><?php echo $row['c_amount']; ?></td>
              <td><?php echo $row['started_date']; ?></td>
              <td><?php echo $row['date_aworded']; ?></td>
              <td><?php echo $row['date_comp']; ?></td>
              <td><?php echo $row['ext_date']; ?></td>
              <td><?php echo $row['com_date']; ?></td>
              <td><?php echo $row['bond_exp']; ?></td>
              <td><?php echo $row['security_exp']; ?></td>

             <!-- <td align="center" class="only_eng">
      			     <button  class="btn btn-info btn-sm btn_up" data-toggle="modal" data-target="#edit_lng" value="<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-pencil"></span> &nbsp;EDIT </button>
      			  </td> -->
				  
              <td align="center" class="only_eng">
                  <button value="<?php echo $row['id']; ?>" class="btn btn-danger btn-sm btn_dell">
                <span class="glyphicon glyphicon-trash"></span> &nbsp;DELETE </button>
              </td>
            </tr>
            <?php  } ?>
          </tbody>
        </table>
      </div>
      <!-- /.box-body -->
    </div>
    
  </section>
   
      </div>
	      <!-- /.End table -->
<!-- ************************************************* -->
      
           <!-- /.col -->
              </div><!-- /.row -->
            </div><!-- /.box-body -->
            
          

          <div class="row">
            
    
    
      </div>
      <!-- /.col (right) -->
         

      </div>
<footer class="main-footer"> 
<div class="pull-right hidden-xs">
          
        </div>  <strong>Copyright &copy; NWP Engineering Department</strong> All rights reserved.
      </footer>

      <!-- Control Sidebar -->
      <aside class="control-sidebar control-sidebar-dark">
        <!-- Create the tabs -->
        <ul class="nav nav-tabs nav-justified control-sidebar-tabs">
          <li><a href="#control-sidebar-home-tab" data-toggle="tab"><i class="fa fa-home"></i></a></li>
          <li><a href="#control-sidebar-settings-tab" data-toggle="tab"><i class="fa fa-gears"></i></a></li>
        </ul>
        <!-- Tab panes -->
        <div class="tab-content">
          <!-- Home tab content -->
          <div class="tab-pane" id="control-sidebar-home-tab">
          </div><!-- /.tab-pane -->

          </div><!-- /.tab-pane -->
        </div>
      </aside><!-- /.control-sidebar -->
      <!-- Add the sidebar's background. This div must be placed
           immediately after the control sidebar -->
      <div class="control-sidebar-bg"></div>
    </div><!-- ./wrapper -->

    <!-- jQuery 2.1.4 -->
    <script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.5 -->
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <!-- Select2 -->
    <script src="plugins/select2/select2.full.min.js"></script>
	<!-- DataTables -->
    <script src="plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
    <!-- InputMask -->
    <script src="plugins/input-mask/jquery.inputmask.js"></script>
    <script src="plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
    <script src="plugins/input-mask/jquery.inputmask.extensions.js"></script>
    <!-- date-range-picker -->
    <script src="plugins/daterangepicker/moment.min.js"></script>
    <script src="plugins/daterangepicker/daterangepicker.js"></script>
    <!-- bootstrap color picker -->
    <script src="plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
    <!-- bootstrap time picker -->
    <script src="plugins/timepicker/bootstrap-timepicker.min.js"></script>
    <!-- SlimScroll 1.3.0 -->
    <script src="plugins/slimScroll/jquery.slimscroll.min.js"></script>
    <!-- iCheck 1.0.1 -->
    <script src="plugins/iCheck/icheck.min.js"></script>
    <!-- FastClick -->
    <script src="plugins/fastclick/fastclick.min.js"></script>
    <!-- AdminLTE App -->
    <script src="dist/js/app.min.js"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="dist/js/demo.js"></script>
    <script src="bootstrap-sweetalert/lib/sweet-alert.min.js"></script>
	
    <!-- Page script -->
    <script>
      $(function () {
        //Initialize Select2 Elements
        $(".select2").select2();

        //Datemask dd/mm/yyyy
        $("#datemask").inputmask("dd/mm/yyyy", {"placeholder": "yyyy-mm-dd"});
        //Datemask2 mm/dd/yyyy
        $("#datemask2").inputmask("mm/dd/yyyy", {"placeholder": "yyyy-mm-dd"});
        //Money Euro
        $("[data-mask]").inputmask();

        //Date range picker
        $('.date').daterangepicker();
        //Date range picker with time picker
       

	   //Date range as a button
        $('#daterange-btn').daterangepicker(
            {
              ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month')]
              },
              startDate: moment().subtract(29, 'days'),
           
            },
        function (start) {
          $('#reportrange span').html(start.format('MMMM D, YYYY'));
        }
        );

        //iCheck for checkbox and radio inputs
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
          checkboxClass: 'icheckbox_minimal-blue',
          radioClass: 'iradio_minimal-blue'
        });
        //Red color scheme for iCheck
        $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
          checkboxClass: 'icheckbox_minimal-red',
          radioClass: 'iradio_minimal-red'
        });
        //Flat red color scheme for iCheck
        $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
          checkboxClass: 'icheckbox_flat-green',
          radioClass: 'iradio_flat-green'
        });

        
      });
	  
      $(function () {
        $("#example1").DataTable();
        $('#example2').DataTable({
          "paging": true,
          "lengthChange": false,
          "searching": false,
          "ordering": true,
          "info": true,
          "autoWidth": false
        });
      });
	</script>

<script>
$('#project').change(function () {
	var id = $(this).val();
	  $.post('function/aworded_contractors_fun.php', {'get_project_data': 'data', pid: id}, function (data) {
		  $.each(data, function (index, data) {
			  $('#c_amount').val(data.total_cost_esti); // Allocation Total
			  $('#allo_amount').val(data.total_cost_esti); // Allocation Total
			  $('#start_date').val(data.start_date);
		  });
	  }, "json");
});

/****************************** Insert  *********************************************************/

var con_type = null;
$("input[name='con_type']").click(function() {
	con_type = this.value;
	if(con_type == 1) {
	  $('#con_list').show();
	  $('#con_input').hide();
	}
	if(con_type == 2) {
	  $('#con_list').hide();
	  $('#con_input').show();
	}
});

$("#form_projects").on('submit',(function(e) {

    /*for (instance in CKEDITOR.instances) {
        CKEDITOR.instances[instance].updateElement();
    }*/

    e.preventDefault();
    $.ajax({
    url: "function/aworded_contractors_fun.php",
    type: "POST",
    data:  new FormData(this),
    contentType: false,
        cache: false,
    processData:false,
    beforeSend : function()
    {
      //$("#preview").fadeOut();
      //$("#err").fadeOut();
    },
    success: function(data){
      if(data.status=='ok')
      {
        swal({
          title: "Added!",
          text: "Successfully Added!",
          type: "success",
          confirmButtonText: "OK"
            },
            function (isConfirm) {
          if (isConfirm) {
              window.location.href = "aworded_contractors.php";
          }
        });
      }
	  else if(data.status=='error2')
      {
        swal("", "Already Allocated!", "warning");
      }
	  else if(data.status=='error3')
      {
        swal("", "Contract Number Already Exist!", "warning");
      }
	  else if(data.status=='error4')
      {
        swal("", "Invalid contract amount!", "warning");
      } 
	  else if(data.status=='error5')
      {
        swal("", "Please Select/Enter Contractor Name!", "warning");
      }
	  else if(data.status=='error6')
      {
        swal("", "Contract Amount not be equal to the allocation amount", "warning");
      }
	  else
      {
        swal("Oops...", "Something went wrong!", "warning");
      }
      
      },
      error: function(e) 
      {
        //$("#err").html(e).fadeIn();
        alert("err2");
      }
   });
  //alert("Done");
}));

/******************************************* Remove Contact **************************************/

$(".btn_dell").click(function(){
  var x = $(this).val();
  swal({
	title: "Are you sure?",
	text: "Your may not be able to recover!",
	type: "warning",
	showCancelButton: true,
	confirmButtonClass: "btn-danger",
	confirmButtonText: "Yes, delete it!",
	closeOnConfirm: false
  },
	function () {
	  del_func(x);
	}
  );
});

function del_func(reg_id){
	if(reg_id === '') {

		swal("Warning!", "Please Select Valid Record!", "warning");
	
	} else {
		
		$.post( "function/aworded_contractors_fun.php", {remove_reg: "data", reg_id : reg_id}, function( data ) {
		    if (data.status == 'ok') {
			  swal({
				title: "Deleted!",
				text: "Successfully Deleted!",
				type: "success",
				confirmButtonText: "OK"
			  },
				  function (isConfirm) {
				if (isConfirm) {
					window.location.href = "aworded_contractors.php";
				}
			  });
			}else if(data.status == 'error2') {
			  swal("", "Data Already Exist!", "warning");
			
			} else{
			  swal("Oops...", "Something went wrong!", "warning");
			}
		}, "json");
	}
}
</script>	
		
</body>
</html>
