var table ,  block = $(".content-wrapper").parent();
    var HTML_VIEW_PAY = '';
    var print_page =  `
                        <script>
                          window.print();
                          window.onload = function() { window.print(); }
                        </script>
                        `;
    var table_invoice     =`
                          <html>

                            <head>
                                <meta http-equiv=Content-Type content="text/html; charset=utf-8">
                                <meta name=Generator content="Microsoft Word 15 (filtered)">
                                <style>
                                    body {
                                          -webkit-print-color-adjust: exact;
                                        }
                                    <!--
                                    /* Font Definitions */
                                    
                                    @font-face {
                                        font-family: "Cambria Math";
                                        panose-1: 2 4 5 3 5 4 6 3 2 4;
                                    }
                                    
                                    @font-face {
                                        font-family: Calibri;
                                        panose-1: 2 15 5 2 2 2 4 3 2 4;
                                    }
                                    /* Style Definitions */
                                    
                                    p.MsoNormal,
                                    li.MsoNormal,
                                    div.MsoNormal {
                                        margin-top: 0in;
                                        margin-right: 0in;
                                        margin-bottom: 8.0pt;
                                        margin-left: 0in;
                                        line-height: 107%;
                                        font-size: 11.0pt;
                                        font-family: "Calibri", sans-serif;
                                    }
                                    
                                    .MsoChpDefault {
                                        font-family: "Calibri", sans-serif;
                                    }
                                    
                                    .MsoPapDefault {
                                        margin-bottom: 8.0pt;
                                        line-height: 107%;
                                    }
                                    
                                    @page WordSection1 {
                                        size: 11.0in 8.5in;
                                        margin: .5in .5in .5in .5in;
                                    }
                                    
                                    div.WordSection1 {
                                        page: WordSection1;
                                    }
                                    
                                    -->
                                </style>

                            </head>

                            <body lang=EN-US>

                                <div class=WordSection1>

                                    <div align=center>
                          `;

    




    function edit_items(itemid){
          
          $("#edit_objid").val(itemid);

         
          $.ajax({
                url : 'models/crul_work_details.php',
                type : 'POST',
                data : {type:'get_edit_item', id : itemid},
                dataType : 'json',
                success : function(response) {
                  
                  
                  $("#edit_year_id_fk").val(response[0].year_id_fk).selectpicker('refresh');
                  $("#edit_month_id_fk").val(response[0].month_id_fk).selectpicker('refresh');
                  $("#edit_day_id_fk").val(response[0].day_id_fk).selectpicker('refresh');
                  $("#edit_hours").val(response[0].hours);
                  $("#edit_time_in").val(response[0].time_in);
                  $("#edit_time_out").val(response[0].time_out);
                  $("#edit_is_subtracted").val(response[0].is_subtracted);
                  $("#edit_work_note").val(response[0].work_note);
                  $("#edit_hour_transaction").val(response[0].hour_transaction);
                  $("#edit_employe_id_fk").val(response[0].employe_id_fk);

                  switch (response[0].hour_transaction) {

                      case 'Absent':
                        
                        $("#edit_model_titles").text('تعديل ساعات الغياب بدون عذر');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('عدد ساعات الغياب');
                        $("#edit_hours").attr('placeholder','عدد ساعات الغياب');
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").removeAttr('style');
                        $("#edit_to_div").removeAttr('style');
                        $("#edit_time_in").attr('required','required');
                        $("#edit_time_out").attr('required','required');

                        $("#edit_sub_div").removeAttr('style');
                        $("#edit_is_subtracted").attr('required','required');

                        $("#edit_day_div").removeAttr('style');
                        $("#edit_day_id_fk").attr('required','required');

                        break;

                      case 'Absent With Permission':
                        
                        $("#edit_model_titles").text('تعديل ساعات الغياب بعذر');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('عدد ساعات الغياب');
                        $("#edit_hours").attr('placeholder','عدد ساعات الغياب');
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").removeAttr('style');
                        $("#edit_to_div").removeAttr('style');
                        $("#edit_time_in").attr('required','required');
                        $("#edit_time_out").attr('required','required');

                        $("#edit_sub_div").removeAttr('style');
                        $("#edit_is_subtracted").attr('required','required');
                        
                        $("#edit_day_div").removeAttr('style');
                        $("#edit_day_id_fk").attr('required','required');

                        break;

                      case 'Sick With Permission':
                        
                        $("#edit_model_titles").text('تعديل ساعات الغياب بسبب المرض');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('عدد ساعات الغياب');
                        $("#edit_hours").attr('placeholder','عدد ساعات الغياب');
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").removeAttr('style');
                        $("#edit_to_div").removeAttr('style');
                        $("#edit_time_in").attr('required','required');
                        $("#edit_time_out").attr('required','required');

                        $("#edit_sub_div").removeAttr('style');
                        $("#edit_is_subtracted").attr('required','required');
                        
                        $("#edit_day_div").removeAttr('style');
                        $("#edit_day_id_fk").attr('required','required');

                        break;

                      case 'Attend':
                        
                        $("#edit_model_titles").text('تعديل تسجيل حضور الموظف');
                        $("#edit_hour_div").attr('style','display:none;');
                        $("#edit_hour_lbl").text('عدد ساعات الغياب');
                        $("#edit_hours").attr('placeholder','عدد ساعات الغياب');
                        $("#edit_hours").removeAttr('required');

                        $("#edit_from_div").removeAttr('style');
                        $("#edit_to_div").removeAttr('style');
                        // $("#time_in").attr('required','required');
                        // $("#time_out").attr('required','required');
                        $("#edit_time_in").removeAttr('required');
                        $("#edit_time_out").removeAttr('required');

                        $("#edit_sub_div").attr('style','display:none;');
                        $("#edit_is_subtracted").removeAttr('required');
                        
                        $("#edit_day_div").removeAttr('style');
                        $("#edit_day_id_fk").attr('required','required');

                        break;

                      case 'Extra Hours':
                        
                        $("#edit_model_titles").text('تعديل ساعات العمل الاضافية');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('عدد الساعات الاضافية');
                        $("#edit_hours").attr('placeholder','عدد الساعات الاضافية');
                        
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").removeAttr('style');
                        $("#edit_to_div").removeAttr('style');
                        $("#edit_time_in").attr('required','required');
                        $("#edit_time_out").attr('required','required');

                        $("#edit_sub_div").attr('style','display:none;');
                        $("#edit_is_subtracted").removeAttr('required');
                        
                        $("#edit_day_div").removeAttr('style');
                        $("#edit_day_id_fk").attr('required','required');

                        break;


                      case 'Reward':
                        
                        $("#edit_model_titles").text('تعديل المكافءة');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('مبلغ المكافئة');
                        $("#edit_hours").attr('placeholder','مبلغ المكافئة');
                        
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").attr('style','display:none;');
                        $("#edit_to_div").attr('style','display:none;');
                        $("#edit_time_in").removeAttr('required');
                        $("#edit_time_out").removeAttr('required');

                        $("#edit_sub_div").attr('style','display:none;');
                        $("#edit_is_subtracted").removeAttr('required');
                        
                        $("#edit_day_div").removeAttr('style');
                        $("#edit_day_id_fk").attr('required','required');

                        break;

                      case 'Penalty':
                        
                        $("#edit_model_titles").text('تعديل العقوبة');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('مبلغ العقوبة');
                        $("#edit_hours").attr('placeholder','مبلغ العقوبة');
                        
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").attr('style','display:none;');
                        $("#edit_to_div").attr('style','display:none;');
                        $("#edit_time_in").removeAttr('required');
                        $("#edit_time_out").removeAttr('required');

                        $("#edit_sub_div").attr('style','display:none;');
                        $("#edit_is_subtracted").removeAttr('required');
                        
                        $("#edit_day_div").removeAttr('style');
                        $("#edit_day_id_fk").attr('required','required');

                        break;


                      case 'Salary':
                        
                        $("#edit_model_titles").text('تحديد الراتب الشهري للموظف');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('الراتب الشهري المحدد');
                        $("#edit_hours").attr('placeholder','الراتب الشهري المحدد');
                        
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").attr('style','display:none;');
                        $("#edit_to_div").attr('style','display:none;');
                        $("#edit_time_in").removeAttr('required');
                        $("#edit_time_out").removeAttr('required');

                        $("#edit_sub_div").attr('style','display:none;');
                        $("#edit_is_subtracted").removeAttr('required');
                        

                        $("#edit_day_div").attr('style','display:none;');
                        $("#edit_day_id_fk").removeAttr('required');

                        break;

                      case 'Loan':
                        
                        $("#edit_model_titles").text('تعديل السلفة الشهرية للموظف');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('مبلغ السلفة');
                        $("#edit_hours").attr('placeholder','مبلغ السلفة');
                        
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").attr('style','display:none;');
                        $("#edit_to_div").attr('style','display:none;');
                        $("#edit_time_in").removeAttr('required');
                        $("#edit_time_out").removeAttr('required');

                        $("#edit_sub_div").attr('style','display:none;');
                        $("#edit_is_subtracted").removeAttr('required');
                        

                        // $("#edit_day_div").attr('style','display:none;');
                        $("#edit_day_id_fk").removeAttr('required');

                        break;

                      case 'Refund':
                        
                        $("#edit_model_titles").text('تعديل تسديد السلفة الشهرية من راتب الموظف');
                        $("#edit_hour_div").removeAttr('style');
                        $("#edit_hour_lbl").text('مبلغ التسديد');
                        $("#edit_hours").attr('placeholder','مبلغ التسديد');
                        
                        $("#edit_hours").attr('required','required');

                        $("#edit_from_div").attr('style','display:none;');
                        $("#edit_to_div").attr('style','display:none;');
                        $("#edit_time_in").removeAttr('required');
                        $("#edit_time_out").removeAttr('required');

                        $("#edit_sub_div").attr('style','display:none;');
                        $("#edit_is_subtracted").removeAttr('required');
                        

                        // $("#edit_day_div").attr('style','display:none;');
                        $("#edit_day_id_fk").removeAttr('required');

                        break;

                        

                    }
                  


                      

                     
                },
                complete: function() {
                    
                      
                      $('#edit_form_cont').modal('toggle');
                     
                }

          }); // submit ajax



    }

    function delete_items(itemid) {
       
          swal({
              title: Translation("هل انت متأكد؟!"),
              text: Translation("سوف لن تستطيع استرجاع المعلومات المرتبطة بهذا السجل في حالة المسح"),
              type: "warning",
              showCancelButton: true,
              confirmButtonColor: "#EF5350",
              confirmButtonText: Translation("موافق: امسح!"),
              cancelButtonText: Translation("الغاء الامر"),
              closeOnConfirm: false,
              closeOnCancel: true
          },
          function(isConfirm){
              if (isConfirm) {
                $.ajax({
                  url: "models/crul_work_details.php",
                  type:"POST",
                  data: {"type":"delete","obj_id":itemid} ,
                  dataType : "JSON",
                  complete:function(){
                    //hideLoader();
                  },
                  beforeSend:function(){
                    //showLoader("Deleting Purchase");
              },
                  success: function(res) {
                    if(res.is_success == true){
                      
                      swal({
                                  title: Translation("تم الحذف"),
                                  text: Translation(res.data),
                                  confirmButtonColor: "#66BB6A",
                                  type: "success"
                              });
                      $(".datatable-column-search-inputs").DataTable().ajax.reload(null, false);
                    }else{
                      swal({
                                  title: Translation("خطأ"),
                                  text: Translation(res.data),
                                  confirmButtonColor: "#2196F3",
                                  type: "error"
                              });
                    }
                  }
              });
              }
          }); // /confirm
    }

    function show_salary_board(years = null, months = null){

          var headers           = '';
          var attach            = "";
          var attach_other      = "";
          var footers           = "";
          var customer_details  = "";
          var print_attach      = "";
          var total_pay         = "";
          var show_page         = 1;
          var titels            = '';

          var employe_id        ='';

          if(query_id != 'not'){
            employe_id = query_id;
          }
          
          var year_name         = Translation('All Years');
          var month_name        = Translation('All Months');

          if(years != null && years !=''){
            year_name         = +years + +2015 -1;

          }

          
          switch (months){

            case '1':
              month_name = 'كانون الثاني';
              break;

            case '2':
              month_name = 'شباط';
              break;

            case '3':
              month_name = 'اذار';
              break;
            case '4':
              month_name = 'نيسان';
              break;
            case '5':
              month_name = 'ايار';
              break;
            case '6':
              month_name = 'حزيران';
              break;
            case '7':
              month_name = 'تموز';
              break;
            case '8':
              month_name = 'اب';
              break;
            case '9':
              month_name = 'ايلول';
              break;
            case '10':
              month_name = 'تشرين الاول';
              break;
            case '11':
              month_name = 'تشرين الثاني';
              break;
            case '12':
              month_name = 'كانون الاول';
              break;


          }

          $.ajax({
                url : 'models/crul_work_details.php',
                type : 'POST',
                data : {type:'show_salary_board', 
                        employe_id  : employe_id, 
                        year_id     : years, 
                        month_id    :months},
                dataType : 'json',
                success : function(response) {
                    console.log(response)
                  if(response.invoice.length > 0){

                      
                      titels = Translation(' قائمة كشف بالرواتب المحددة والمستحقة ');
                      
                       headers =`

                                  <p style="text-align:center; font-size:20px;">`+titels+` </p>
                                  <p style="text-align:center; font-size:20px;">`+year_name+ " - " + month_name+` </p>

                                  
                                    
                                    `;
                        
                        
                       customer_details = `

                                            `;


                     attach =  `

                                <table class=MsoTableGrid dir=rtl border=1 cellspacing=0 cellpadding=0 width="98%" style='width:98.0%;border-collapse:collapse;border:none'>
                                  <tr style='page-break-inside:avoid;height:80.5pt'>
                                      <td width=25 style='width:19.1pt;border:solid windowtext 1.0pt;padding:0in 1.45pt 0in 1.45pt;  height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          #
                                          </span></p>
                                      </td>
                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('اسم الموظف')+`
                                          </span></p>
                                      </td>
                                     
                                     <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('السنة')+`
                                          </span></p>
                                      </td>
                                     <td width=34 style='width:25.6pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الشهر')+`
                                          </span></p>
                                      </td>

                                      <td width=34 style='width:25.6pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الحضور')+`
                                          </span></p>
                                      </td>

                                      <td width=34 style='width:25.6pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('غياب بدون عذر')+`
                                          </span></p>
                                      </td>

                                      <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('غياب بعذر')+`
                                          </span></p>
                                      </td>

                                      <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('مريض بعذر')+`
                                          </span></p>
                                      </td>

                                      <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الساعات المستقطعه من الراتب')+`
                                          </span></p>
                                      </td>

                                      <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الساعات الاضافية')+`
                                          </span></p>
                                      </td>
                                      <td width=34 style='width:25.6pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الساعات الكلية')+`
                                          </span></p>
                                      </td>
                                      <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('المكافئات')+`
                                          </span></p>
                                      </td>
                                      <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('العقوبات')+`
                                          </span></p>
                                      </td>

                                      <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('السلف')+`
                                          </span></p>
                                      </td>

                                      <td width=25 style='width:19.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('تسديد السلف')+`
                                          </span></p>
                                      </td>
                                      
                                      <td width=44 style='width:33.3pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الراتب المحدد')+`
                                          </span></p>
                                      </td>
                                      <td width=51 style='width:38.05pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-top:0in;margin-right:  5.65pt;margin-bottom:0in;margin-left:5.65pt;margin-bottom:.0001pt;text-align:  center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الراتب المستحق')+`
                                          </span></p>
                                      </td>
                                  </tr>

                                  `;
                     
                      // show sell item details   
                      var sum_total_invoice   = 0;
                      var sum_total_payment   = 0;
                      var sum_total_discount  = 0;
                      var sum_total_withdraw  = 0;
                      var sum_total_return    = 0;

                      for(var i=0 ; i < response.invoice.length;i++){

                        // compute totals
                       
                        var total_hours = response.invoice[i].total_hours
                        
                        total_hours     = total_hours.toFixed(2);

                       


                        attach = attach + `

                                            <tr>
                                              <td width=25  style='width:19.1pt;border:solid windowtext 1.0pt;  border-top:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+(i+1)+`
                                                  </span></p>
                                              </td>
                                              <td width=75  style='width:56.35pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                 `+response.invoice[i].employe_full_name+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].year_no+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].month_name+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].attend_hours+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].absent_hours+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].perm_hours+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].sick_hours+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].absent_subtracted+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].extra_hours+`
                                                  </span></p>
                                              </td>
                                              <td width=34  style='width:25.6pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+total_hours+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+currency_format_display(response.invoice[i].rewards)+`
                                                  </span></p>
                                              </td>
                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+currency_format_display(response.invoice[i].penalty)+`
                                                  </span></p>
                                              </td>

                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+currency_format_display(response.invoice[i].loan)+`
                                                  </span></p>
                                              </td>

                                              <td width=25  style='width:19.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+currency_format_display(response.invoice[i].refund)+`
                                                  </span></p>
                                              </td>
                                              
                                              <td width=44  style='width:33.3pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+currency_format_display(response.invoice[i].salary)+`
                                                  </span></p>
                                              </td>
                                              <td width=51  style='width:38.05pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+currency_format_display(response.invoice[i].real_salary)+`
                                                  </span></p>
                                              </td>
                                            </tr>
                                          
                                          
                                           `;
                        
             
                       
                      } // for i


                     

                      
                      
                       footers =`

                                  
                              </table>

                          </div>

                          

                      </div>

                  </body>

                  
                     `+print_attach+`
                    </html>`;

                  }else{
                    show_page = 0;
                    
                  }    

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

                      HTML_VIEW_PAY = table_invoice + headers + customer_details + attach + attach_other + footers;
                       //  me.data('requestRunning', false);
                     if(show_page==1){
                       
                        window.open().document.write(HTML_VIEW_PAY);

                     }else{
                      noty({
                            width: 200,
                            text: Translation('لا توجد قائمة رواتب لهذه السنة والشهر'),
                            type: 'error',
                            dismissQueue: true,
                            timeout: 2000,
                            layout: 'topRight',
                      });
                     }
                       
                      
                     
                        // window.open().document.write(HTML_VIEW);
                }

          }); // submit ajax


    }


    function show_details(cust_id, typo){

      $("#query_form")[0].reset();
      $("#employe_id_").val('');
      $("#employe_id_").val(cust_id);
      $("#operation").val(typo);

      // set year and month automatically

      var currentTime = new Date();
      // returns the month (from 0 to 11)
      var month = currentTime.getMonth() + 1

      // returns the day of the month (from 1 to 31)
      var day = currentTime.getDate()

      // returns the year (four digits)
      var year = currentTime.getFullYear()

      var year_id = year - 2015 +1;
      
      $("#year_id_").val(year_id);
      $("#month_id_").val(month);


      $('#query_modal').modal('toggle');

    }





   
    function add_hours(cust_id, typo){

      $("#add_form")[0].reset();
      $("#employe_id_fk").val('');
      $("#employe_id_fk").val(cust_id);
      $("#hour_transaction").val(typo);

      // set year and month automatically

      var currentTime = new Date();
      // returns the month (from 0 to 11)
      var month = currentTime.getMonth() + 1

      // returns the day of the month (from 1 to 31)
      var day = currentTime.getDate()

      // returns the year (four digits)
      var year = currentTime.getFullYear()

      var year_id = year - 2015 +1;
      
      $("#year_id_fk").val(year_id);
      $("#month_id_fk").val(month);

      

      switch (typo) {

        case 'absent':
          
          $("#model_titles").text('اضافة ساعات غياب بدون عذر');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('عدد ساعات الغياب');
          $("#hours").attr('placeholder','عدد ساعات الغياب');
          $("#hours").attr('required','required');

          $("#from_div").removeAttr('style');
          $("#to_div").removeAttr('style');
          $("#time_in").attr('required','required');
          $("#time_out").attr('required','required');

          $("#sub_div").removeAttr('style');
          $("#is_subtracted").attr('required','required');

          $("#day_div").removeAttr('style');
          $("#day_id_fk").attr('required','required');

          break;

        case 'absent_with_per':
          
          $("#model_titles").text('اضافة ساعات غياب بعذر');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('عدد ساعات الغياب');
          $("#hours").attr('placeholder','عدد ساعات الغياب');
          $("#hours").attr('required','required');

          $("#from_div").removeAttr('style');
          $("#to_div").removeAttr('style');
          $("#time_in").attr('required','required');
          $("#time_out").attr('required','required');

          $("#sub_div").removeAttr('style');
          $("#is_subtracted").attr('required','required');
          
          $("#day_div").removeAttr('style');
          $("#day_id_fk").attr('required','required');

          break;

        case 'sick':
          
          $("#model_titles").text('اضافة ساعات غياب بسبب المرض');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('عدد ساعات الغياب');
          $("#hours").attr('placeholder','عدد ساعات الغياب');
          $("#hours").attr('required','required');

          $("#from_div").removeAttr('style');
          $("#to_div").removeAttr('style');
          $("#time_in").attr('required','required');
          $("#time_out").attr('required','required');

          $("#sub_div").removeAttr('style');
          $("#is_subtracted").attr('required','required');
          
          $("#day_div").removeAttr('style');
          $("#day_id_fk").attr('required','required');

          break;

        case 'attend':
          
          $("#model_titles").text('تسجيل حضور الموظف لهذا اليوم');
          $("#hour_div").attr('style','display:none;');
          $("#hour_lbl").text('عدد ساعات الغياب');
          $("#hours").attr('placeholder','عدد ساعات الغياب');
          $("#hours").removeAttr('required');

          $("#from_div").removeAttr('style');
          $("#to_div").removeAttr('style');
          // $("#time_in").attr('required','required');
          // $("#time_out").attr('required','required');
          $("#time_in").removeAttr('required');
          $("#time_out").removeAttr('required');

          $("#sub_div").attr('style','display:none;');
          $("#is_subtracted").removeAttr('required');
          
          $("#day_div").removeAttr('style');
          $("#day_id_fk").attr('required','required');

          break;

        case 'extra':
          
          $("#model_titles").text('اضافة ساعات العمل الاضافية');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('عدد الساعات الاضافية');
          $("#hours").attr('placeholder','عدد الساعات الاضافية');
          
          $("#hours").attr('required','required');

          $("#from_div").removeAttr('style');
          $("#to_div").removeAttr('style');
          $("#time_in").attr('required','required');
          $("#time_out").attr('required','required');

          $("#sub_div").attr('style','display:none;');
          $("#is_subtracted").removeAttr('required');
          
          $("#day_div").removeAttr('style');
          $("#day_id_fk").attr('required','required');

          break;


        case 'reward':
          
          $("#model_titles").text('اضافة مكافءة');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('مبلغ المكافئة');
          $("#hours").attr('placeholder','مبلغ المكافئة');
          
          $("#hours").attr('required','required');

          $("#from_div").attr('style','display:none;');
          $("#to_div").attr('style','display:none;');
          $("#time_in").removeAttr('required');
          $("#time_out").removeAttr('required');

          $("#sub_div").attr('style','display:none;');
          $("#is_subtracted").removeAttr('required');
          
          $("#day_div").removeAttr('style');
          $("#day_id_fk").attr('required','required');

          break;

        case 'penalty':
          
          $("#model_titles").text('اضافة عقوبة');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('مبلغ العقوبة');
          $("#hours").attr('placeholder','مبلغ العقوبة');
          
          $("#hours").attr('required','required');

          $("#from_div").attr('style','display:none;');
          $("#to_div").attr('style','display:none;');
          $("#time_in").removeAttr('required');
          $("#time_out").removeAttr('required');

          $("#sub_div").attr('style','display:none;');
          $("#is_subtracted").removeAttr('required');
          
          $("#day_div").removeAttr('style');
          $("#day_id_fk").attr('required','required');

          break;


        case 'salary':
          
          $("#model_titles").text('تحديد الراتب الشهري للموظف');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('الراتب الشهري المحدد');
          $("#hours").attr('placeholder','الراتب الشهري المحدد');
          
          $("#hours").attr('required','required');

          $("#from_div").attr('style','display:none;');
          $("#to_div").attr('style','display:none;');
          $("#time_in").removeAttr('required');
          $("#time_out").removeAttr('required');

          $("#sub_div").attr('style','display:none;');
          $("#is_subtracted").removeAttr('required');
          

          $("#day_div").attr('style','display:none;');
          $("#day_id_fk").removeAttr('required');

          break;


        case 'loan':
          
          $("#model_titles").text('اعطاء سلفة شهرية للموظف');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('مبلغ السلفة');
          $("#hours").attr('placeholder','مبلغ السلفة');
          
          $("#hours").attr('required','required');

          $("#from_div").attr('style','display:none;');
          $("#to_div").attr('style','display:none;');
          $("#time_in").removeAttr('required');
          $("#time_out").removeAttr('required');

          $("#sub_div").attr('style','display:none;');
          $("#is_subtracted").removeAttr('required');
          
          $("#day_div").removeAttr('style');
          $("#day_id_fk").removeAttr('required');
         

          break;

        case 'refund':
          
          $("#model_titles").text('تسديد سلفة من راتب الموظف');
          $("#hour_div").removeAttr('style');
          $("#hour_lbl").text('مبلغ التسديد');
          $("#hours").attr('placeholder','مبلغ التسديد');
          
          $("#hours").attr('required','required');

          $("#from_div").attr('style','display:none;');
          $("#to_div").attr('style','display:none;');
          $("#time_in").removeAttr('required');
          $("#time_out").removeAttr('required');

          $("#sub_div").attr('style','display:none;');
          $("#is_subtracted").removeAttr('required');
          
          $("#day_div").removeAttr('style');
          $("#day_id_fk").removeAttr('required');

          break;

          

      }

      $('#add_hours_modal').modal('toggle');

    }

    $('#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('تحميل')+'  <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
    }

    


    function get_Employee(){

            $.ajax({
                  url : 'models/crul_employe.php',
                  type : 'POST',
                  data : {type:'get'},
                  dataType : 'json',
                  success : function(response) {
                    //console.log(response)
                      var attach ='<option value="" selected="selected">'+Translation('اختيار الموظف')+'</option>';
                      var aaa='';
                        for(var i=response.length-1 ; i >= 0;i--){
                          
                          attach = attach + '<option data-subtext="" value="'+response[i][0]+'">'+response[i][1]+'</option>';
                         
                        }
                        $("#employe_id_fk").html(attach);
                        
                        $('#employe_id_fk').selectpicker('refresh');
                       
                  },
                  complete: function() {
                         //  me.data('requestRunning', false);
                  }

            }); // submit ajax

    }

  

   


  

$(document).ready(function() {
  
    
    get_Employee();
    
    
    $('#add_form').submit(function(event) {
      event.preventDefault();
      
    
      var employe_id_fk             = $("#employe_id_fk").val();
      var hour_transaction          = $("#hour_transaction").val();
      var year_id_fk                = $("#year_id_fk").val();
      var month_id_fk               = $("#month_id_fk").val();
      var day_id_fk                 = $("#day_id_fk").val();
      var hours                     = $("#hours").val();
      var time_in                   = $("#time_in").val();
      var time_out                  = $("#time_out").val();
      var is_subtracted             = $("#is_subtracted").val();
      var work_note                 = $("#work_note").val();

     
      var Check1 = true;
      var Check2 = true;
      var Check3 = true;
      var Check4 = true;
      var Check5 = true;
      var Check6 = true;
      var Check7 = true;
      var Check8 = true;

      Check1 = Checks('employe_id_fk');
      Check2 = Checks('year_id_fk');
      Check3 = Checks('month_id_fk');

      switch (hour_transaction) {

              case 'absent':
              case 'absent_with_per':
              case 'sick':
                  Check4 = Checks('day_id_fk');
                  Check5 = Checks('hours');
                  Check6 = Checks('time_in');
                  Check7 = Checks('time_out');
                  Check8 = Checks('is_subtracted');
                 
                  break;

              case 'attend':

                  Check4 = Checks('day_id_fk');
                 
                 
                  break;

              case 'reward':

                  Check4 = Checks('day_id_fk');
                  Check5 = Checks('hours');

                  break;

              case 'penalty':

                  Check4 = Checks('day_id_fk');
                  Check5 = Checks('hours');

                  break;

              case 'salary':
              case 'loan':
              case 'refund':
                 
                  Check5 = Checks('hours');

                  break;


      }

    
      

      if(Check1 && Check2 && Check3 && Check4 && Check5 && Check6 && Check7 && Check8){
            $("#submit_add_btn").attr('disabled',true);
            $.ajax({
                url: "models/crul_employe_work.php",
                type:"POST",
                dataType: "JSON",
                data : {employe_id_fk     :employe_id_fk,
                        year_id_fk        :year_id_fk,
                        month_id_fk       :month_id_fk,
                        day_id_fk         :day_id_fk,
                        hours             :hours,
                        time_in           :time_in,
                        time_out          :time_out,
                        is_subtracted     :is_subtracted,
                        work_note         :work_note,
                        trans             :hour_transaction,
                        type              :'create'}
            })
            .done(function(res) {
                //alert(res);
                if(res.is_success == true){

                    $("#add_form")[0].reset();
                    $("#employe_id_fk").val('');
                    $("#is_subtracted").val('');
                    
      
                    $('#add_hours_modal').modal('toggle');

                    $(".datatable-column-search-inputs").DataTable().ajax.reload(null, false);
                    
                    noty({
                                    width: 200,
                                    text: Translation(res.data),
                                    type: 'success',
                                    dismissQueue: true,
                                    timeout: 3000,
                                    layout: 'topRight',
                              });
                }else{
                    swal({
                        title:"Warning",
                        text: Translation(res.data),
                        confirmButtonColor: "#66BB6A",
                        type: "info"
                    });
                }
                $("#submit_add_btn").attr('disabled',false);
            })
            .fail(function(err) {
                 swal({
                    title: Translation("خطأ"),
                    text: (err),
                    confirmButtonColor: "#66BB6A",
                    type: "error"
                });
                $("#submit_add_btn").attr('disabled',false);
            })
            .always(function() {
                console.log("complete");
            });
        }
    });

    
    $('#edit_form').submit(function(event) {
      event.preventDefault();
      
    
      var id                        = $("#edit_objid").val();
      var hour_transaction          = $("#edit_hour_transaction").val();
      var year_id_fk                = $("#edit_year_id_fk").val();
      var month_id_fk               = $("#edit_month_id_fk").val();
      var day_id_fk                 = $("#edit_day_id_fk").val();
      var hours                     = $("#edit_hours").val();
      var time_in                   = $("#edit_time_in").val();
      var time_out                  = $("#edit_time_out").val();
      var is_subtracted             = $("#edit_is_subtracted").val();
      var work_note                 = $("#edit_work_note").val();
      var employe_id_fk             = $("#edit_employe_id_fk").val();

     
      var Check1 = true;
      var Check2 = true;
      var Check3 = true;
      var Check4 = true;
      var Check5 = true;
      var Check6 = true;
      var Check7 = true;
      var Check8 = true;

      // Check1 = Checks('employe_id_fk');
      Check2 = Checks('edit_year_id_fk');
      Check3 = Checks('edit_month_id_fk');

      switch (hour_transaction) {

              case 'Absent':
              case 'Absent With Permission':
              case 'Sick With Permission':
                  Check4 = Checks('edit_day_id_fk');
                  Check5 = Checks('edit_hours');
                  Check6 = Checks('edit_time_in');
                  Check7 = Checks('edit_time_out');
                  Check8 = Checks('edit_is_subtracted');
                 
                  break;

              case 'Attend':

                  Check4 = Checks('edit_day_id_fk');
                 
                 
                  break;

              case 'Reward':

                  Check4 = Checks('edit_day_id_fk');
                  Check5 = Checks('edit_hours');

                  break;

              case 'Penalty':

                  Check4 = Checks('edit_day_id_fk');
                  Check5 = Checks('edit_hours');

                  break;

              case 'Extra Hours':

                  Check4 = Checks('edit_day_id_fk');
                  Check5 = Checks('edit_hours');

                  break;

              case 'Salary':
              case 'Loan':
              case 'Refund':
                 
                  Check5 = Checks('edit_hours');

                  break;




      }

    
      

      if(Check1 && Check2 && Check3 && Check4 && Check5 && Check6 && Check7 && Check8){
            $("#submit_edit_btn").attr('disabled',true);
            $.ajax({
                url: "models/crul_work_details.php",
                type:"POST",
                dataType: "JSON",
                data : {id                :id,
                        year_id_fk        :year_id_fk,
                        month_id_fk       :month_id_fk,
                        day_id_fk         :day_id_fk,
                        hours             :hours,
                        time_in           :time_in,
                        time_out          :time_out,
                        is_subtracted     :is_subtracted,
                        work_note         :work_note,
                        trans             :hour_transaction,
                        employe_id_fk     :employe_id_fk,
                        type              :'update'}
            })
            .done(function(res) {
                //alert(res);
                if(res.is_success == true){

                    $("#edit_form")[0].reset();
                    
                    
      
                    $('#edit_form_cont').modal('toggle');

                    $(".datatable-column-search-inputs").DataTable().ajax.reload(null, false);
                    
                    noty({
                                    width: 200,
                                    text: Translation(res.data),
                                    type: 'success',
                                    dismissQueue: true,
                                    timeout: 3000,
                                    layout: 'topRight',
                              });
                }else{
                    swal({
                        title:"Warning",
                        text: Translation(res.data),
                        confirmButtonColor: "#66BB6A",
                        type: "info"
                    });
                }
                $("#submit_edit_btn").attr('disabled',false);
            })
            .fail(function(err) {
                 swal({
                    title: Translation("خطأ"),
                    text: (err),
                    confirmButtonColor: "#66BB6A",
                    type: "error"
                });
                $("#submit_edit_btn").attr('disabled',false);
            })
            .always(function() {
                console.log("complete");
            });
        }
    });


    
    $('#query_form').submit(function(event) {
      event.preventDefault();
      
    
      var employe_id_fk             = $("#employe_id_").val();
      var operation                 = $("#operation").val();
      var year_id_fk                = $("#year_id_").val();
      var month_id_fk               = $("#month_id_").val();
      var day_id_fk                 = $("#day_id_").val();
      
     
      var Check1 = true;
      var Check2 = true;
      var Check3 = true;
      var Check4 = true;
      var Check5 = true;
      var Check6 = true;
      var Check7 = true;
      var Check8 = true;

      Check1 = Checks('employe_id_');


      var headers           = '';
      var attach            = "";
      var attach_other      = "";
      var footers           = "";
      var customer_details  = "";
      var print_attach      = "";
      var total_pay         = "";
      var show_page         = 1;
      var titels            = '';

      
      

      if(Check1 && Check2 && Check3 && Check4 && Check5 && Check6 && Check7 && Check8){
            $("#submit_query_btn").attr('disabled',true);
            $.ajax({
                url: "models/crul_employe_work.php",
                type:"POST",
                dataType: "JSON",
                data : {employe_id_fk     :employe_id_fk,
                        year_id_fk        :year_id_fk,
                        month_id_fk       :month_id_fk,
                        day_id_fk         :day_id_fk,
                        
                        trans             :operation,
                        type              :'employe_details'}
            })
            .done(function(response) {
               

               if(response.invoice.length > 0){

                      
                      titels = Translation('تفاصيل عن اوقات عمل الموظف');
                      
                       headers =`

                                  <p style="text-align:center; font-size:20px;">`+titels+` </p>

                                  
                                    
                                    `;
                        
                        
                       customer_details = `
                                            <p style="text-align:center; font-size:20px;">
                                              <span > `+response.invoice[0].employe_full_name+`</span>
                                              
                                            </p>
                                            `;


                     attach =  `

                                <table class=MsoTableGrid dir=rtl border=1 cellspacing=0 cellpadding=0 width="98%" style='width:98.0%;border-collapse:collapse;border:none'>
                                  <tr style='page-break-inside:avoid;height:80.5pt'>
                                      <td width=25 style='width:19.1pt;border:solid windowtext 1.0pt;padding:0in 1.45pt 0in 1.45pt;  height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          #
                                          </span></p>
                                      </td>
                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('السنة')+`
                                          </span></p>
                                      </td>

                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الشهر')+`
                                          </span></p>
                                      </td>

                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('اليوم')+`
                                          </span></p>
                                      </td>

                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الوقت : من')+`
                                          </span></p>
                                      </td>

                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الوقت : الى')+`
                                          </span></p>
                                      </td>

                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('عدد الساعات / المبلغ')+`
                                          </span></p>
                                      </td>

                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('نوع العملية')+`
                                          </span></p>
                                      </td>

                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('الملاحظات')+`
                                          </span></p>
                                      </td>

                                      <td width=75 style='width:56.35pt;border:solid windowtext 1.0pt;border-right:  none;padding:0in 1.45pt 0in 1.45pt;height:80.5pt'>
                                          <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span dir=LTR>
                                          `+Translation('تاريخ الانشاء')+`
                                          </span></p>
                                      </td>
                                     
                                      
                                  </tr>

                                  `;
                     
                      // show sell item details   
                      var sum_total_salary    = 0;
                      var sum_total_absent    = 0;
                      var sum_total_per       = 0;
                      var sum_total_sick      = 0;
                      var sum_total_attend    = 0;
                      var sum_total_sub       = 0;
                      var sum_total_reward    = 0;
                      var sum_total_penalty   = 0;
                      var sum_total_extra     = 0;

                      for(var i=0 ; i < response.invoice.length;i++){

                        var year_name         = +response.invoice[i].year_id_fk + +2015 -1;
                        var month_name        = '';
                        
                        switch (response.invoice[i].month_id_fk){

                              case '1':
                                month_name = 'كانون الثاني';
                                break;

                              case '2':
                                month_name = 'شباط';
                                break;

                              case '3':
                                month_name = 'اذار';
                                break;
                              case '4':
                                month_name = 'نيسان';
                                break;
                              case '5':
                                month_name = 'ايار';
                                break;
                              case '6':
                                month_name = 'حزيران';
                                break;
                              case '7':
                                month_name = 'تموز';
                                break;
                              case '8':
                                month_name = 'اب';
                                break;
                              case '9':
                                month_name = 'ايلول';
                                break;
                              case '10':
                                month_name = 'تشرين الاول';
                                break;
                              case '11':
                                month_name = 'تشرين الثاني';
                                break;
                              case '12':
                                month_name = 'كانون الاول';
                                break;


                        }

                        // compute totals
                       
                        var hour_transaction  = response.invoice[i].hour_transaction;
                        var hours             = response.invoice[i].hours;

                        var hours_field       = '';
                        var trans_field       = '';
                        var classs            = ''
                        switch (hour_transaction){

                          case 'Attend':
                                hours_field = `<label  >`+ hours +`</label>`;
                                trans_field = `<label  >`+ Translation('Attended') +`</label>`;
                                classs      = 'style="background-color:Lime;"';
                            break;

                          case 'Absent':
                                hours_field = `<label  >`+ hours +`</label>`;
                                trans_field = `<label  >`+ Translation('Absent') +`</label>`;
                                classs      = 'style="background-color:OrangeRed;"';
                                
                            break;

                          case 'Absent With Permission':
                                hours_field = `<label  >`+ hours +`</label>`;
                                trans_field = `<label  >`+ Translation('Absent With Permission') +`</label>`;
                                classs      = 'style="background-color:Pink;"';
                                
                            break;

                          case 'Sick With Permission':
                                hours_field = `<label  >`+ hours +`</label>`;
                                trans_field = `<label  >`+ Translation('Sick With Permission') +`</label>`;
                                classs      = 'style="background-color:Cyan;"';
                                
                            break;

                          case 'Extra Hours':
                                hours_field = `<label  >`+ hours +`</label>`;
                                trans_field = `<label  >`+ Translation('Extra Hours') +`</label>`;
                                classs      = 'style="background-color:LightSkyBlue;"';
                                
                            break;

                          case 'Reward':
                                hours_field = `<label  >`+ currency_format_display(hours) +`</label>`;
                                trans_field = `<label  >`+ Translation('Reward') +`</label>`;
                                classs      = 'style="background-color:Wheat;"';
                                
                            break;

                          case 'Penalty':
                                hours_field = `<label  >`+ currency_format_display(hours) +`</label>`;
                                trans_field = `<label  >`+ Translation('Penalty') +`</label>`;
                                classs      = 'style="background-color:Tomato;"';
                                
                            break;

                          case 'Salary':
                                hours_field = `<label  >`+ currency_format_display(hours) +`</label>`;
                                trans_field = `<label  >`+ Translation('Determined Salary') +`</label>`;
                                classs      = 'style="background-color:Yellow;"';
                                
                            break;


                        }
                        
                        // total_hours     = total_hours.toFixed(2);

                        

                        attach = attach + `

                                            <tr>
                                              <td width=25  style='width:19.1pt;border:solid windowtext 1.0pt;  border-top:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].work_id+`
                                                  </span></p>
                                              </td>
                                              <td width=75  style='width:56.35pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                 `+year_name+`
                                                  </span></p>
                                              </td>
                                              <td width=20 style='width:15.25pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+month_name+`
                                                  </span></p>
                                              </td>
                                              <td width=20 style='width:15.25pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].day_id_fk+`
                                                  </span></p>
                                              </td>
                                              <td width=20 style='width:15.25pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].time_in+`
                                                  </span></p>
                                              </td>
                                              <td width=20 style='width:15.25pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].time_out+`
                                                  </span></p>
                                              </td>
                                              <td width=20 style='width:15.25pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+hours_field+`
                                                  </span></p>
                                              </td>
                                              <td width=20 `+classs+` style='width:15.25pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+trans_field+`
                                                  </span></p>
                                              </td>
                                              <td width=20 style='width:15.25pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].work_note+`
                                                  </span></p>
                                              </td>
                                              <td width=20 style='width:15.25pt;border-top:none;border-left:solid windowtext 1.0pt;  border-bottom:solid windowtext 1.0pt;border-right:none;padding:0in 1.45pt 0in 1.45pt'>
                                                  <p class=MsoNormal align=center dir=RTL style='margin-bottom:0in;margin-bottom:  .0001pt;text-align:center;line-height:normal;direction:rtl;unicode-bidi:embed'><span lang=AR-SA style='font-family:"Arial",sans-serif'>
                                                  `+response.invoice[i].created_at+`
                                                  </span></p>
                                              </td>
                                              
                                            </tr> `;
                        
             
                       
                      } // for i



                      

                      // var total_amount  = +sum_total_invoice + +sum_total_withdraw;
                      // var total_paid    = sum_total_payment - sum_total_return;
                      // var total_remain  = total_amount - total_paid - sum_total_discount;

                      
                       footers =`

                                  
                              </table>

                          </div>

                          

                      </div>

                  </body>
                  
                    </html>`;

                  }else{
                    show_page = 0;
                    
                  }    

                 

                    HTML_VIEW_PAY = table_invoice + headers + customer_details + attach + attach_other + footers;
                       //  me.data('requestRunning', false);
                    if(show_page==1){
                       
                        window.open().document.write(HTML_VIEW_PAY);

                    }else{
                      noty({
                            width: 200,
                            text: Translation('لا توجد قائمة رواتب لهذه السنة والشهر'),
                            type: 'error',
                            dismissQueue: true,
                            timeout: 2000,
                            layout: 'topRight',
                      });
                    }
                       
                      
             
                    
                
                $("#submit_query_btn").attr('disabled',false);
            })
            .fail(function(err) {
                 swal({
                    title: Translation("خطأ"),
                    text: (err),
                    confirmButtonColor: "#66BB6A",
                    type: "error"
                });
                $("#submit_query_btn").attr('disabled',false);
            })
            .always(function() {
                console.log("complete");
            });
        }
    });
    

    

    $('#filter_btn').on('click',function(){
      $("#data_table_div").removeAttr('style');
      
      if($("#year_filter").val()!='' && $("#month_filter").val()!='' ){
        var yy = $("#year_filter").val();
        var mm = $("#month_filter").val();

        var _href ="index.php?p=work_details&y="+yy+"&m="+mm;

        if($("#day_filter").val()!=''){
          var dd = $("#day_filter").val();
          _href = _href + "&d="+dd;
        }

        if(query_id !='not'){
          var id = query_id;
          _href = _href + "&id="+id;
        }

       
        window.location = _href;
      }else{
            swal({
                    title:"Error",
                    text: Translation('الرجاء قم بتحديد السنة والشهر'),
                    confirmButtonColor: "#66BB6A",
                    type: "warning"
                });
      }


    });



    $('#salary_btn').on('click',function(){
      
        
        var yy = $("#year_filter").val();
        var mm = $("#month_filter").val();
        show_salary_board(yy, mm);

      // if($("#year_filter").val()!='' && $("#month_filter").val()!='' ){
      //   var yy = $("#year_filter").val();
      //   var mm = $("#month_filter").val();

      //   show_salary_board(yy, mm);
        
      // }else{
      //       swal({
      //               title:"Error",
      //               text: Translation('الرجاء قم بتحديد السنة والشهر'),
      //               confirmButtonColor: "#66BB6A",
      //               type: "warning"
      //           });
      // }


    });
    
    
    

});




  var query_y    = getQueryVariable("y");
  var query_m    = getQueryVariable("m");
  var query_d    = getQueryVariable("d");
  var query_id   = getQueryVariable("id");


$(function() {

  $("#year_filter").selectpicker('refresh');
  $("#month_filter").selectpicker('refresh');
  $("#day_filter").selectpicker('refresh');


  $(".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'
        }
    });

  $('.pickatime').pickatime();

  // 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+'" />');
    });
    
    

   
   // alert(query_y + "&&"+ query_m)
   if(query_y == 'not' || query_m == 'not'){
      

      // var currentTime = new Date();
      // // returns the month (from 0 to 11)
      // var month = currentTime.getMonth() + 1

      // // returns the day of the month (from 1 to 31)
      // var day = currentTime.getDate()

      // // returns the year (four digits)
      // var year = currentTime.getFullYear()

      // var year_id = year - 2015 +1;
      
      // $("#year_filter").val(year_id).selectpicker('refresh');
      // $("#month_filter").val(month).selectpicker('refresh');

   }else{
    $("#year_filter").val(query_y).selectpicker('refresh');
    $("#month_filter").val(query_m).selectpicker('refresh');
    if(query_d != 'not'){
      $("#day_filter").val(query_d).selectpicker('refresh');
    }
   }




   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_work_details.php",
        "data": function(d){
          d.type = "load";
          if(query_y !='not'){
            d.y =query_y ;
          }
          if(query_m !='not'){
            d.m =query_m ;
          }

          if(query_d !='not'){
            d.d =query_d ;
          }

          if(query_id !='not'){
            d.id =query_id ;
          }
          
        }
        },
        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": 11,
              "data": null,
              "render": function (data,type,row) {

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

                                        <li class='text-primary-600'>
                                            <a  class='text-info-400' onclick="edit_items(`+row[0]+`)" data-popup='tooltip' data-original-title='`+Translation('تعديل')+`'>
                                                <i class='icon-pencil7'></i>
                                            </a>
                                        </li>

                                        

                                        <li class='text-primary-600'>
                                            <a  class='text-danger-400' onclick="delete_items(`+row[0]+`)" data-popup='tooltip' data-original-title='`+Translation('حذف')+`'>
                                                <i class='icon-trash'></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 href="index.php?p=work_details&id=`+row[14]+`"   data-popup='tooltip' data-original-title='`+Translation('عرض جميع تفاصيل العمليات لهذا الموظف')+`'>`+data+`</a>`;


               },
               "targets": 1
            },

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

                    switch (data){

                          case 'Attend':
                               return `<span class="label btn-block bg-success-400"  >`+ Translation('Attended') +`</span >`;  
                            break;

                          case 'Absent':
                               return `<span class="label btn-block bg-danger-400"  >`+ Translation('Absent') +`</span >`;  
                                
                                
                            break;

                          case 'Absent With Permission':
                               return `<span class="label btn-block bg-pink-400"  >`+ Translation('Absent With Permission') +`</span >`;  
                                
                                
                            break;

                          case 'Sick With Permission':
                               return `<span class="label btn-block bg-info-400"  >`+ Translation('Sick With Permission') +`</span >`;  
                                
                                
                            break;

                          case 'Extra Hours':
                               return `<span class="label btn-block bg-primary-400"  >`+ Translation('Extra Hours') +`</span >`;  
                               
                            break;

                          case 'Reward':
                               return `<span class="label btn-block bg-orange-400"  >`+ Translation('Reward') +`</span >`;  
                               
                                
                            break;

                          case 'Penalty':
                               return `<span class="label btn-block bg-warning-400"  >`+ Translation('Penalty') +`</span >`;  
                                
                            break;

                          case 'Salary':
                               return `<span class="label btn-block bg-teal-400"  >`+ Translation('Determined Salary') +`</span >`;  
                                
                            break;

                          case 'Loan':
                               return `<span class="label btn-block bg-purple-400"  >`+ Translation('Loan') +`</span >`;  
                                
                            break;

                          case 'Refund':
                               return `<span class="label btn-block bg-grey-400"  >`+ Translation('Refund') +`</span >`;  
                                
                            break;


                    }                                               
                                            

                    
                },
                "targets": 8
            },
            
            

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

                   

                    switch (row[8]){

                          

                          case 'Reward':
                          case 'Penalty':
                          case 'Salary':
                          case 'Loan':
                          case 'Refund':
                               return currency_format_display(data);  
                                
                            break;

                          default: 

                               return (data);  



                    }
                },
                "targets": 7
            },

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

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

            

            

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

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

            // {
            //     "render": function(data, type, row){
            //       var remin = (row[3] - row[5] - row[4]);
            //        if(remin == 0 ){
            //          status=  '<span class="label label-success btn-block ">'+Translation('واصل')+'</span>';
            //        }else if(remin >0 && row[5] >0){
            //          status = '<span class="label bg-orange-400  btn-block ">'+Translation('واصل جزئيا')+'  </span>';
            //        }else if(remin >0 && row[5] == 0){
            //          status = '<span class="label bg-danger-400  btn-block ">'+Translation('غير واصل')+'  </span>';
            //        }else if(remin <0){
            //          status = '<span class="label bg-violet  btn-block ">'+Translation('المبلغ المدفوع اكبر من الكلي')+'  </span>';
            //        }else{
            //          status = '<span class="label bg-violet  btn-block ">'+Translation('غير معروف')+'  </span>';
            //        }
            //        return  status;
            //     },
            //     "targets": 8
            // }
            

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

            $("#attended_hours").text((aData[15]))
            $("#absent_hours").text((aData[16]))
            $("#per_hours").text((aData[17]))
            $("#sick_hours").text((aData[18]))
            $("#sub_hours").text((aData[19]))
            $("#extra_hours").text((aData[20]))
            $("#reward_hours").text(currency_format_display(aData[21]))
            $("#penalty_hours").text(currency_format_display(aData[22]))
            $("#salary_hours").text(currency_format_display(aData[23]))
            $("#act_salary_hours").text(currency_format_display(aData[24]))
            $("#loan_id").text(currency_format_display(aData[25]))
            $("#refund_id").text(currency_format_display(aData[26]))
            var total_h = (+aData[15] + (aData[20] * 1.3333) - aData[19] );

            // $("#total_hours").text(total_h.toFixed(2))

        },
        "order": [[0,'desc']],
        "displayLength": 25,
      
        initComplete: function () {
         
         // $("div.datatable-header").append(`<a data-toggle="collapse"
         //                              style="margin-right:10px;"
         //                              data-target="#add-form-cont"
         //                              href="javascript:;"
         //                              aria-controls="add-form-cont"
         //                              aria-expanded="false"
         //                              class="btn btn-primary"
         //                              id="add_Item_Btn">
         //                                <i class="`+CURRENT_BTN_ICON+`"></i> `+CURRENT_BTN+`
         //                            </a>`); 


          $("div.datatable-header").append(`&nbsp;<div class="btn-group">
                          <button id="inv_filter_btn" type="button"
                          class="btn bg-info-400 btn-labeled dropdown-toggle"
                          data-toggle="dropdown"><b><i class="icon-add">
                          </i></b> <span id="inv_filter">`+Translation('اضافة عملية')+`</span> <span class="caret"></span></button>
                          <ul class="dropdown-menu dropdown-menu-right">
                          <li>
                                <a  onclick="add_hours('','absent')"  data-original-title='Show Invoice Payments'>
                                  <i class='icon-shield-notice text-danger-400'></i> `+Translation('اضافة ساعات غياب بدون عذر')+`
                                </a>
                              </li>
                              <li>
                                <a  onclick="add_hours('','absent_with_per')"  data-original-title='Add New Payments to this Invoice'>
                                  <i class='icon-shield-check text-primary-600'></i>`+Translation('اضافة ساعات غياب بعذر')+` 
                                </a>
                              </li>
                              <li>
                                <a  onclick="add_hours('','sick')"  data-original-title='Edit or Remove Invoice Payments'>
                                  <i class='icon-aid-kit text-orange-600'></i>`+Translation('اضافة ساعات غياب مرض')+` 
                                </a>
                              </li>


                              <li>
                                <a  onclick="add_hours('','attend')"  data-original-title='Edit or Remove Invoice Payments'>
                                  <i class='icon-user-check text-teal-600'></i>`+Translation('تسجيل حضور الموظف للدوام')+` 
                                </a>
                              </li>

                              
                              <li>
                                <a  onclick="add_hours('','extra')"  data-original-title='Edit or Remove Invoice Payments'>
                                  <i class='icon-hour-glass2 text-success'></i>`+Translation('اضافة ساعات عمل اضافية')+` 
                                </a>
                              </li>

                              <li>
                                <a  onclick="add_hours('','reward')"  data-original-title='Edit or Remove Invoice Payments'>
                                  <i class='icon-stats-growth2 text-success'></i>`+Translation('اضافة مكافئة')+` 
                                </a>
                              </li>

                              <li>
                                <a  onclick="add_hours('','penalty')"  data-original-title='Edit or Remove Invoice Payments'>
                                  <i class='icon-stats-decline2 text-success'></i>`+Translation('اضافة عقوبة')+` 
                                </a>
                              </li>

                              <li>
                                <a  onclick="add_hours('','salary')"  data-original-title='Edit or Remove Invoice Payments'>
                                  <i class='icon-cash3 text-success'></i>`+Translation('تحديد الراتب الشهري')+` 
                                </a>
                              </li>

                              <li>
                                <a  onclick="add_hours('','loan')"  data-original-title='Edit or Remove Invoice Payments'>
                                  <i class='icon-wallet text-purple'></i>`+Translation('اعطاء سلفة')+` 
                                </a>
                              </li>

                              <li>
                                <a  onclick="add_hours('','refund')"  data-original-title='Edit or Remove Invoice Payments'>
                                  <i class='icon-cash text-pink'></i>`+Translation('تسديد سلفة')+` 
                                </a>
                              </li>
                        </ul>
                      </div>`);

         // $("div.datatable-header").append(`&nbsp;<div class="btn-group">
         //                  <button id="inv_filter_btn" type="button"
         //                  class="btn bg-info-400 btn-labeled dropdown-toggle"
         //                  data-toggle="dropdown"><b><i class="icon-filter4">
         //                  </i></b> <span id="inv_filter">تحديد اليوم</span> <span class="caret"></span></button>
         //                  <ul class="dropdown-menu dropdown-menu-right">
         //                  <li><a href="index.php?p=employe_work&y=`+query_y+`&m=`+query_m+`"><i class="icon-close2 text-teal"></i> ازالة الفلتر </a></li>
         //                  <li class="divider"></li>
         //                  <li><a href="index.php?p=employe_work&d=1">يوم 1</a></li>
         //                  <li><a href="index.php?p=employe_work&d=2">يوم 2 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=3"> يوم 3</a></li>
         //                  <li><a href="index.php?p=employe_work&d=4"> يوم 4 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=5"> يوم 5 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=6"> يوم 6 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=7"> يوم 7 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=8"> يوم 8 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=9"> يوم 9 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=10"> يوم 10 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=11"> يوم 11 </a></li>
         //                  <li><a href="index.php?p=employe_work&d=12"> يوم 12 </a></li>

         //                </ul>
         //              </div>`);
 
            // invoce_filter_btn_label();

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


});



