<?php require('includes/finance/finance_functions.php');?>
<div class="row mt-n5">
  <div class="col-md-4 border-right grey lighten-3 p-2">

    <h3>New <?php echo $_REQUEST['payment_type']?> Registration</h3>

    <?php if($_REQUEST['payment_type']=="Expenses"){?>
    <form id="expenseForm" >
      <div class="row">

        <div class="form-group col-12 col-md-5">
          <label for="employee">Employee</label>
          <?php echo listSurveyor($_SESSION['userid'],"employee")?>
        </div>
        <div class="form-group col-12 col-md-5">
          <label for="type">Expense Type</label>
          <select id="type" class="form-control autocomplete col-12" data-placeholder="Expense Type" name="type"><?php echo getExpenses()?></select>
        </div>
        <div class="col-1 col-md-2 mt-1">
            <button type="button" id="newExpense" class="btn btn-secondary m-0 px-3 py-2 z-depth-0 border mt-md-4" data-href="includes/finance/finance_newExpense.php"  data-toggle="tooltip" data-placement="top" data-original-title="Register New Expense Type"><span class="fa-layers fa-fw">
              <i class="fad fa-plus" data-fa-transform="left-4" style="color:green"></i>
              <i class="fad fa-user-cog" data-fa-transform="right-4"></i>
            </span></button>
          </div>

      </div>

      <div id="vehicle" class="col-12 white p-2 border" style="display: none">
      <div class="row">
        <div class="form-group mb-3 col-6">
          <div class="col-12"><label for="vReg">Vehicle Reg</label></div>          
          <div class="col-12 input-group">
            <select class="browser-default custom-select" id="vReg" name="vReg" data-placeholder="Select Vehicle" style="max-width:200px"><?php echo listVehicles('vReg')?></select>
            <div class="input-group-append">
              <button type="button" id="newVehicle" class="btn btn-secondary m-0 px-3 py-2 z-depth-0 border" data-href="includes/finance/finance_newVehicle.php" data-values="" data-toggle="tooltip" data-placement="top" data-original-title="Register New Vehicle"><span class="fa-layers fa-fw">
              <i class="fad fa-plus" data-fa-transform="left-4" style="color:green"></i>
              <i class="fad fa-car" data-fa-transform="right-4"></i>
              </span></button>
            </div>
          </div>
        </div>

        <div class="form-group col-6">
          <div class="col-12"><label for="miles">Mileage</label></div>
          
          <div class="col-12 input-group">
            <input class="form-control" type="text" name="vMileage" aria-describedby="checkMileage">
            <div class="input-group-append">
              <button class="btn btn-md btn-info border m-0  py-2 z-depth-0 waves-effect" type="button" id="checkMileage" data-toggle="tooltip" data-placement="top" data-original-title="Calculate Rate"><i class="fad fa-calculator-alt"></i></button>
            </div>
          </div>
        </div>
    </div>
        <div class="form-group col-12 ">
          <label for="vDetails">Journey Details</label>
          <input class="form-control form-control-sm" type="text" name="vDetails">
        </div>

         <div id="mileageDetails"></div>
      </div>

      <div class="form-group">
        <label for="desc">Description</label>
        <textarea id="desc" class="form-control form-control-sm" rows="4" name="desc"></textarea>
      </div>

      <div class="form-group">
        <label class="control-label" for="date">Date</label>
        <input class="form-control datepicker" type="text" name="date" id="date" value="<?php echo date('d-m-Y')?>" />
      </div>






  <div class="row">
  <div class="col-12 col-md-6">
      <div class="form-group">
        <label class="col-12 control-label" for="payment_method">Payment Method</label>
        <div class="col-12">
          <select class="form-control autocomplete payMethod" name="payment_method">
            <option value="Cash">Cash</option>
            <option value="Card">Card</option>
            <option value="Cheque">Cheque</option>
            <option value="Direct Debit">Direct Debit</option>
            <option value="Counter Credit">Counter Credit</option>
            <option value="Credit Transfer">Credit Transfer</option>
            <option value="BACS">BACS</option>
          </select>
        </div>
      </div>
      <div id="chequeNo" class="col-12 mt-1" style="display: none">
        <input id="chequeInput" type="text" class="form-control" name="chequeNo" placeholder="Cheque Number">
      </div>
    </div>
    
    <div class="col-12 col-md-6">
      <div class="form-group">
        <label class="col-12 control-label" for="use_vat">VAT Applicable</label>
        <div class="col-12">
          <div class="form-check">
            <input id="va1" type="radio" class="form-check-input" name="use_vat" value="applyvat" checked>
            <label class="form-check-label" for="va1">Inc VAT</label>
          </div>
        </div>
        <div class="col-12">
          <div class="form-check">
            <input id="va2" type="radio" class="form-check-input" name="use_vat" value="novat">
            <label class="form-check-label" for="va2">No VAT</label>
          </div>
        </div>
      </div>
    </div>


  </div>


    <div class="form-group row">
      <div class="col-12"><label for="desc">Amount</label></div>
      <div class="col-1 col-md-1"><span class="fad fa-pound-sign lead"></span></div>
      <div class="col-6 col-md-6"><input class="form-control form-control-sm" type="text" name="amount"></div>
      </div>



    <button type="submit" id="btn_save" style="display: none"></button>

    <input type="hidden" id="vatrate" name="vatrate" value="<?php echo getCompanyDetails('vatrate')?>">
    <input type="hidden" id="vRate" name="vRate" value="">
    <input type="hidden" id="vatComponent" name="vat" value="">
    <input type="hidden" id="netComponent" name="net" value="">
    <input type="hidden" name="payment_type" value="<?php echo $_REQUEST['payment_type']?>">
    <input type="hidden" name="FUNCTION" value="register_expense">
    <button id="reset" type="reset" style="display: none">reset</button>

    </form>
    <?php }


    if($_REQUEST['payment_type']=="Outgoings"){?>
    <form id="outgoingsForm">
      <div class="row mb-3">

        <div class="col-12 col-md-6">
          <div class="col-12 "><label for="date">Date</label></div>
          <div class="col-12"><input class="form-control datepicker" type="text" name="date" id="date" value="<?php echo date('d-m-Y')?>" /></div>
        </div>

        <div class="col-12 col-md-6">
          <div class="col-12 "><label for="payee">Code</label></div>
          <div class="col-12">
          <select class="form-control-sm autocomplete w-100" id="code" name="code" data-placeholder="Select Code"><?php echo listCodes('payee','desc')?></select>
          </div>
        </div>

      </div>



      <div class="form-group mb-3">
        <div class="col-12 "><label for="payee">Payee</label></div>
        <div class="row ml-1">
          <div class="col-9 col-md-6">
            <select class="form-control form-control-sm autocomplete" id="payee" name="payee" data-placeholder="Select Payee"><?php echo listPayee('out')?></select>
          </div>
          <div class="col-1 col-md-3">
            <button type="button" id="newPayee" class="btn btn-secondary m-0 px-3 py-2 z-depth-0 border" data-href="includes/finance/finance_newPayee.php?type=out" data-values="out" data-toggle="tooltip" data-placement="top" data-original-title="Register New Payee"><span class="fa-layers fa-fw">
              <i class="fad fa-plus" data-fa-transform="left-4" style="color:green"></i>
              <i class="fad fa-user-cog" data-fa-transform="right-4"></i>
            </span></button>
          </div>
        </div>
      </div>



      <div class="form-group">
      <div class="col-12"><label for="desc">Description</label></div>
      <div class="col-12"><textarea id="desc" class="form-control form-control-sm" rows="4" name="desc"></textarea></div>
      </div>

      <div class="col-12">
        <div class="row">
          <div class="col-12 col-md-6">
            <div class="form-group">
              <label class="col-12 control-label" for="payment_method">Payment Method</label>
              <div class="col-12">
                <select class="form-control autocomplete payMethod" name="payment_method">
                  <option value="Cash">Cash</option>
                  <option value="Card">Card</option>
                  <option value="Cheque">Cheque</option>
                  <option value="Direct Debit">Direct Debit</option>
                  <option value="Counter Credit">Counter Credit</option>
                  <option value="Credit Transfer">Credit Transfer</option>
                  <option value="BACS">BACS</option>
                </select>
              </div>
            </div>
            <div id="chequeNo" class="col-12 mt-1" style="display: none">
              <input id="chequeInput" type="text" class="form-control" name="chequeNo" placeholder="Cheque Number">
            </div>
          </div>


          <div class="col-12 col-md-6">
            <div class="form-group">
              <label class="col-12 control-label" for="use_vat">VAT Applicable</label>
              <div class="col-12">
                <div class="form-check">
                  <input id="v1" type="radio" class="form-check-input" name="use_vat" value="applyvat" checked>
                  <label class="form-check-label" for="v1">Inc VAT</label>
                </div>
              </div>
              <div class="col-12">
                <div class="form-check">
                  <input id="v2" type="radio" class="form-check-input" name="use_vat" value="novat">
                  <label class="form-check-label" for="v2">No VAT</label>
                </div>
              </div>
            </div>
          </div>

        </div>
      </div>

      <div class="form-group row">
      <div class="col-12"><label for="amount">Amount</label></div>
      <div class="col-1 col-md-1"><span class="fad fa-pound-sign lead"></span></div>
      <div class="col-6 col-md-6"><input class="form-control form-control-sm" type="text" name="amount"></div>
      </div>



      <button type="submit" id="btn_save" style="display: none"></button>

      <input type="hidden" id="vatrate" name="vatrate" value="<?php echo getCompanyDetails('vatrate')?>">
      <input type="hidden" id="vRate" name="vRate" value="">
      <input type="hidden" id="vatComponent" name="vat" value="">
      <input type="hidden" id="netComponent" name="net" value="">
      <input type="hidden" name="payment_type" value="<?php echo $_REQUEST['payment_type']?>">
      <input type="hidden" name="FUNCTION" value="register_outgoing">
      <button id="reset" type="reset" style="display: none">reset</button>
    </form>
    <?php }?>

  </div>

  <div class="col-md-8 p-2 border">
  <div class="row ml-2">
  <h3>Registered <?php echo $_REQUEST['payment_type']?></h3>
  <div id="expenseList" class="col-12"></div>
  </div>

