$('#dataTableClass').DataTable();

$(document).on('click', '.addWastePro , .date', function () {
    var itr = $('#premiumItr').val();
    $('#addWastePro' + itr + '').parent().html('<button id="remove' + itr + '" data-itr="'+itr+'" type="button" class="close flt-none removeicon removeproduct" aria-hidden="true">&times;</button>');


    itr++;
        $('#wasteTable tbody').append('<tr data-itr="'+itr+'">'
            + '<td><input type="text" class="form-control date"  name="date' + itr + '" id="date' + itr + '"></td>'
            + '<td >'
            + '<input type="number" name="Amount' + itr + '" id="Amount' + itr + '" min="0" class="form-control countAmount" value=""/>'
            + '</td>'
            + '<td class="noprint"><button id="remove' + itr + '" data-itr="'+itr+'" type="button" class="close flt-none removeicon removeproduct" aria-hidden="true">&times;</button></td>'
            +''
        );
        $('#premiumItr').val(itr);


});


$(document).live('keyup', '.countAmount', function () {
    var piecesNum = 0;
    $('.countAmount').each(function () {
        piecesNum += Number($(this).val());
    });
    $('#allCost').html(Number(piecesNum));
});

$(document).on('click', '.removeicon', function () {

        var itr = $(this).data('itr');
        var piecesNum = Number($('#allCost').html());
        var pieces = Number($('#Amount' + itr).val());
        var total = piecesNum - pieces
        $('#allCost').html(total);

    // delete by partents
    //this make it not sent by post
    $(this).parent().parent().remove();



    return false;

});

$(document).on('change', '#clientID', function () {

    var clientID = $(this).val();

    $.post("premiumAjaxController.php?do=getClientdata", {clientID: clientID}, function (data) {
        var obj = jQuery.parseJSON( data );
            // $('#table tbody').empty();
            // $('#table tbody').append('<tr>'
         $('#name').html(obj.clientname);
        $('#mobile').html(obj.clientmobile);
        $('#area').html(obj.areaName);
        $('#type').html(obj.typeName);
        $('#debt').html(obj.clientdebt);
        $('#comment').html(obj.clientdetails);

//
    });

});



var mydate=new Date();
jQuery(document).on('focus',".date", function(){
    jQuery(this).datetimepicker({
        format : 'yyyy-mm-dd',
        autocomplete:false,
        autoclose :  true,
        todayHighlight : true,
        pickTime: false,
        minView: 2,
        zIndex:9999,
    }).on('changeDate', function(ev)
    {
        mydate=jQuery('.awdatepicker').val();
    });
});



// $(".date").datepicker("setDate", -1)

    $(document).on('click',function (event) {
        var modal = document.getElementById("myModal");;
//
        if (event.target == modal) {
                $('#myModal').modal('hide');
                // $('.modal-backdrop').modal('hide');
                $('.modal-backdrop').remove();
        }

});


    //سداد
$('body').on('click', '#add_pay', function () {

    var id = $(this).data('id');

    $.post("premiumAjaxController.php?do=getpremiumdata", {id: id}, function (data) {
        var obj = jQuery.parseJSON( data );


        var azmy = { 'premiumid' : obj.id,'lastDate': obj.date, "payedDept" : obj.value,"premiumDetailId" : obj.premiumclientId,"debtBefore" : obj.clientdebt,"clientid" : obj.clientid,
            "comment" : "دفعه سداد لمبلغ : "+ obj.value  +  "  وتاريخه : " + obj.date,"clientpremium" : '1'};
        localStorage.setItem("azmy", JSON.stringify(azmy));

    });

});


$(document).ready(function(){
    const listJSON = localStorage.getItem('azmy')
    if (listJSON !== null) {
        list = JSON.parse(listJSON)

            window.document.getElementById("premiumid").value= list.premiumid;
            window.document.getElementById("premiumDetailId").value= list.premiumDetailId;
            window.document.getElementById("lastDate").value= list.lastDate;
            window.document.getElementById("payedDept").value= list.payedDept;
            window.document.getElementById("txtAmountxx").value= list.payedDept;
            window.document.getElementById("debtBefore").value= list.debtBefore;
            var debtAfter = list.debtBefore - list.payedDept ;
            window.document.getElementById("debtAfter").value= debtAfter;

        var yourValue = list.clientid;
            $("#clientid").val(yourValue).trigger("change");
            window.document.getElementById("comment1").value= list.comment;
            window.document.getElementById("clientpremium").value= list.clientpremium;

    }

    localStorage.removeItem("azmy");

});

//تأجيل

$(document).on('click', '#delay', function () {

    var id = $(this).data('id');

    $.post("premiumAjaxController.php?do=getpremium", {id: id}, function (data) {
        var obj = jQuery.parseJSON( data );
        $('#id').val(obj.id);
        $('#premiumclientId').val(obj.premiumclientId);
        $('#date').val(obj.date);
        $('#Amount').val(obj.value);
        $('#conditions').val(obj.conditions);
        $('#payed').val(obj.payed);

    });

});




////////////////////////////////////////////////////////////// step 2

