 function deleteData(id) {
    var url = base_url + '/delete'
     deleteEntityRecord(id, url)
 }

 function restoreData(id) {
     var url = base_url + '/restore'
     restoreEntityRecord(id, url)
 }


$('#inv-sample-id').change(function () {
    sample_id = $(this).val()
})

function generate_invoice () {
    total_amt = 0
    if(invoice_id == 0) { /*generate_uuid ('INV', '#invoice-number');*/}
     $.ajax({
         url: get_refs_base_url + '/test-items/' + sample_id,
         type: "GET",
         dataType: "json",
         success: function (response) {
             if(response.success){
                 let res = response.data;
                 $('#invoice-item').empty();
                 for (var i in res.sample_tests){
                     //let heading = {"sample": res.sample, "department": res.sample_tests[i].department_name}
                     let samples_list = res.sample_tests[i].samples;
                     for(var j in samples_list){
                       //  renderPrintTestItemDepartmentRow(samples_list[j], heading,'sample-result-print')
                         var tests = samples_list[j].tests
                         var sampleTest = list_to_tree(tests)
                         sampleTest.map(parent => {
                             renderInvoiceTestItemRow(parent, 'invoice-item')
                             var children = parent.children
                             children.map(function (child) {
                                 renderInvoiceTestItemRow(child, 'invoice-item')
                                 if (child.children.length) {
                                     var grandchildren = child.children
                                     grandchildren.map(function (grandchild) {
                                         if(grandchild.group_result !="") {
                                             renderInvoiceTestItemRow(grandchild, 'invoice-item')
                                         }
                                     })
                                 }
                             });
                         })
                     }
                 }

             }
         }
     });
 }

 let total_amt = 0.00
 let discount_amt = 0.00
 let total_net_amt = 0.00

 function renderInvoiceTestItemRow(sample_tests, closet_parent_id)
 {
     if(sample_tests.group_result!=="" && sample_tests.usd_price>0){
         let net_amount = calculateInvoicePrice(sample_tests);
         let tr = document.createElement('TR');
         tr.insertCell(0).innerHTML = sample_tests.group_result + manipulateControlInputFields(sample_tests);
         tr.insertCell(1).innerHTML = sample_tests.commission_rate;
         tr.insertCell(2).innerHTML = (currency==1?'៛ ' + Math.ceil((sample_tests.usd_price * exchange_rate)/100) * 100:'$ ' + sample_tests.usd_price/* toFixedNumber(net_amount,2)*/);
         tr.cells[0].classList.add('py-2','px-3')
         tr.cells[1].classList.add('py-2','text-right')
         tr.cells[2].classList.add('py-2','text-right')
         $('#'+closet_parent_id).append(tr);

         total_amt += (currency==1 ? Math.ceil((parseFloat(sample_tests.usd_price) * exchange_rate)/100) * 100 : parseFloat(sample_tests.usd_price) /*toFixedNumber(net_amount,2)*/)
         console.log('total_amt', total_amt)
         total_net_amt += (currency==1 ? Math.ceil((parseFloat(net_amount) * exchange_rate)/100) * 100 : parseFloat(net_amount)) 
         discount_amt = total_amt - total_net_amt
         $('#total-amount').val(toFixedNumber(total_amt,2))
         $('#net-amount').val(toFixedNumber(total_net_amt ,2))
         //console.log('total_net_amt',toFixedNumber(total_net_amt,2));
         $('#discount-amount').val(toFixedNumber(discount_amt,2))
             
         /*if( $('#discount-amount').val()<=0.00){
             $('#net-amount').val(toFixedNumber(total_net_amt,2))
             $('#owe-amount').val(toFixedNumber(total_net_amt,2))
         } else{
             $('#net-amount').val(toFixedNumber(total_net_amt,2))
         }*/
     }
 }

 function calculateInvoicePrice(sample_tests){
     let net_amount = 0
     if(sample_tests.commission_type == 2){
         net_amount = (parseFloat(sample_tests.usd_price)) - parseFloat(sample_tests.commission_rate)
     }
     else{
         if(parseInt(sample_tests.commission_rate)>0){
         // if commission_type is % sample_tests.commission_rate is decimal number Ex: 10% = 0.1, 1 = 100%
            net_amount = parseFloat(sample_tests.usd_price) * (1 - parseFloat(parseInt(sample_tests.commission_rate)/100))
         }
         else{
             net_amount = parseFloat(sample_tests.usd_price)
         }
     }
     return net_amount
 }

 function manipulateControlInputFields(sample_tests){
        let net_amount = calculateInvoicePrice(sample_tests);
        let inputs = '<input name="test_sample_id[]" type="hidden" value="'+sample_tests.id+'" />';
        inputs += '<input name="test_name[]" type="hidden" value="'+sample_tests.group_result+'" />';
        inputs += '<input name="item_discount_type[]" type="hidden" value="'+sample_tests.commission_type+'" />';
        inputs += '<input name="item_discount[]" type="hidden" value="'+sample_tests.commission_rate+'" />';
        inputs += '<input name="unit_price[]" type="hidden" value="'+(currency==1 ? Math.ceil((sample_tests.usd_price * exchange_rate)/100) * 100 : toFixedNumber(sample_tests.usd_price,2))+'" />';
        return inputs;
 }

 $('#discount-amount').change(function () {
     let net_amount = 0;
     if($('#discount_type').val()==1){
         //net_amount = parseFloat($('#total-amount').val()) - parseFloat($('#discount-amount').val())
         net_amount = parseFloat($('#total-amount').val()) * (1 - (parseFloat($('#discount-amount').val())/100))
     }
     else{
         net_amount = parseFloat($('#total-amount').val()) - parseFloat($('#discount-amount').val())
     }
     //let net_amount = parseFloat($('#total-amount').val()) - parseFloat($('#discount-amount').val())
     //console.log(Math.ceil((net_amount/100))*100)
     let finalAmount = currency==1 ? Math.ceil((net_amount)/100) * 100 : toFixedNumber(net_amount,2)
     $('#net-amount').val(finalAmount)
     $('#owe-amount').val(finalAmount)
 })

 $('#paid-amount').change(function () {
     let own_amount = parseFloat($('#net-amount').val()) - parseFloat( $('#paid-amount').val())
     $('#owe-amount').val(toFixedNumber(own_amount,2))
 })

 if(invoice_id > 0){
     //generate_invoice()
     // $('#invoice-number').val(invoice.invoice_code)
     // $('#invoice_date').val(invoice.invoice_date)
     // $('#total-amount').val(invoice.total)
     // $('#discount-amount').val(invoice.discount)
     // $('#net-amount').val(invoice.gross_total)
     // $('#paid-amount').val(invoice.deposit)
     // $('#paid-date').val(invoice.first_paid_date)
     // $('#owe-amount').val(invoice.balance)
 }

 $('.inv-report-filter').click(function(){
     let report_type = $(this).attr('data-report-type')
     let rpt_type = (report_type == 'daily' ? report_type_daily : (report_type == 'monthly' ?  report_type_monthly: report_type_yearly))
     $('#modal-invoice-report-filter #invoice-name').html(report_type_tittle+ ': '+ rpt_type)
     if(report_type == 'daily'){ $('#daily-val').removeClass('d-none'); $('#monthly-val, #yearly-val').addClass('d-none')}
     else if(report_type == 'monthly') { $('#monthly-val').removeClass('d-none'); $('#daily-val, #yearly-val').addClass('d-none')}
     else { $('#yearly-val').removeClass('d-none'); $('#daily-val, #monthly-val').addClass('d-none')}
 });

 $('#modal-invoice-report-filter').on('show.bs.modal', function (event) {

 });
