$(document).ready(function(){
    disable_enable_background(false)
});
var acc_cnt = 1;
// function import_customers()
// {
//     var check = 1;
//     if($("#customer_import").val() == '')
//     {
//         $("#customer_import").css({'border-color':'red'});
//         $("#customer_import").addClass('shake');
//         $('#customer_import').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
//         {
//             $('#customer_import').delay(200).removeClass('shake');
//         });
//         check = 0;
//     }
//     else
//     {
//         $("#customer_import").css({'border-color':'#CCC'});
//     }

//     if(check == 0)
//     {
//         var msg = 'Oh snap ! You forgot to upload excel';
//         display_alert('err',msg);

//         $("body, html").animate({'scrollTop':0},1000);
//     }
//     else
//     {
//         var form_id = document.getElementById('customer_import_form');
//         var form_data = new FormData(form_id);
//         var path = base_url+"utilityctrl/import_dhiangems_customers/";

//         fileUpAjaxCall('POST',path,form_data,'JSON',resp =>
//         {
//             console.log(resp);
//             var data = "";
//             if(typeof(resp['error']) != "undefined" && resp['error'] !== null)
//             {
//                 alert(resp['error']);
//             }
//             else
//             {
//                 data += '<tr>';
//                 data += '<th>ID</th>';
//                 data += '<th>Name</th>';
//                 data += '<th>Group</th>';
//                 data += '<th>Mobile Number</th>';
//                 data += '<th>Email ID</th>';
//                 data += '<th>City</th>';
//                 data += '<th>Address</th>';
//                 data += '<th>Phone Number</th>';
//                 data += '<th>GST</th>';
//                 data += '<th>PAN</th>';
//                 data += '<th>REF NAME</th>';
//                 data += '<th>REF1</th>';
//                 data += '<th>REF2</th>';
//                 data += '</tr>';

//                 var arr_count = Object.keys(resp).length;
//                 var i = 1;
//                 $('#total_cnt').val(arr_count);
//                 $.each(resp['imp_customers'],function(index,value)
//                 {
//                     if(value['Q'] != "")
//                     {
//                     // console.log(value);
//                         if (value['flag'] == "red")
//                         {
//                           data += '<tr id="row_id_'+acc_cnt+'" style="color:red">';
//                           data += '<input type="hidden" name="acc_id[]" id="acc_id_'+acc_cnt+'" class="form-control" value="0" readonly/>'; 
//                         }
//                         else
//                         {
//                           data += '<tr id="row_id_'+acc_cnt+'" style="background:#fff">';
//                           data += '<input type="hidden" name="acc_id[]" id="acc_id_'+acc_cnt+'" class="form-control" value="1" readonly/>'; 
//                         }
//                         data += '<td>'+i+'</td>';
//                         data += '<td>';
//                             data += '<input type="text" name="cust_name[]" id="cust_name_'+acc_cnt+'" value="'+value['name']+'" readonly>';
//                         data += '</td>';
//                         data += '<td>';
//                             data += '<input type="text" value="'+value['group']+'" readonly>';
//                             data += '<input type="hidden" name="group_id[]" id="group_id_'+acc_cnt+'" value="'+value['group_id']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="mobile[]" id="mobile_'+acc_cnt+'" value="'+value['mobile']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="email[]" id="email_'+acc_cnt+'" value="'+value['email']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="cityId[]" id="cityId_'+acc_cnt+'" value="'+value['cityId']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="address[]" id="address_'+acc_cnt+'" value="'+value['address']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="tel1[]" id="tel1_'+acc_cnt+'" value="'+value['tel1']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="gst[]" id="gst_'+acc_cnt+'" value="'+value['gst']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="pan[]" id="pan_'+acc_cnt+'" value="'+value['pan']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="ref_name[]" id="ref_name_'+acc_cnt+'" value="'+value['ref_name']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="ref1[]" id="ref1_'+acc_cnt+'" value="'+value['ref1']+'" readonly>';
//                         data += '</td>';

//                         data += '<td>';
//                             data += '<input type="text" name="ref2[]" id="ref2_'+acc_cnt+'" value="'+value['ref2']+'" readonly>';
//                         data += '</td>';

//                         data += '</tr>';

//                         i++;
//                     }
//                 });
//                 $('#customer_wrapper').html(data);
//             }
//              $("#loading").hide();
//         },
//             errmsg => {});
//     }
// }

