var table ;
    

    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;
                          } 

                          .label {
                              display: inline-block;
                              font-weight: 500;
                              padding: 2px 5px 1px 5px;
                              line-height: 1.5384616;
                              border: 1px solid transparent;
                              text-transform: uppercase;
                              text-align: center;

                              font-size: 10px;
                              letter-spacing: 0.1px;
                              border-radius: 2px;
                          }
                          
                          .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;
                          }

                          .bg-success-400 {
                            background-color: #4CAF50;
                            border-color: #4CAF50;
                            color: #fff;
                          }

                        

                        </style>

                      </head>

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


   


    function add_transfer(){
      $('#transfer_form_cont').modal('toggle');

    }


    function add_profit(){
      $('#profit_form_cont').modal('toggle');

    }


    

  


    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 set_date(){

        var now = new Date();
        var month = (now.getMonth() + 1);               
        var day = now.getDate();
        if (month < 10) 
            month = "0" + month;
        if (day < 10) 
            day = "0" + day;
        var today = now.getFullYear() + '-' + month + '-' + day;
        $('#first_date').val(today);
        $('#second_date').val(today);
    }

   function get_City(){

      $.ajax({
                      url : 'models/crul_city.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="" selected="selected">'+Translation('Select City')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                             

                            }
                            $("#city_id").html(attach);
                            $('#city_id').selectpicker('refresh');
                           

                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function get_Zone(id=0){

      $.ajax({
                      url : 'models/crul_zone.php',
                      type : 'POST',
                      data : {

                              id:id,
                              type:'get_with_id'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="" selected="selected">'+Translation('Select Zone')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                             

                            }
                            $("#zone_id").html(attach);
                            $('#zone_id').selectpicker('refresh');
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function get_Sub_Zone(id=0){

      $.ajax({
                      url : 'models/crul_sub_zone.php',
                      type : 'POST',
                      data : {

                              id:id,
                              type:'get_with_id'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="" selected="selected">'+Translation('Select Sub Zone')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                             

                            }
                            $("#sub_zone_id").html(attach);
                            $('#sub_zone_id').selectpicker('refresh');
                            

                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function get_Quarter(id=0){

      $.ajax({
                      url : 'models/crul_quarter.php',
                      type : 'POST',
                      data : {

                              id:id,
                              type:'get_with_id'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="" selected="selected">'+Translation('اختيار الحي')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                             

                            }
                            $("#quarter_id").html(attach);
                            $('#quarter_id').selectpicker('refresh');
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function get_Street(id=0){

      $.ajax({
                      url : 'models/crul_street.php',
                      type : 'POST',
                      data : {

                              id:id,
                              type:'get_with_id'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="" selected="selected">'+Translation('اختيار الشارع')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                             

                            }
                            $("#street_id").html(attach);
                            $('#street_id').selectpicker('refresh');
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function showLoader(msg){
      $("#loader_msg").html(msg);
      $('#loader_modal').modal('toggle');
    }

    function hideLoader(){
      
      $('#loader_modal').modal('toggle');
    }

    function get_Category(){

      $.ajax({
                      url : 'models/crul_category.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="0" selected="selected">'+Translation('All Category')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                             

                            }
                            $("#category_id").html(attach);
                            
                            $('#category_id').selectpicker('refresh');
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }


    function get_Doctor(){

      $.ajax({
                  url : 'models/crul_doctor.php',
                  type : 'POST',
                  data : {

                          
                          type:'get'
                        },
                  dataType : 'json',
                  success : function(response) {
                      
                      var attach ='<option value="" selected="selected">Select Doctor</option>';
                    
                      for(var i=response.length-1 ; i >= 0;i--){
                          
                          attach = attach + '<option value="'+response[i].doctor_id+'">'+response[i].doctor_name+'</option>';
                         

                      }
                      
                      $("#doctor_id").html(attach);
                      $("#doctor_id").selectpicker('refresh');

                      
                  },
                  complete: function() {

                  }

            }); // submit ajax

    }

    function get_Product(){

      $.ajax({
                      url : 'models/crul_product.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(res) {
                          
                            
                            var attach ='<option value="" selected="selected">'+Translation('All Products')+'</option>';
                          
                            for(var i=res.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+res[i][0]+'">'+res[i][1]+'</option>';
                             

                            }
                            $("#product_id").html(attach);
                           
                            $('#product_id').selectpicker('refresh');
                           

                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function get_Supervisor(){

      $.ajax({
                      url : 'models/crul_user.php',
                      type : 'POST',
                      data : {type:'get_supervisor'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="0" selected="selected">'+Translation('Select Supervisor')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i].staff_id+'">'+response[i].username+'</option>';
                             

                            }
                            $("#supervisor_id").html(attach);
                            
                            $('#supervisor_id').selectpicker('refresh');
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function get_TeamLeader(){

      $.ajax({
                      url : 'models/crul_user.php',
                      type : 'POST',
                      data : {type:'get_team_leader'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="0" selected="selected">'+Translation('Select TeamLeader')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i].staff_id+'">'+response[i].username+'</option>';
                             

                            }
                            $("#teamleader_id").html(attach);
                            
                            $('#teamleader_id').selectpicker('refresh');
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    function get_Saleman(){

      $.ajax({
                      url : 'models/crul_user.php',
                      type : 'POST',
                      data : {type:'get_saleman'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="0" selected="selected">'+Translation('Select Saleman')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i].staff_id+'">'+response[i].username+'</option>';
                             

                            }
                            $("#saleman_id").html(attach);
                            
                            $('#saleman_id').selectpicker('refresh');
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }


    function get_Pharmacy(){

      $.ajax({
                      url : 'models/crul_pharmacy.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="0" selected="selected">'+Translation('Select Pharmacy')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i].pharmacy_id+'">'+response[i].pharmacy_name+'</option>';
                             

                            }
                            $("#pharmacy_id").html(attach);
                            
                            $('#pharmacy_id').selectpicker('refresh');
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

$(document).ready(function() {
  
    get_City();
    set_date();
    get_Category();
    get_Doctor();
    get_Product();
    get_Supervisor();
    get_TeamLeader();
    get_Saleman();
    get_Pharmacy();

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

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


    $("#city_id").on('change',function(e){    
      var ID   = $(this).val();
      get_Zone(ID);
    });

    $("#zone_id").on('change',function(e){    
        var ID   = $(this).val();
        get_Sub_Zone(ID);
    });

    $("#sub_zone_id").on('change',function(e){    
        var ID   = $(this).val();
        get_Quarter(ID);
    });

    $("#quarter_id").on('change',function(e){    
        var ID   = $(this).val();
        get_Street(ID);
    });

 



    $('#product_filter_btn').on('click',function(){
      
    
        

        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var product_id      = $("#product_id").val();
        var pharmacy_id     = $("#pharmacy_id").val();
        var saleman_id      = $("#saleman_id").val();
        var doctor_id       = $("#doctor_id").val();
        var zone_id         = $("#zone_id").val();
        var city_id         = $("#city_id").val();
        var sub_zone_id     = $("#sub_zone_id").val();
        var quarter_id      = $("#quarter_id").val();
        var street_id       = $("#street_id").val();
        
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Product Name</th>
                          <th class="multi_lang">Category</th>
                          <th class="multi_lang">Ordered Qty</th>
                          <th class="multi_lang">Sell Price</th>
                          <th class="multi_lang">Report No.</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Status</th>
                          <th class="multi_lang">Doctor</th>
                          <th class="multi_lang">Rate</th>
                          <th class="multi_lang">Pharmacy</th>
                          <th class="multi_lang">City</th>
                          <th class="multi_lang">Zone</th>
                          <th class="multi_lang">Saleman</th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").html('');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"ordered_product_by_visit",
                        
                        d1          : d1,
                        d2          : d2,
                        product_id  : product_id,
                        pharmacy_id : pharmacy_id,
                        saleman_id  : saleman_id,
                        doctor_id   : doctor_id,
                        city_id     : city_id,
                        zone_id     : zone_id,
                        sub_zone_id : sub_zone_id,
                        quarter_id  : quarter_id,
                        street_id   : street_id

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].product_id+`</td>
                                            <td>`+res.data[i].product_name+`</td>
                                            <td>`+res.data[i].category+`</td>
                                            <td>`+res.data[i].ordered_quantity+`</td>
                                            <td>`+currency_format_display(res.data[i].order_sell_price)+`</td>
                                            <td>`+res.data[i].report_number+`</td>
                                            <td>`+res.data[i].report_date+`</td>
                                            <td>`+res.data[i].is_delivered+`</td>
                                            <td>`+res.data[i].doctor_name+`</td>
                                            <td>`+res.data[i].rate_percent+`</td>
                                            <td>`+res.data[i].pharmacy_name+`</td>
                                            <td>`+res.data[i].city_name+`</td>
                                            <td>`+res.data[i].zone_name+`</td>
                                            <td>`+res.data[i].username+`</td>
                                            

                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].ordered_quantity);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Total Qty</td>
                                            <td id="total_invoice">`+(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });

    $('#offer_doctor_filter_btn').on('click',function(){
      
    
        

        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var product_id      = $("#product_id").val();
        var pharmacy_id     = $("#pharmacy_id").val();
        var saleman_id      = $("#saleman_id").val();
        var doctor_id       = $("#doctor_id").val();
        var zone_id         = $("#zone_id").val();
        var city_id         = $("#city_id").val();
        var sub_zone_id     = $("#sub_zone_id").val();
        var quarter_id      = $("#quarter_id").val();
        var street_id       = $("#street_id").val();
        
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Doctor Name</th>
                          <th class="multi_lang">Specialty</th>
                          <th class="multi_lang">Report No.</th>
                          <th class="multi_lang">Date</th>
                          <th class="multi_lang">Offer</th>
                          <th class="multi_lang">Offer Money</th>
                          <th class="multi_lang">City</th>
                          <th class="multi_lang">Zone</th>
                          <th class="multi_lang">Saleman</th>
                          <th class="multi_lang">Doctor Rated by Saleman</th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").html('');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"doctor_offer",
                        
                        d1          : d1,
                        d2          : d2,
                        product_id  : product_id,
                        pharmacy_id : pharmacy_id,
                        saleman_id  : saleman_id,
                        doctor_id   : doctor_id,
                        city_id     : city_id,
                        zone_id     : zone_id,
                        sub_zone_id : sub_zone_id,
                        quarter_id  : quarter_id,
                        street_id   : street_id

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].report_id+`</td>
                                            <td>`+res.data[i].doctor_name+`</td>
                                            <td>`+res.data[i].doctor_specialty+`</td>
                                            <td>`+res.data[i].report_number+`</td>
                                            <td>`+res.data[i].report_date+`</td>
                                            <td>`+res.data[i].doctor_offer+`</td>
                                            <td>`+res.data[i].offer_money+`</td>
                                            <td>`+res.data[i].city_name+`</td>
                                            <td>`+res.data[i].zone_name+`</td>
                                            <td>`+res.data[i].username+`</td>
                                            <td>`+res.data[i].rating_doctor+`</td>
                                            

                                        </tr>
                                        `;
                      // total_inv = +total_inv + +(res.data[i].ordered_quantity);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });

    $('#product_by_ordered_qty_btn').on('click',function(){
      
    
        

        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var product_id      = $("#product_id").val();
        var pharmacy_id     = $("#pharmacy_id").val();
        var saleman_id      = $("#saleman_id").val();
        var doctor_id       = $("#doctor_id").val();
        var zone_id         = $("#zone_id").val();
        var city_id         = $("#city_id").val();
        var sub_zone_id     = $("#sub_zone_id").val();
        var quarter_id      = $("#quarter_id").val();
        var street_id       = $("#street_id").val();
        
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Product Name</th>
                          <th class="multi_lang">Product Code</th>
                          <th class="multi_lang">Product Company</th>
                          <th class="multi_lang">Category</th>
                          <th class="multi_lang">Total Ordered Qty</th>
                          
                          <th class="multi_lang">Doctor</th>
                          <th class="multi_lang">Pharmacy</th>
                          <th class="multi_lang">City</th>
                          <th class="multi_lang">Zone</th>
                          <th class="multi_lang">Saleman</th>
                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").html('');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"ordered_product_by_qty",
                        
                        d1          : d1,
                        d2          : d2,
                        product_id  : product_id,
                        pharmacy_id : pharmacy_id,
                        saleman_id  : saleman_id,
                        doctor_id   : doctor_id,
                        city_id     : city_id,
                        zone_id     : zone_id,
                        sub_zone_id : sub_zone_id,
                        quarter_id  : quarter_id,
                        street_id   : street_id

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].product_id+`</td>
                                            <td>`+res.data[i].product_name+`</td>
                                            <td>`+res.data[i].product_code+`</td>
                                            <td>`+res.data[i].made_in+`</td>
                                            <td>`+res.data[i].category+`</td>
                                            <td>`+res.data[i].ordered_quantity+`</td>
                                            <td>`+res.data[i].doctor_name+`</td>
                                            <td>`+res.data[i].pharmacy_name+`</td>
                                            <td>`+res.data[i].city_name+`</td>
                                            <td>`+res.data[i].zone_name+`</td>
                                            <td>`+res.data[i].username+`</td>
                                            

                                        </tr>
                                        `;
                      total_inv = +total_inv + +(res.data[i].ordered_quantity);

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Total Qty</td>
                                            <td id="total_invoice">`+(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });

    $('#saleman_visit_btn').on('click',function(){
      
    
        

        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var product_id      = $("#product_id").val();
        var pharmacy_id     = $("#pharmacy_id").val();
        var saleman_id      = $("#saleman_id").val();
        var doctor_id       = $("#doctor_id").val();
        var zone_id         = $("#zone_id").val();
        var city_id         = $("#city_id").val();
        var sub_zone_id     = $("#sub_zone_id").val();
        var quarter_id      = $("#quarter_id").val();
        var street_id       = $("#street_id").val();
        
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Report No.</th>
                          <th class="multi_lang">Report Title</th>
                          <th class="multi_lang">Report Date</th>
                          <th class="multi_lang">Report Type</th>
                          <th class="multi_lang">Visit Type</th>
                          
                          <th class="multi_lang">Doctor</th>
                          <th class="multi_lang">Specialty</th>
                          <th class="multi_lang">In/Out Plan</th>
                          <th class="multi_lang">City</th>
                          <th class="multi_lang">Zone</th>
                          <th class="multi_lang">Saleman</th>
                          <th class="multi_lang">Doctor Rated by Saleman</th>

                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").html('');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"saleman_visit",
                        
                        d1          : d1,
                        d2          : d2,
                        product_id  : product_id,
                        pharmacy_id : pharmacy_id,
                        saleman_id  : saleman_id,
                        doctor_id   : doctor_id,
                        city_id     : city_id,
                        zone_id     : zone_id,
                        sub_zone_id : sub_zone_id,
                        quarter_id  : quarter_id,
                        street_id   : street_id

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    

                    for(var i=0 ; i <=res.data.length-1;i++){

                      if (res.data[i].report_type ==0) {
                        var rt= 'مع مواد';
                      }else{
                        var rt= 'بدون مواد';
                      }

                      if (res.data[i].visit_type ==0) {
                        var vt= 'زيارة طبيب';
                      }else{
                        var vt= 'زيارة صيدلية بدون طبيب';
                      }

                      if (res.data[i].is_out_plan ==0) {
                        var iop= 'داخل الخطة';
                      }else{
                        var iop= 'خارج الخطة';
                      }

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].report_id+`</td>
                                            <td>`+res.data[i].report_number+`</td>
                                            <td>`+res.data[i].report_title+`</td>
                                            <td>`+res.data[i].report_date+`</td>
                                            <td>`+rt+`</td>
                                            <td>`+vt+`</td>
                                            <td>`+res.data[i].doctor_name+`</td>
                                            <td>`+res.data[i].doctor_specialty+`</td>
                                            <td>`+iop+`</td>
                                            <td>`+res.data[i].city_name+`</td>
                                            <td>`+res.data[i].zone_name+`</td>
                                            <td>`+res.data[i].username+`</td>
                                            <td>`+res.data[i].rating_doctor+`</td>
                                            

                                        </tr>
                                        `;
                      total_inv++;

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Total Visits</td>
                                            <td id="total_invoice">`+(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });

    
    $('#total_saleman_visit_btn').on('click',function(){
      
    
        

        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var product_id      = $("#product_id").val();
        var pharmacy_id     = $("#pharmacy_id").val();
        var saleman_id      = $("#saleman_id").val();
        var doctor_id       = $("#doctor_id").val();
        var zone_id         = $("#zone_id").val();
        var city_id         = $("#city_id").val();
        var sub_zone_id     = $("#sub_zone_id").val();
        var quarter_id      = $("#quarter_id").val();
        var street_id       = $("#street_id").val();
        
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Saleman Name</th>
                          <th class="multi_lang">Total No. of Doctors Visits</th>
                          <th class="multi_lang">Total No. of Pharmacy Visits</th>
                          <th class="multi_lang">Totals</th>
                          <th class="multi_lang">City</th>
                          <th class="multi_lang">Zone</th>

                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").html('');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"total_saleman_visit",
                        
                        d1          : d1,
                        d2          : d2,
                        product_id  : product_id,
                        pharmacy_id : pharmacy_id,
                        saleman_id  : saleman_id,
                        doctor_id   : doctor_id,
                        city_id     : city_id,
                        zone_id     : zone_id,
                        sub_zone_id : sub_zone_id,
                        quarter_id  : quarter_id,
                        street_id   : street_id

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].saleman_id_fk+`</td>
                                            <td>`+res.data[i].username+`</td>
                                            <td>`+res.data[i].total_visit_doctor+`</td>
                                            <td>`+res.data[i].total_visit_pharmacy+`</td>
                                            <td>`+res.data[i].total_visits+`</td>
                                            
                                            <td>`+res.data[i].city_name+`</td>
                                            <td>`+res.data[i].zone_name+`</td>
                                            

                                        </tr>
                                        `;
                      total_inv++;

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Total Visits</td>
                                            <td id="total_invoice">`+(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });

    $('#saleman_cover_ratio_btn').on('click',function(){
      
    
        

        var d1              = $("#first_date").val();
        var d2              = $("#second_date").val();
        var product_id      = $("#product_id").val();
        var pharmacy_id     = $("#pharmacy_id").val();
        var saleman_id      = $("#saleman_id").val();
        var doctor_id       = $("#doctor_id").val();
        var zone_id         = $("#zone_id").val();
        var city_id         = $("#city_id").val();
        var sub_zone_id     = $("#sub_zone_id").val();
        var quarter_id      = $("#quarter_id").val();
        var street_id       = $("#street_id").val();
        
       
       
        var attach = `<thead>
                        <tr class="bg-blue-300">
                          <th>ID</th>
                          <th class="multi_lang">Saleman Name</th>
                          <th class="multi_lang">Total No. of Doctors Visits</th>
                          <th class="multi_lang">Total No. of Pharmacy Visits</th>
                          <th class="multi_lang">Totals</th>
                          <th class="multi_lang">City</th>
                          <th class="multi_lang">Zone</th>

                        </tr>
                      </thead>
                      <tbody>`;

        $("#table1").html('');
       
        $.ajax({
                url: "models/crul_report.php",
                type:"POST",
                dataType : "JSON",
                data: {"type":"total_saleman_visit",
                        
                        d1          : d1,
                        d2          : d2,
                        product_id  : product_id,
                        pharmacy_id : pharmacy_id,
                        saleman_id  : saleman_id,
                        doctor_id   : doctor_id,
                        city_id     : city_id,
                        zone_id     : zone_id,
                        sub_zone_id : sub_zone_id,
                        quarter_id  : quarter_id,
                        street_id   : street_id

                      } ,
                complete:function(){
                  hideLoader();
                },
                beforeSend:function(){
                  showLoader("Please Wait While Gathering Report Information");
                },
                success: function(res) {
                  if(res.is_success == true){

                    var total_inv = 0;
                    
                    for(var i=0 ; i <=res.data.length-1;i++){

                      attach = attach + `
                                        <tr>
                                            <td>`+res.data[i].saleman_id_fk+`</td>
                                            <td>`+res.data[i].username+`</td>
                                            <td>`+res.data[i].total_visit_doctor+`</td>
                                            <td>`+res.data[i].total_visit_pharmacy+`</td>
                                            <td>`+res.data[i].total_visits+`</td>
                                            
                                            <td>`+res.data[i].city_name+`</td>
                                            <td>`+res.data[i].zone_name+`</td>
                                            

                                        </tr>
                                        `;
                      total_inv++;

                    }

                    attach = attach + `</tbody>
                                       <tfoot>
                                        <tr class="alpha-info">
                                            <td></td>
                                            <td></td>
                                            <td class="multi_lang">Total Visits</td>
                                            <td id="total_invoice">`+(total_inv)+`</td>
                                            <td></td>
                                            <td></td>
                                            <td></td>
                                            
                                            
                                        </tr>
                                      </tfoot>
                                        `;

                    $("#table1").html(attach);
                    

                  }else{
                    swal({
                                title: Translation("خطأ"),
                                text: Translation(res.data),
                                confirmButtonColor: "#2196F3",
                                type: "error"
                            });
                  }
                }
        });

      
     


    });


   
});





$(function() {
//var singleSelect = $('.datatable-column-search-inputs').DataTable();
  var query_y = getQueryVariable("d1");
  var query_m = getQueryVariable("d2");

  if(query_y != 'not' ){
    $("#first_date").val(query_y);
  }

  if( query_m != 'not'){
    $("#second_date").val(query_m);
  }


  $(".touchspin-basic").TouchSpin({
      min: 1,
      max: 1000000,
      step: 1
  });

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

  




    // end highlight row and column


    // Single row selection
    
  


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


});