</div>





<div class="col-12 mb-5">

<p class="lead text-center">Generated Reports <a href="javascript:void(0)" class="btn btn-secondary m-0 px-3 py-2 z-depth-0 border mt-2" onClick="toggleVisibility('reports');genFiles()"><i class="fad fa-arrow-square-right"></i></a></p>

<div id="reports" class="mb-5 bg-info" style="display: none"></div>
</div>

<script src="packages/fileupload/sortable.min.js" type="text/javascript"></script>
<script src="packages/fileupload/fileinput.min.js" type="text/javascript"></script>
<script src="packages/fileupload/fa_theme.js" type="text/javascript"></script>
<link href="packages/fileupload/fileinput.min.css" media="all" rel="stylesheet" type="text/css"/>
<link href="packages/fileupload/theme.min.css" media="all" rel="stylesheet" type="text/css"/>

<script>
$('.payMethod').change(function(){
  var val=$(this).val()
  console.log(val)
  if(val=="Cheque"){$('#chequeNo').css('display','block')}
  else{
    $('#chequeInput').val('')
    $('#chequeNo').css('display','none')
  }
})


$('#checkMileage').click(function(){
  var v=$("#expenseForm select[name='vReg']")
  var m=$("#expenseForm input[name='vMileage']")
  if(v.val()==""){response('danger','<strong>YOU MUST SELECT A VEHICLE</strong>')}
  else if(m.val()==""){response('danger','<strong>YOU MUST SELECT ADD MILEAGE</strong>')}
  else{
  $.post('includes/finance/finance_functions.php',{FUNCTION:"calcMileage",reg:v.val(),miles:m.val()},function(data){
    $('#mileageDetails').html(data)
    var r=$("#returnTotal").val()
    $("#expenseForm input[name='amount']").val(r)
    var rte=$("#returnRate").val()
    $("#expenseForm input[name='vRate']").val(rte)
    var rtv=$("#returnVat").val()
    $("#expenseForm input[name='vat']").val(rtv)
    var rtn=$("#returnNet").val()
    $("#expenseForm input[name='net']").val(rtn)
  })
  }
})