function import_customers()
{
    var check = 1;
    if($("#customer_import").val() == '')
    {
        $("#customer_import").css({'border-color':'red'});
        $("#customer_import").addClass('shake');
        $('#customer_import').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e)
        {
            $('#customer_import').delay(200).removeClass('shake');
        });
        check = 0;
    }
    else
    {
        $("#customer_import").css({'border-color':'#CCC'});
    }

    if(check == 0)
    {
        var msg = 'Oh snap ! You forgot to upload excel';
        display_alert('err',msg);

        $("body, html").animate({'scrollTop':0},1000);
    }
    else
    {
        var form_id = document.getElementById('customer_import_form');
        var form_data = new FormData(form_id);
        var path = base_url+"utilityctrl/import_dhiangems_customers/";

        fileUpAjaxCall('POST',path,form_data,'JSON',resp =>
        {
            console.log(resp);
            var data = "";
            if(typeof(resp['error']) != "undefined" && resp['error'] !== null)
            {
                alert(resp['error']);
            }
            else
            {
                data += '<tr>';
                data += '<th>ID</th>';
                data += '<th>Name</th>';
                data += '<th>Owner</th>';
                data += '<th>Group</th>';
                data += '<th>Mobile Number</th>';
                data += '<th>Email ID</th>';
                data += '<th>City</th>';
                data += '<th>Address</th>';
                data += '<th>Phone Number</th>';
                data += '<th>GST</th>';
                data += '<th>PAN</th>';
                data += '<th>REF NAME</th>';
                data += '<th>REF1</th>';
                data += '<th>REF2</th>';
                data += '</tr>';

                var arr_count = Object.keys(resp).length;
                var i = 1;
                $('#total_cnt').val(arr_count);
                $.each(resp['imp_customers'],function(index,value)
                {
                    if(value['B'] != "")
                    {
                    // console.log(value);
                        if (value['flag'] == "red")
                        {
                          data += '<tr id="row_id_'+acc_cnt+'" style="color:red">';
                          data += '<input type="hidden" name="acc_id[]" id="acc_id_'+acc_cnt+'" class="form-control" value="0" readonly/>'; 
                        }
                        else
                        {
                          data += '<tr id="row_id_'+acc_cnt+'" style="background:#fff">';
                          data += '<input type="hidden" name="acc_id[]" id="acc_id_'+acc_cnt+'" class="form-control" value="1" readonly/>'; 
                        }
                        data += '<td>'+i+'</td>';
                        data += '<td>';
                            data += '<input type="text" name="supp_name[]" id="supp_name_'+acc_cnt+'" value="'+value['supp_name']+'" readonly>';
                        data += '</td>';
                        data += '<td>';
                            data += '<input type="text" name="cust_name[]" id="cust_name_'+acc_cnt+'" value="'+value['name']+'" readonly>';
                        data += '</td>';
                        data += '<td>';
                            data += '<input type="text" value="'+value['group']+'" readonly>';
                            data += '<input type="hidden" name="group_id[]" id="group_id_'+acc_cnt+'" value="'+value['group_id']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="mobile[]" id="mobile_'+acc_cnt+'" value="'+value['mobile']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="email[]" id="email_'+acc_cnt+'" value="'+value['email']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="cityId[]" id="cityId_'+acc_cnt+'" value="'+value['cityId']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="address[]" id="address_'+acc_cnt+'" value="'+value['address']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="tel1[]" id="tel1_'+acc_cnt+'" value="'+value['tel1']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="gst[]" id="gst_'+acc_cnt+'" value="'+value['gst']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="pan[]" id="pan_'+acc_cnt+'" value="'+value['pan']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="ref_name[]" id="ref_name_'+acc_cnt+'" value="'+value['ref_name']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="ref1[]" id="ref1_'+acc_cnt+'" value="'+value['ref1']+'" readonly>';
                        data += '</td>';

                        data += '<td>';
                            data += '<input type="text" name="ref2[]" id="ref2_'+acc_cnt+'" value="'+value['ref2']+'" readonly>';
                        data += '</td>';

                        data += '</tr>';

                        i++;
                    }
                });
                $('#customer_wrapper').html(data);
            }
             $("#loading").hide();
        },
            errmsg => {});
    }
}
function insert_imported_customers()
{
  var check = 1;

    if(check == 0)
    {
      var msg = 'Oh snap ! You forgot to upload Excel file';
      display_alert('err',msg);
      $("body, html").animate({'scrollTop':0},1000);
    }
    else
  {
    var path = base_url+"utilityctrl/insert_customer_records/";
    var formid = document.getElementById('insert_cust_import');
    var formdata = new FormData(formid);

    $(".jobpost_btn").prop('disabled',true);

    $("#loading").show();
    $.ajax({
      type:'POST',
      url:path,
      data : formdata,
      contentType : false,
      processData : false,
      success:function(resp)
      {
          console.log(resp);
          if(resp == 1)
          {
              var msg = 'Successfully Uploaded!';
              display_alert('succ',msg);
              $("#insert_cust_import")[0].reset();
              $("body, html").animate({'scrollTop':0},1000);
              setTimeout(function(){window.location.reload(); },2000);
          }
          else if(resp == 2)
          {
            var msg = 'Already exist';
            display_alert('err',msg);
            $("body, html").animate({'scrollTop':0},1000);
          }
          else
          {
            var msg = 'Database Error ! Please try again letter';
            display_alert('warn',msg);
            $("body, html").animate({'scrollTop':0},1000);
          }

          $("#loading").hide();
      },
      error:function(resp){
        console.log(resp);
      }   
    });
  }
}