$(document).on('change', '.clientID', function () {

    var clientID = $(this).val();

    $.post("premiumAjaxController.php?do=getClientdata", {clientID: clientID}, function (data) {
        var obj = jQuery.parseJSON( data );
        // $('#table tbody').empty();
        // $('#table tbody').append('<tr>'
        $('.name').html(obj.clientname);
        $('.mobile').html(obj.clientmobile);
        $('.area').html(obj.areaName);
        $('.type').html(obj.typeName);
        $('.debt').html(obj.clientdebt);
        $('.comment').html(obj.clientdetails);

//
    });

});



//تحويل من مياشر الى نسبه
$(document).on('change keyup', '#payment_dir', function () {

        var payment_dir = $('#payment_dir').val();//مباشر
        var first_payment = $('#first_payment').val();
        var piecesNum = 0;
        var total = 0;

        piecesNum += (payment_dir * 100) / first_payment;
        var roundnum = Math.round(piecesNum);
        $('#payment_Per').val( roundnum + ' %');

        //مجموع باقي المبلغ
         total += first_payment - payment_dir
        $('#rest_payment').val(total);

});


//تحويل من نسبه الى مباشر
$(document).on('change keyup', '#payment_Per', function () {

        var payment_Per = $('#payment_Per').val();//نسبه
        var first_payment = $('#first_payment').val();
        var Num = 0;
        var total = 0;


        Num += ( payment_Per * first_payment) / 100;
        $('#payment_dir').val(Num);

        //مجموع باقي المبلغ
        total += first_payment - Num
        $('#rest_payment').val(total);
});




$('body').on('change keyup', '.cal_installment_values', function () {
    var rest_payment = $('#rest_payment').val();
    var installment_date = $('#installment_every_date').val();
    var installment_nos = $('#installment_nos').val();
    var installment_months = $('#installment_months').val();
    var installment_days = $('#installment_days').val();

    $.post("premiumAjaxController.php?do=installmentDates", {rest_payment: rest_payment, installment_date: installment_date, installment_nos: installment_nos
        , installment_months: installment_months,installment_days:installment_days}, function (rslt) {
        console.log(rslt);
        $('#installment_show_container').html(rslt);
    });
});



//سداد
$(document).on('click', '#add_pay_auto', function () {

    var id = $(this).data('id');

    $.post("premiumAjaxController.php?do=getpaypremiumautodata", {id: id}, function (data) {
        var obj = jQuery.parseJSON( data );
        var luckys = { 'premiumid' : obj.idInstallment,'lastDate': obj.date, "debtBefore" : obj.value,"clientid" : obj.clientid,
            "comment" : "دفعه مقدم بمبلغ : "+ obj.value  +  "  وتاريخه : " + obj.date,"clientpremium" : '1'};
        localStorage.setItem("lucky", JSON.stringify(luckys));

    });

});


$(document).ready(function(){

    const listJSON = localStorage.getItem('lucky')
    if (listJSON !== null) {
        list = JSON.parse(listJSON)
        alert(list.debtBefore)

        window.document.getElementById("premiumid").value= list.premiumid;
        window.document.getElementById("lastDate").value= list.lastDate;
        var clientid = list.clientid;
        $("#clientid").val(clientid).trigger("change");
        window.document.getElementById("debtBefore").value= list.debtBefore;
        window.document.getElementById("comment1").value= list.comment;
        window.document.getElementById("clientpremium").value= list.clientpremium;

    }

    localStorage.removeItem("lucky");

});

//تأجيل

$(document).on('click', '#delay_auto', function () {

    var id = $(this).data('id');

    $.post("premiumAjaxController.php?do=getdelaypremiumauto", {id: id}, function (data) {
        var obj = jQuery.parseJSON( data );
        $('#idInstallment').val(obj.idInstallment);
        $('#premiumauoId').val(obj.premiumauoId);
        $('#date').val(obj.date);
        $('#Amount').val(obj.value);
        $('#conditions').val(obj.conditions);
        $('#payed').val(obj.payed);

    });

});



$(document).on('change', '.clientPremium', function () {

    var clientID = $(this).val();


    $.post("premiumAjaxController.php?do=checkPremiumClient", {clientID: clientID}, function (data) {
            var nu = Number(data);

        if (nu == 1){
            $('.clientNotPremium').hide();
            $('.clientFindPremium').show();
            $('.notPremium').val('');
            $('.Premium').val('Premium');
            $(".nopremiumpayedDeptCurr").removeAttr('id');
            $('.nopremiumpayedDept').removeAttr('id');





        } else{
            $('.clientNotPremium').show();
            $('.clientFindPremium').hide();
            $('.notPremium').val('notPremium');
            $('.Premium').val('');
            $('.nopremiumpayedDeptCurr').attr('id', 'payedDeptCurr');
            $('.nopremiumpayedDept').attr('id', 'payedDept');
        }

//
    });

});


$(document).on('change', '.clientPremium', function () {

    var clientID = $(this).val();

    $.post("premiumAjaxController.php?do=checkPremiumClientData", {clientID: clientID}, function (data) {
        $('.Premium-client').empty();
        $('.Premium-client').append(data)

    });

});