$('#type').change(function(){
if($(this).val()=="Mileage"){$('#vehicle').show()}
//if($(this).val()=="Outgoings" || $(this).val()=="Commission" || $(this).val()=="Commission By Cheque" || $(this).val()=="Commission By Discount"){$('#outgoings').show()}
})




$('#expenseForm').on('click', '#newVehicle', function(){
  $('#popupBox .modal-body').html(''); 
  var target = $(this).data('href');
  var string = $(this).data('values');
  var dest=encodeURI(target)
  $('#popupBox').modal('show');
  $('#popupBox .modal-body').load(dest);
})

$('#outgoingsForm').on('click', '#newPayee', function(){
  $('#popupBox .modal-body').html(''); 
  var target = $(this).data('href');
  var string = $(this).data('values');
  var dest=encodeURI(target)
  $('#popupBox').modal('show');
  $('#popupBox .modal-body').load(dest);
})

$('#expenseForm').on('click', '#newExpense', function(){
  $('#popupBox .modal-body').html(''); 
  var target = $(this).data('href');
  var string = $(this).data('values');
  var dest=encodeURI(target)
  $('#popupBox').modal('show');
  $('#popupBox .modal-body').load(dest);
})




$("#expenseForm button[type='submit']").click(function(event){
  event.preventDefault();
  var e=$("#employee");
  var t=$("#type");
  var d=$("#expenseForm textarea[name='desc']");
  var a=$("#expenseForm input[name='amount']");
  if(e.val()==""){response('danger','<strong>YOU MUST SELECT AN EMPLOYEE</strong>')}
  if(t.val()==""){response('danger','<strong>YOU MUST SELECT AN EXPENSE TYPE</strong>')}
  if(d.val()==""){response('danger','<strong>YOU MUST ADD A DESCRIPTION</strong>')}
  if(a.val()==""){response('danger','<strong>YOU MUST ADD A MONETARY VALUE</strong>')}
  if(e.val()!="" && t.val()!="" && d.val()!="" && a.val()!=""){
    $.ajax({
      url:'includes/finance/finance_functions.php',
      type:'POST',
      data:$('#expenseForm').serialize(),
      success:function(result){
        //debug(result)
        response('success','Your expenses have been registered successfully')
        var start=$('#date1').val();
        var end=$('#date2').val();
        $('#expenseList').load('includes/finance/finance_exp_data.php?payment_type=<?php echo $_REQUEST['payment_type']?>&start='+start+'&end='+end)
        $('#vehicle').hide();
        $('#mileageDetails').html('')
        $('#type').val('').trigger('change')
        $('#employee').val('').trigger('change')
        $('#reset').click();
      }
    });
  }
});

