var table ,  block = $(".content-wrapper").parent();
    var HTML_VIEW_PAY = '';
    var print_page =  `
                        <script>
                          window.print();
                          window.onload = function() { window.print(); }
                        </script>
                        `;
    var HTML_Table = `
                      <!DOCTYPE html>
                      <html lang="ar" dir="rtl">
                      <head>
                        <meta charset="utf-8">
                        <meta http-equiv="X-UA-Compatible" content="IE=edge">
                        <style>

                          .print {  
                            -webkit-print-color-adjust: exact;
                          }
                          
                          .table0_td {
                            
                            border:none;
                            padding:0in 0pt 0in 0pt;
                            height:9.95pt
                            
                          }
                          
                          .table0_td1 {
                            
                            border:none;
                            border-bottom:solid #984806 1.0pt;
                            padding:0in 0pt 0in 0pt;
                            height:9.95pt
                            
                          }
                          
                          
                          
                          .table1_td {
                            
                            border:solid #984806 1.0pt;
                            border-left-color: white;
                            background:#984806;
                            padding:0pt 0pt 0pt 0pt;
                            height:.1in
                            
                          }
                          
                          .last_td {
                            border-left-color: #984806 !important;
                          }
                          
                          .table1_td1 {
                            
                            border:solid #984806 1.0pt;
                            border-top:none;
                            background:white;
                            padding:0pt 0pt 0pt 0pt;
                            height:.1in
                            
                          }
                          
                          .table2_td {
                            
                            border:solid #984806 1.0pt;
                            border-left-color: white;
                            background:#984806;
                            padding:0pt 0pt 0pt 0pt;
                            height:.1in;
                            
                          }
                          
                          .td_head {
                            text-align: center;
                            
                            color : white;
                            height: .3in;
                          }
                          
                          .td_body {
                            text-align: center;
                            
                            color : black;
                            height: .2in;
                            border:solid #984806 1.0pt;
                          }
                          
                          #inv_table tr:nth-child(odd) {
                            background-color: #FDE9D9;
                          }
                          
                          @font-face {
                            font-family: "Noto Kufi Arabic";
                            src: url(assets/custom_font/NotoKufiArabic-Regular.ttf) format("truetype");
                            }
                          
                          .badge {
                            display: inline-block;
                            min-width: 10px;
                            padding: 3px 7px;
                            font-size: 12px;
                            font-weight: 500;
                            color: #fff;
                            line-height: 1.5384616;
                            vertical-align: middle;
                            white-space: nowrap;
                            text-align: center;
                            background-color: #999999;
                            border-radius: 10px;
                          } 
                          
                          .badge {
                            padding: 1px 1px 1px 1px;
                            font-size: 10px;
                            letter-spacing: 0px;
                            vertical-align: baseline;
                            
                          }
                          
                          .bg-orange-400 {
                            background-color: #FFA726;
                            border-color: #FFA726;
                            color: #fff;
                          }
                          
                          .col-sm-4 {
                            width: 33.33333333%;
                          }
                          
                          .col-sm-3 {
                            width: 25%;
                          }

                        </style>

                      </head>

                      <body class="print" style=" font-family: 'Noto Kufi Arabic', Times, serif;">
                        <div align=center>
                      `;

    



    var product_list_temp;
    var other_list_temp;


   

    
    function show_invoice(itm,printing=null,detail_show=null){

          var headers           ='';
          var attach            = "";
          var attach_other      = "";
          var footers           ="";
          var customer_details  = "";
          var print_attach      = "";

          $.ajax({
                url : 'models/crul_invoice_deleted.php',
                type : 'POST',
                data : {type:'show_invoice', id : itm},
                dataType : 'json',
                success : function(response) {
                    // console.log(response)
                    
                        headers =`

                                     <table  dir=rtl border=1 cellspacing=0 cellpadding=0
                                       style='background:white;border-collapse:collapse;border:none; font-weight:bold;'>
                                       <!--<tr>
                                        <td width=590 colspan=6 style='width:6.15in;border:none;padding:0in 5.4pt 0in 5.4pt'>       
                                          <span style="float: left; font-size: 20px;" id="inv_slogan">`+COMPANY_NAME+`</span>
                                        </td>
                                       </tr>-->
                                       <tr style='height:9.95pt'>
                                        <td width=295 colspan=3 style='width:221.4pt;' class="table0_td">
                                        `+Translation('رقم الوصل')+`  :
                                        <span id="inv_num" style="color:red;">`+response.invoice[0].invoice_number+`</span>
                                        <br>
                                        `+Translation('التــــــــــــاريخ')+`  :  
                                        <span id="inv_date">`+response.invoice[0].invoice_date+`</span>

                                        </td>
                                        <td width=295 align="center" colspan=2 rowspan=3 style='width:221.4pt;' class="table0_td">
                                        <span id="inv_logo"> 
                                        <img src="`+COMPANY_LOGO+`" height="150px" align="left">
                                         </span>
                                        </td>
                                       </tr>
                                       
                                       <tr style='height:9.95pt'>
                                        <td width=295 colspan=4 style='width:221.4pt;' class="table0_td">
                                       `+Translation('موبايل')+`  : <span dir="ltr" id="inv_phone"> `+COMPANY_PHONE+` </span>
                                        <br>
                                       `+Translation('البريد الالكتروني')+`  : <span dir="ltr" id="inv_email"> `+COMPANY_EMAIL+` </span>
                                        </td>
                                       </tr>
                                       
                                       <tr style='height:9.95pt'>
                                        <td colspan=6 width=118 style='width:88.55pt;' class="table0_td1">
                                            <span id="inv_address"> `+Translation(COMPANY_ADDRESS)+` </span>
                                          
                                        </td>
                                       </tr>
                                       
                                       
                                      </table>
                                      <br>
                                          `;
                        
                        customer_details = `

                                            <table  dir=rtl border=1 cellspacing=0 cellpadding=0  width=595 style='width:6.2in;border-collapse:collapse;border:none'>
                                             <tr style='page-break-inside:avoid;' class="td_head">
                                              <td width=307 style='width:3.2in;' class="table1_td">
                                              `+Translation('اﺳﻢ اﻟﺰﺑﻮﻥ')+`
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td">
                                              `+Translation('ﺭﻗﻢ اﻟﻬﺎﺗﻒ')+`
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td last_td">
                                              `+Translation('اﻟﻌﻨﻮاﻥ')+`
                                              </td>
                                             </tr>
                                             <tr style='page-break-inside:avoid;' class="td_body">
                                              <td width=307 style='width:3.2in;' class="table1_td1">
                                              <span  id="inv_customer_name"> `+response.invoice[0].customer_full_name+` </span>
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td1">
                                              <span  id="inv_customer_phone"> `+response.invoice[0].customer_phone+` </span>
                                              </td>
                                              <td width=144 style='width:1.5in;' class="table1_td1">
                                              <span  id="inv_customer_address"> `+response.invoice[0].customer_address+` </span>
                                              </td>
                                             </tr>
                                            </table>
                                            <br>
                                            `;

                      attach =  `
                                  <table id="inv_table"  dir=rtl border=1 cellspacing=0 cellpadding=0 width=591 style='width:443.5pt;border-collapse:collapse;border:#000000; '>
                                   <tr style='page-break-inside:avoid;' class="td_head">
                                    <td width=23 style='width:14.4pt;' class="table2_td">
                                   `+Translation('ﺕ')+` 
                                    </td>
                                    <td width=68 style='width:50.7pt;' class="table2_td">
                                   `+Translation('اﻟﻤﺎﺩﺓ')+` 
                                    </td>
                                    <td width=64 style='width:48.3pt;' class="table2_td">
                                   `+Translation('اﻟﻜﻤﻴﺔ')+` 
                                    </td>
                                    <td width=114 style='width:85.5pt;' class="table2_td">
                                   `+Translation('اﻟﺘﻔﺎﺻﻴﻞ')+` 
                                    </td>
                                    <td width=87 style='width:65.25pt;' class="table2_td">
                                   `+Translation('اﻟﻄﻮﻝ اﻟﻜﻠﻲ')+` 
                                    </td>
                                    <td width=73 style='width:54.9pt;' class="table2_td">
                                   `+Translation('ﺳﻌﺮ اﻟﻮﺣﺪﺓ')+` 
                                    </td>
                                    <td width=79 style='width:58.95pt;' class="table2_td">
                                   `+Translation('ملاحظات')+` 
                                    </td>
                                    <td width=74 style='width:55.5pt;' class="table2_td last_td">
                                   `+Translation('اﻟﻤﺠﻤﻮﻉ')+` 
                                    </td>
                                   </tr>`;
                     
                      // show sell item details                       
                      for(var i=0 ; i < response.invoice.length;i++){

                        // compute and format discount
                        var disss = 0;
                        if(response.invoice[i].sell_discount != '0.00'){
                          if(response.invoice[i].sell_discount_type == 0){
                          var disss = response.invoice[i].sell_discount + " %";
                          }else{
                            var disss = currency_format_display(response.invoice[i].sell_discount);
                          }
                        }

                        // compute total row price

                        var total_row_price = 0;

                        if(response.invoice[i].other_id_fk > 0){
                          total_row_price = response.invoice[i].sell_item_quantity * response.invoice[i].item_sell_price_per_meter - response.invoice[i].actual_discount;
                          total_row_price = currency_format_display(total_row_price);
                        
                        }else{
                          total_row_price = response.invoice[i].total_length * response.invoice[i].item_sell_price_per_meter - response.invoice[i].actual_discount;
                          total_row_price = currency_format_display(total_row_price);
                        }



                        if(response.invoice[i].product_id_fk > 0){
  
                           // display sell item details
                        
                          var Det = '';
                          if(detail_show != null){

                            for(var j=0 ; j < response.details.length;j++){
                              var sell_id = response.invoice[i].sell_item_id;

                              if(sell_id == response.details[j].sell_item_id_fk){
                                // Det = Det + ` <input types="text"  class=" col-sm-4" value="`+response.details[j].sell_item_detail_height+`" readonly> `;
                                Det = Det + ` <span style="border: 1px solid black"  class="">`+response.details[j].sell_item_detail_height+`</span> `;
                              }
                            }
                          }


                          attach = attach + `
                                                  <tr style='page-break-inside:avoid;' class="td_body">
                                                    <td width=23 style='width:14.4pt;'>
                                                      <span  id="inv_count"> `+(+i +1)+` </span>
                                                    </td>
                                                    <td width=68 style='width:50.7pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].product_name+` </span>
                                                    </td>
                                                    <td width=64 style='width:48.3pt;'>
                                                      <span  id="inv_quantity"> `+parseInt(response.invoice[i].sell_item_quantity)+` </span>
                                                    </td>
                                                    <td width=114 style='width:85.5pt; font-family:  Times, serif;'>
                                                      <span  id="inv_details">
                                            `+Det+`         </span>
                                                    </td>
                                                    <td width=87  style='width:65.25pt;'>
                                                      <span  id="inv_total_length"> `+response.invoice[i].total_length+` ﻡ </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].item_sell_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=79 style='width:58.95pt;'>
                                                      <span  id="inv_discount">`+ response.invoice[i].sell_item_notes +` </span>
                                                    </td>
                                                    <td width=74 style='width:55.5pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+total_row_price+` </span>
                                                    </td>
                                                   </tr>
                                                  `;
                        
                        }else{ // other product

                          attach_other = attach_other + `
                                                  <tr style='page-break-inside:avoid;' class="td_body">
                                                    <td width=23 style='width:14.4pt;'>
                                                      <span  id="inv_count"> `+(response.invoice.length)+` </span>
                                                    </td>
                                                    <td width=68 style='width:50.7pt;'>
                                                      <span  id="inv_product"> `+response.invoice[i].product_name+` </span>
                                                    </td>
                                                    <td width=64 style='width:48.3pt;'>
                                                      <span  id="inv_quantity"> `+response.invoice[i].sell_item_quantity+` </span>
                                                    </td>
                                                    <td width=114 style='width:85.5pt;'>
                                                      <span  id="inv_details">
                                                      </span>
                                                    </td>
                                                    <td width=87  style='width:65.25pt;'>
                                                      <span  id="inv_total_length">  </span>
                                                    </td>
                                                    <td width=73 style='width:54.9pt;'>
                                                      <span  id="inv_unit_price"> `+currency_format_display(response.invoice[i].item_sell_price_per_meter)+` </span>
                                                    </td>
                                                    <td width=79 style='width:58.95pt;'>
                                                      <span  id="inv_discount">`+ response.invoice[i].sell_item_notes +` </span>
                                                    </td>
                                                    <td width=74 style='width:55.5pt;' class="last_td">
                                                      <span  id="inv_total_row"> `+total_row_price+` </span>
                                                    </td>
                                                   </tr>
                                                  `;

                        }
                       
                      } // for i

                      if(printing != null){
                        print_attach = `
                                        <script>
                                          window.print();
                                          window.onload = function() { window.print(); }
                                        </script>
                                        `;
                      }else{
                        print_attach = "";
                      }

                      footers =`<tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=439 colspan=7  style='width:329.05pt;border-top:solid #984806 1.0pt;
                                  border-left:solid #984806 1.0pt;border-bottom:none;border-right:none;
                                  background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                 `+Translation('اﻟﺘﻨﺰﻳﻼﺕ')+` 
                                  </td>
                                  

                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                   `+currency_format_display(response.invoice[0].total_discount)+`
                                  </td>

                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=7 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                 `+Translation('اﻟﻤﺒﻠﻎ اﻟﻜﻠﻲ ﻟﻠﻮﺻﻞ')+` 
                                  </td>
                                  <td width=74 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                   `+currency_format_display(response.invoice[0].invoice_total_price_actual)+`
                                  </td>
                                 </tr>

                                 <tr style='page-break-inside:avoid;height:.1in; background: white;'>
                                  
                                  <td width=591 colspan=8 style='width:443.5pt;border:none;
                                  border-bottom:solid #984806 1.0pt; background: white;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                   &nbsp;
                                  </td>
                                  
                                 </tr>


                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=6 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in'>
                                 `+Translation('ﺣﺴﺎﺏ اﻟﺰﺑﻮﻥ اﻟﻜﻠﻲ')+` 
                                  </td>
                                  <td width=74 colspan=2 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(response.account[0].total_invoice_price_all)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=6 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in; color:#2b9408'>
                                 `+Translation('اﻟﻤﺒﻠﻎ اﻟﻜﻠﻲ اﻟﻤﺪﻓﻮﻉ')+` 
                                  </td>
                                  <td width=74 colspan=2 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(response.account[0].total_Paid_all)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.1in'>
                                  <td width=517 colspan=6 valign=top style='width:388.0pt;border:none;
                                  border-left:solid #984806 1.0pt;background:white;padding:2.15pt 5.75pt 2.15pt 5.75pt;
                                  height:.1in; color:#b92909'>
                                 `+Translation('اﻟﻤﺒﻠﻎ اﻟﻜﻠﻲ اﻟﻤﺘﺒﻘﻲ')+` 
                                  </td>
                                  <td width=74 colspan=2 style='width:55.5pt;border-top:none;border-left:solid #984806 1.0pt;
                                  border-bottom:solid #984806 1.0pt;border-right:none;
                                  padding:0pt .1in 0pt .1in;height:.1in'>
                                  `+currency_format_display(response.account[0].total_remained_all)+`
                                  </td>
                                 </tr>
                                 <tr style='page-break-inside:avoid;height:.3in'>
                                 
                                  <td width=517 colspan=8  style='width:388.0pt;border:none;background:white;
                                    padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                    <span id="inv_note">`+Translation('ﻣﻼﺣﻈﺔ')+` : `+response.invoice[0].invoice_note+` </span>
                                  </td>
                                 </tr>
                                 
                                 <tr style='page-break-inside:avoid;height:.1in; background-color: white'>
                                  <td width=517 colspan=7  style='width:388.0pt;border:none;
                                  padding:2.15pt 5.75pt 2.15pt 5.75pt;height:.1in'>
                                  
                                  </td>
                                  <td width=74 style='width:55.5pt;border:none;padding:2.15pt .15in 2.15pt .15in;
                                  height:.1in'>
                                 `+Translation('اﻟﺘﻮﻗﻴﻊ')+` 
                                  </td>
                                 </tr>
                                </table>

                                </div>




                                </body>

                                `+print_attach+`

                                </html>`;

                      

                     
                },
                complete: function() {
                       //  me.data('requestRunning', false);
                       HTML_VIEW = HTML_Table + headers + customer_details + attach + attach_other + footers;
                       //  me.data('requestRunning', false);
                        window.open().document.write(HTML_VIEW);
                }

          }); // submit ajax


    }

    

   


    $('#print_pay').click(function(e) {
      e.preventDefault();
         print_invoice('Print','pay');
    });

    $('#pdf_pay').click(function(e) {
      e.preventDefault();
         print_invoice('PDF','pay');
    });


   

    function print_invoice(typ,payment=null){
      if(typ=='Print'){
        window.open().document.write(HTML_VIEW_PAY+print_page);
      }else if(typ=='PDF'){
        var nn = $("#inv_numb").val();
        pdf_generator(HTML_VIEW_PAY,nn,'pay')
      }else{
        window.open().document.write(HTML_VIEW_PAY);
      }
    }

    function pdf_generator(HTML,INV_NO,payment){

                $.ajax({
                      url : 'models/crul_invoice.php',

                      type : 'POST',
                      data : {type:'PDF',html:HTML,inv_no:INV_NO,pay:payment},
                      dataType : 'json',
                      success : function(response) {
                            console.log(response)
                            if(response.is_success == true) {

                                    noty({
                                        width: 200,
                                        text: Translation('ﺗﻢ ﺗﻮﻟﻴﺪ ﻣﻠﻒ اﻝـ PDF ﺑﻨﺠﺎﺡ'),
                                        type: 'success',
                                        dismissQueue: true,
                                        timeout: 3000,
                                        layout: 'top',
                                    });
                                    var LINKS = '<a class="btn btn-labeled btn-labeled-left heading-btn btn-info" href="'+response.data+'" target="_blank" style="float: right;"> '+Translation('Download')+' <b><i class="icon-file-download"></i></b> </a>';
                                    if(payment !=null){
                                      $("#link_pdf_pay").append(LINKS);
                                    }else{
                                      $("#link_pdf").append(LINKS);
                                    }

                                  
                                  $("#pdf").attr('disabled',false);
                                  $("#pdf_pay").attr('disabled',false);
                                  $("#print").attr('disabled',false);
                                  $("#print_pay").attr('disabled',false);
                            } else {

                               noty({
                                     width: 200,
                                     text: Translation(response.message),
                                     type: 'error',
                                     dismissQueue: true,
                                     timeout: 3000,
                                     layout: 'top',
                               });

                                  $("#pdf").attr('disabled',false);
                                  $("#pdf_pay").attr('disabled',false);
                                  $("#print").attr('disabled',false);
                                  $("#print_pay").attr('disabled',false);
                            }
                      },
                      complete: function() {
                            //   me.data('requestRunning', false);
                      }

                }); // submit ajax
    }

   
    var products_array_values =[] ;

    // make products as searchable list

    

    


    







    // End editing invoice section

$(document).ready(function() {
  
   


    // handling two level of modal in same page 

    $('.modal').on('hidden.bs.modal', function (e) {
        if($('.modal').hasClass('in')) {
        $('body').addClass('modal-open');
        }    
    });

   









});







$(function() {
//var singleSelect = $('.datatable-column-search-inputs').DataTable();
    
  $(".touchspin-basic").TouchSpin({
        min: 1,
        max: 10000000,
        step: 1,
        buttondown_class: "btn btn-xs",
        buttonup_class: "btn btn-xs"
    });

  $('.daterange-single').daterangepicker({ 
        singleDatePicker: true,
        locale: {
            format: 'YYYY-MM-DD'
        }
    });

  // Mini
    $('.select-size-xs').select2({
        containerCssClass: 'select-xs'
    });


   // Setting datatable defaults
    $.extend( $.fn.dataTable.defaults, {
        autoWidth: false,
        columnDefs: [{ 
            orderable: false,
            width: '100px',
            targets: [ 5 ]
        }],
        dom: '<"datatable-header"fBl><"datatable-scroll"t><"datatable-footer"ip>',
        language: {
            search: '<span>'+Translation('ﺑﺤﺚ')+':</span> _INPUT_',
            searchPlaceholder: Translation('ﺑﺤﺚ ﻋﻦ')+'...',
            lengthMenu: '<span>'+Translation('ﻋﺮﺽ')+':</span> _MENU_',
            paginate: { 'first': 'First', 'last': 'Last', 'next': '&larr;', 'previous': '&rarr;' }
        },
        drawCallback: function () {
            $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').addClass('dropup');
        },
        preDrawCallback: function() {
            $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').removeClass('dropup');
        }
    });


    

    // Individual column searching with text inputs
    $('.datatable-column-search-inputs tfoot td').not(':last-child').each(function () {
        var title = $('.datatable-column-search-inputs thead th').eq($(this).index()).text();
        $(this).html('<input type="text" class="form-control input-sm" placeholder="'+Translation('ﺑﺤﺚ ﻋﻦ')+' '+title+'" />');
    });
    
    

   var query_ip = getQueryVariable("ip");


  


   var table = $('.datatable-column-search-inputs').DataTable({
        
        buttons: {            
            buttons: [
                {
                    extend: 'copyHtml5',
                    className: 'btn btn-default',
                    exportOptions: {
                        columns: [ 0, ':visible' ]
                    }
                },
                {
                    extend: 'excelHtml5',
                    className: 'btn btn-default',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'pdfHtml5',
                    className: 'btn btn-default',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'print',
                    className: 'btn btn-default',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                    extend: 'colvis',
                    text: '<i class="icon-three-bars"></i> <span class="caret"></span>',
                    className: 'btn bg-blue btn-icon'
                }
                
            ]
        },
        lengthMenu: [
            [  10, 25, 50, 100 ],
            [  '10', '25', '50', '100' ]
        ],
        "processing": true,
        "serverSide": true,
        "ajax": {
        "url" : "models/crul_invoice_deleted.php",
        "data": function(d){
          d.type = "load";
          if(query_ip !='not'){
            d.ip =query_ip ;
          }
        }
        },
        drawCallback: function() {
                  $('[data-popup=tooltip]').tooltip();
                  $('[data-popup=popover-custom]').popover({
                    template: '<div class="popover  border-teal-400"><div class="arrow"></div><h3 class="popover-title bg-teal-400"></h3><div class="popover-content"></div></div>'
                  });

            },
        "columnDefs": [
          {
              "targets": 10,
              "data": null,
              "render": function (data,type,row) {

                            return  `<ul class='icons-list pull-right'>
                                      

                                        <li class='text-primary-600'>
                                            <a  class='text-success-400' onclick="show_invoice(`+row[0]+`,null,'Details')" data-popup='tooltip' data-original-title='`+Translation('ﻋﺮﺽ ﻭﺻﻞ اﻟﺒﻴﻊ ﻣﻊ اﻟﺘﻔﺎﺻﻴﻞ')+`'>
                                                <i class='icon-stack-text'></i>
                                            </a>
                                        </li>

                                        <li class='text-primary-600'>
                                            <a  class='text-info-400' onclick="show_invoice(`+row[0]+`,'Print','Details')" data-popup='tooltip' data-original-title='`+Translation('ﻃﺒﺎﻋﺔ اﻟﻮﺻﻞ ﻣﻊ اﻟﺘﻔﺎﺻﻴﻞ')+`'>
                                                <i class='icon-printer2'></i>
                                            </a>
                                        </li>

                                        <li class='text-primary-600'>
                                            <a  class='text-orange-400' onclick="show_invoice(`+row[0]+`)" data-popup='tooltip' data-original-title='`+Translation('ﻋﺮﺽ ﻭﺻﻞ اﻟﺒﻴﻊ ﺑﺪﻭﻥ اﻟﺘﻔﺎﺻﻴﻞ')+`'>
                                                <i class='icon-calculator'></i>
                                            </a>
                                        </li>

                                        <li class='text-primary-600'>
                                            <a  class='text-pink-400' onclick="show_invoice(`+row[0]+`,'Print')" data-popup='tooltip' data-original-title='`+Translation('ﻃﺒﺎﻋﺔ اﻟﻮﺻﻞ ﺑﺪﻭﻥ اﻟﺘﻔﺎﺻﻴﻞ')+`'>
                                                <i class='icon-printer4'></i>
                                            </a>
                                        </li>
                                        
                                        
                                       
                                    </ul>`;
                        }
          },


            {
               "render": function(data, type, row){


                        // return  `<a onclick="show_invoice(`+row[0]+`)"  data-popup='tooltip' data-original-title='Show Invoice Details'>`+data+`</a>`;
                        return  `<a onclick="load_account_statment('',`+row[0]+`)"  data-popup='tooltip' data-original-title='`+Translation('ﻋﺮﺽ ﺟﻤﻴﻊ ﻛﺸﻮﻓﺎﺕ اﻟﺤﺴﺎﺏ ﻟﻬﺬا اﻟﻮﺻﻞ')+`'>`+data+`</a>`;


               },
               "targets": 1
            },

            {
               "render": function(data, type, row){


                        return  `<a onclick="load_account_statment(`+row[11]+`)"  data-popup='tooltip' data-original-title='`+Translation('ﻋﺮﺽ ﺟﻤﻴﻊ ﻛﺸﻮﻓﺎﺕ اﻟﺤﺴﺎﺏ ﻟﻬﺬا اﻟﺰﺑﻮﻥ')+`'>`+data+`</a>`;


               },
               "targets": 3
            },

            
            // {
            //     "render": function(data, type, row){

            //         return currency_format_display(data);
            //     },
            //     "targets": 4
            // },

            {
                "render": function(data, type, row){

                    return currency_format_display(data);
                },
                "targets": 5
            },

            {
                "render": function(data, type, row){

                    return currency_format_display(data);
                },
                "targets": 6
            },

            {
                "render": function(data, type, row){

                    return currency_format_display(data);
                },
                "targets": 7
            },
            

            {
                "render": function(data, type, row){


                   if(row[8] ==2 ){
                     status=  `<span class="label label-success btn-block ">`+Translation('ﻭاﺻﻞ')+`</span>`;
                   }else if(row[8] ==3){
                     status = '<span class="label bg-purple  btn-block ">'+Translation('ﻣﺮﺟﻊ')+'  </span>';
                   }else if(row[8] ==4){
                     status = '<span class="label bg-violet  btn-block ">'+Translation('ﻋﺮﺽ')+'  </span>';
                   }else{

                        // datepart: 'y', 'm', 'w', 'd', 'h', 'n', 's'

                        //Set the two dates
                        var y2k  = new Date(row[2]);
                        var today= new Date();
                        var days = Date.dateDiff('d', y2k, today); //displays 625
                        //alert(days)
                         if(days >= row[10]){
                             var afterTermDays = days - row[10];
                             if(row[8]==1){
                               status = '<span class="label bg-orange  btn-block btn-xs">'+Translation('ﻭاﺻﻞ ﺟﺰﺋﻴﺎ')+'  ('+afterTermDays+' DAYS OUT)</span>';
                             }else{
                               status = '<span class="label label-danger btn-block btn-xs">'+Translation('ﻏﻴﺮ ﻭاﺻﻞ')+'  ('+afterTermDays+' DAYS OUT)</span>';
                             }

                         }else{
                             var remainedDays = row[10]-days;
                             if(row[8]==1){
                               status = '<span class="label label-info  btn-block btn-xs"> '+Translation('ﻭاﺻﻞ ﺟﺰﺋﻴﺎ')+'  ('+remainedDays+' DAYS REM)</span>';
                             }else{
                               status = '<span class="label label-primary btn-block btn-xs">'+Translation('ﻏﻴﺮ ﻭاﺻﻞ')+'  ('+remainedDays+' DAYS REM)</span>';
                             }

                         }
                   }

                    status=  `<span dir='ltr' class="label label-success btn-block ">Deleted at : `+row[12]+`</span>` + status;

                    return  status;
                },
                "targets": 8
            }
            

           
        ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {


            if(   aData[5] == 1 ){ // outcome type
                $(nRow).children().each(function (index, td) {
                    $(this).addClass('alpha-info');
                });
            }
        },
        "order": [[0,'desc']],
        "displayLength": 25,
      
        initComplete: function () {
         
        // $("div.datatable-header").append(`<a  style="margin-left:15px;" class="btn bg-info-400"  id="headerBtn" onclick="add_item()"> <i class="icon-file-pdf position-right"></i> Add Category </a>`);   
      

            this.api().columns().every( function() {
                var column = this;
                

            });
            escape_notes();
        }
    });

    
    table.columns().every( function () {
        var that = this;
        $('input', this.footer()).on('keyup change', function () {
            that.search(this.value).draw();
        });
    });


    // Single row selection
    
    $('.datatable-column-search-inputs tbody').on('click', 'tr', function() {
        if ($(this).hasClass('success')) {
            $(this).removeClass('success');
        }
        else {
            table.$('tr.success').removeClass('success');
            $(this).addClass('success');
        }
    });


         // change filter to top
    $('.datatable-column-search-inputs tfoot tr').insertAfter($('.datatable-column-search-inputs thead tr'))


    $('.dataTables_length select').select2({
        minimumResultsForSearch: Infinity,
        width: 'auto'
    });


});



