<?php require_once(ROOT_URL.'/includes/asset/asset_functions.php');
?>
<script>
document.getElementById('navBox').innerHTML =
    '<li class="breadrumb-item"><a href="index.php?t=includes/master&p=client.php&view=list">Schedules</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadrumb-item active">New Client</li>'
</script>
<form id="clientForm" class="form-horizontal">
    <fieldset>
        <div class="row card-data">
            <div class="col-12">
                <?php require_once(ROOT_URL.'/forms/elements/client_details.frm');?>
            </div>
            <?php
            require_once(ROOT_URL.'/forms/elements/client_contacts.frm');
            if($_SESSION['access']==1 || $_SESSION['access']==999){
                require_once(ROOT_URL.'/forms/elements/client_billing.frm');
                require_once(ROOT_URL.'/forms/elements/client_other.frm');
                require_once(ROOT_URL.'/forms/elements/client_additional.frm');
            }
            ?>
        </div>
        <div class="spacer50"></div>
        <input id="function" type="hidden" name="FUNCTION" value="insert">
    </fieldset>

    <div class="col-12 col-md-12 text-center">

        <input id="btn_reset" type="reset" value="Reset Form" style="display:none" />
        <input id="btn_create" type="submit" name="submit" value="Submit" style="display:none" />

    </div>
</form>
<script>
// Function to copy client information to billing and primary contact fields
function setBilling() {
    document.getElementById("billing_name").value = document.getElementById("clientname").value;
    document.getElementById("billing1").value = document.getElementById("address1").value;
    document.getElementById("billing2").value = document.getElementById("address2").value;
    document.getElementById("billing_town").value = document.getElementById("town").value;
    document.getElementById("billing_county").value = document.getElementById("county").value;
    document.getElementById("billing_postcode").value = document.getElementById("postcode").value;
    document.getElementById("billing_phone").value = document.getElementById("phone1").value;
    document.getElementById("billing_email").value = document.getElementById("email").value;
    document.getElementById("pri_contact_tel").value = document.getElementById("phone1").value;
    document.getElementById("pri_contact_mob").value = document.getElementById("mobile").value;
    document.getElementById("pri_contact_email").value = document.getElementById("email").value;
}

// Adding CREATE button to footer controls
document.addEventListener('DOMContentLoaded', function() {
    document.getElementById("footerControls").innerHTML =
        '<button class="btn btn-secondary" onclick="triggerAction(\'btn_create\')">CREATE</button>';
});

// Function to calculate and set the renewal date based on inception date and term years
document.querySelector('[name="incepton_date"]').addEventListener("change", function() {
    var d = this.value;
    var t = document.querySelector('[name="term_years"]').value;
    var res = d.split("-");
    var day = res[0];
    var month = res[1];
    var year = parseInt(res[2]) + parseInt(t);
    document.querySelector('[name="renewal_date"]').value =
        day + "-" + month + "-" + year;
});
</script>