$("#outgoingsForm button[type='submit']").click(function(event){
  event.preventDefault();
  var t=$("#code");
  var p=$("#payee");
  var d=$("#outgoingsForm textarea[name='desc']");
  var a=$("#outgoingsForm input[name='amount']");
  if(t.val()==""){response('danger','<strong>YOU MUST SELECT A CODE</strong>')}
  if(p.val()==""){response('danger','<strong>YOU MUST SELECT A PAYEE</strong>')}
  if(d.val()==""){response('danger','<strong>YOU MUST ADD A DESCRIPTION</strong>')}
  if(a.val()==""){response('danger','<strong>YOU MUST ADD A MONETARY VALUE</strong>')}
  if(p.val()!="" && t.val()!="" && d.val()!="" && a.val()!=""){
    $.ajax({
      url:'includes/finance/finance_functions.php',
      type:'POST',
      data:$('#outgoingsForm').serialize(),
      success:function(result){
        //debug(result)
        response('success','Your ougoings have been registered successfully')
        var start=$('#date1').val();
        var end=$('#date2').val();
        $('#expenseList').load('includes/finance/finance_exp_data.php?payment_type=<?php echo $_REQUEST['payment_type']?>&start='+start+'&end='+end)
        $('#code').val('').trigger('change')
        $('#payee').val('').trigger('change')
        $('#reset').click();
      }
    });
  }
});


$('#expenseList').load('includes/finance/finance_exp_data.php?payment_type=<?php echo $_REQUEST['payment_type']?>')

$('#navBox').html('<li class="breadrumb-item"><a href="index.php?t=includes/finance&p=finance_invoices.php">Finance</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadrumb-item active"><?php echo $_REQUEST['payment_type']?></li>')

$(document).ready(function() {
  $('#footerControls').html('<button class="btn btn-secondary" onclick="triggerAction(\'btn_save\')">SAVE</button>');
})

function genFiles(){
  $('#reports').load('includes/finance/finance_expense_reports.php?payment_type=<?php echo $_REQUEST['payment_type']?>');
}

function regenData(){  
  $('#expenseList').load('includes/finance/finance_exp_data.php?payment_type=<?php echo $_REQUEST['payment_type']?>');
}	

genFiles()
</script>
