"use strict"
$(document).ready(function () {
    $('#CategoryTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/categorydatatable',
        columns: [{
                data: 'id',
                name: 'id'
            }, {
                data: 'cat_name',
                name: 'cat_name'
            },
            {
                data: 'cat_image',
                name: 'cat_image'
            }, {
                data: 'action',
                name: 'action'
            }
        ],
        columnDefs: [{
            targets: 2,
            render: function (data) {
                var url =data;
                var pathname = new URL(url).pathname;               
                if (data != null) {
                    return '<img src="'+data+'" style="height:50px;width:50px;border-radius: 0px">';
                } else {
                    return '';
                }
            }
        }],
        order: [
            [0, "DESC"]
        ]
    });
});

function disablebtn(){
    alert($("#admin_demo_msg").val());
}

   
$(document).ready(function () {
    $('#notification_table').DataTable({
        processing: true,
        serverSide: true,
       
        ajax: $("#url_path").val()+'/notification_table',
        columns: [
            {data: 'id'    , name: 'id'},            
            {data: 'message' , name:'message'}
        ]
    });
});


function samplecollectionchange(val){
   if(val==2){
        $("#sample_collection_fee_div").css("display","block");
        $("#sample_collection_fee").attr("required",true);
   }else{
        $("#sample_collection_fee_div").css("display","none");
        $("#sample_collection_fee").attr("required",false);
   }
}

function fasttimedivhcnage(val){
   if(val==1){
        $("#fast_time_div").css("display","block");
        $("#fast_time").attr("required",true);
   }else{
        $("#fast_time_div").css("display","none");
        $("#fast_time").attr("required",false);
   }
}

$(document).ready(function () {
    $('#SubcategoryTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/subcategorydatatable',
        columns: [{
                data: 'id',
                name: 'id'
            }, {
                data: 'sub_name',
                name: 'sub_name'
            },
            {
                data: 'sub_image',
                name: 'sub_name'
            }, {
                data: 'action',
                name: 'action'
            }
        ],
        columnDefs: [{
            targets: 2,
            render: function (data) {
                            
                if (data != null) {
                    return '<img src="'+ data+'" style="height:50px;width:50px;border-radius: 0px">';
                } else {
                    return '';
                }
            }
        }],
        order: [
            [0, "DESC"]
        ]
    });
});
$(document).ready(function () {
    $('#CityTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/citydatatable',
        columns: [{
                data: 'id',
                name: 'id'
            }, {
                data: 'city_name',
                name: 'city_name'
            }, {
                data: 'action',
                name: 'action'
            }
        ],
        order: [
            [0, "DESC"]
        ]
    });
});

$(document).ready(function () {
    $('#ContactTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/contact_datatable',
        columns: [{
                data: 'id',
                name: 'id'
            },{
                data: 'name',
                name: 'name'
            },{
                data: 'email',
                name: 'email'
            },{
                data: 'phone',
                name: 'phone'
            },{
                data: 'subject',
                name: 'subject'
            },{
                data: 'message',
                name: 'message'
            }, {
                data: 'action',
                name: 'action'
            }
        ],
        order: [
            [0, "DESC"]
        ]
    });
});



$(document).ready(function () {
    $('#OrdersTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/OrdersTable',
        columns: [{
                data: 'id',
                name: 'id'
            },{
                data: 'name',
                name: 'name'
            },{
                data: 'payment_method',
                name: 'payment_method'
            },{
                data: 'paid_amount',
                name: 'paid_amount'
            },{
                data: 'datetime',
                name: 'datetime'
            },{
                data: 'more',
                name: 'more'
            },{
                data: 'print',
                name: 'print'
            },{
                data: 'status',
                name: 'status'
            },{
                data: 'action',
                name: 'action'
            }
        ],
        columnDefs: [{
            targets: 5,
            render: function (data) { 
                    return '<a href="javascript::void(0)" onclick="moreinfo('+data+')" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#normalmodal">'+$("#more_lable").val()+'</a>';          
            }
        },{
            targets: 6,
            render: function (data) {
                    var path = $("#url_path").val()+"/adminprintorders"+"/"+data;
                    return '<a href="'+path+'" target="_blank" class="btn btn-primary">'+$("#print_lable").val()+'</a>';               
            }
        }],
        order: [
            [0, "DESC"]
        ]
    });
});

function moreinfo(id){
        $.ajax({
            url: $("#url_path").val()+"/getorderdetails"+"/"+id,
            success: function( data ) {
                var str = JSON.parse(data);
                
                $("#order_no").empty();
                $("#customer_name").empty();
                $("#email").empty();
                $("#address").empty();
                $("#order_place_date").empty();
                $("#payment_method").empty();
                $("#date").empty();
                $("#time").empty();
                $("#tableinfo").empty();
                $("#subtotal").empty();
                $("#txt_charge").empty();
                $("#total").empty();
                
                $("#order_no").html(str.data.id);
                $("#customer_name").html(str.userinfo.name);
                $("#email").html(str.userinfo.email);
                $("#address").html(str.userinfo.address);
                $("#order_place_date").html(str.data.orderplace_date);
                $("#payment_method").html(str.data.payment_method);
                $("#date").html(str.data.date);
                $("#time").html(str.data.time);
                var txt = "";
                for(var i=0;i<str.data.orderdata.length;i++){
                    var mrp = str.data.orderdata[i].mrp;
                    var price = str.data.orderdata[i].price;
                    txt = txt +'<tr><td>'+str.data.orderdata[i].memberdetails.name+' | '+str.data.orderdata[i].memberdetails.relation+'</br>'+str.data.orderdata[i].memberdetails.gender+'</td><td>Name : '+str.data.orderdata[i].item_name+'</br>Parameters : '+str.data.orderdata[i].parameter+'</br>MRP : '+str.currency+mrp.toFixed(2)+'</br>Price : '+str.currency+price.toFixed(2)+'</td><td>'+str.currency+price.toFixed(2)+'</td></tr>';
                }
                var subtotal = str.data.subtotal;
                var txtchr = str.data.tax;
                var finalamount = str.data.final_total;
                $("#tableinfo").html(txt);
                $("#subtotal").html(str.currency+subtotal.toFixed(2));
                $("#txt_charge").html(str.currency+txtchr.toFixed(2));
                $("#total").html(str.currency+finalamount.toFixed(2));
            }
        });
}


$(document).ready(function () {
    $('#popularPackageTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/popularPackageTable',
        columns: [{
                data: 'id',
                name: 'id'
            }, {
                data: 'name',
                name: 'name'
            }, {
                data: 'action',
                name: 'action'
            }
        ],
        order: [
            [0, "DESC"]
        ]
    });
});



$(document).ready(function () {
    $('#ManagerTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/ManagerTable',
        columns: [{
                data: 'id',
                name: 'id'
            },{
                data: 'image',
                name: 'image'
            }, {
                data: 'name',
                name: 'name'
            }, {
                data: 'email',
                name: 'email'
            }, {
                data: 'city',
                name: 'city'
            }, {
                data: 'action',
                name: 'action'
            }
        ],
        columnDefs: [{
            targets: 1,
            render: function (data) {
                            
                if (data != null) {
                    return '<img src="'+ data+'" style="height:50px;width:50px;border-radius: 0px">';
                } else {
                    return '';
                }
            }
        }],
        order: [
            [0, "DESC"]
        ]
    });
});



$(document).ready(function () {
    $('#UserTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/UserTable',
        columns: [{
                data: 'id',
                name: 'id'
            },{
                data: 'image',
                name: 'image'
            }, {
                data: 'name',
                name: 'name'
            }, {
                data: 'email',
                name: 'email'
            }, {
                data: 'city',
                name: 'city'
            }, {
                data: 'member',
                name: 'member'
            }, {
                data: 'action',
                name: 'action'
            }
        ],
        columnDefs: [{
            targets: 1,
            render: function (data) {
                if (data != null) {
                    return '<img src="'+ data+'" style="height:50px;width:50px;border-radius: 0px">';
                } else {
                    return '';
                }
            },
         
        }],
    /*  columnDefs: [{
            targets: 5,
            render: function (data) {
                if (data != null) {
                    alert(data);
                   // return '<img src="'+ data+'" style="height:50px;width:50px;border-radius: 0px">';
                    // return "<input type='button' value='ANAMA'>";
                    // return '<a onclick="view_member(' . "'" .$user->id. "'" . ')" rel="tooltip"  class="btn btn-danger" data-original-title="Remove" style="margin-right: 10px;color:white !important">'.$memertext.'</a>'; 
                } else {
                    return '';
                }
            },
        }], */
        order: [
            [0, "DESC"]
        ]
    });
});



$(document).ready(function () {
    $('#ProfileTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/profiledatatable',
        columns: [{
                data: 'id',
                name: 'id'
            }, {
                data: 'profile_name',
                name: 'profile_name'
            }, {
                data: 'no_of_parameter',
                name: 'no_of_parameter'
            },{
                data: 'frq',
                name: 'frq'
            },{
                data: 'lab_report',
                name: 'lab_report'
            }, {
                data: 'action',
                name: 'action'
            }
        ],columnDefs: [{
            targets: 3,
            render: function (data) {
                            
                if (data != null) {
                    var path = $("#url_path").val() +'/frq'+"/"+data+"/3";
                    return '<a href="'+path+'" class="btn btn-primary">'+$("#view_frq_lable").val()+'</a>';
                } else {
                    return '';
                }
            }
        },{
            targets: 4,
            render: function (data) {
                            
                if (data != null) {
                    var path = $("#url_path").val() +'/storage/app/public/sample_report'+"/"+data;
                    return '<a href="'+path+'" class="btn btn-primary" target="_blank">'+$("#view_report").val()+'</a>';
                } else {
                    return '';
                }
            }
        }],
        order: [
            [0, "DESC"]
        ]
    });
});



$(document).ready(function () {
    $('#PackageTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/package_datatable',
        columns: [{
                data: 'id',
                name: 'id'
            }, {
                data: 'name',
                name: 'name'
            },{
                data: 'frq',
                name: 'frq'
            },{
                data: 'lab_report',
                name: 'lab_report'
            }, {
                data: 'action',
                name: 'action'
            }
        ],columnDefs: [{
            targets: 2,
            render: function (data) {
                            
                if (data != null) {
                    var path = $("#url_path").val() +'/frq'+"/"+data+"/1";
                    return '<a href="'+path+'" class="btn btn-primary">'+$("#view_frq_lable").val()+'</a>';
                } else {
                    return '';
                }
            }
        },{
            targets: 3,
            render: function (data) {
                            
                if (data != null) {
                    var path = $("#url_path").val() +'/storage/app/public/sample_report'+"/"+data;
                    return '<a href="'+path+'" class="btn btn-primary" target="_blank">'+$("#view_report").val()+'</a>';
                } else {
                    return '';
                }
            }
        }],
        order: [
            [0, "DESC"]
        ]
    });
});

function view_member(id)
{
    // alert(id);
    // url: $("#url_path").val()+"/getmembersinfo"+"/"+id,
     $.ajax({
            // url: $("#path_admin").val()+"/getorderinfo"+"/"+id,
              url: $("#url_path").val()+"/getmembersinfo"+"/"+id,
            success: function( data ) {
                console.log(data);
                // alert(data.data_order.name);
    
              var txt="";
              var j=1;
              txt=txt+'<tbody><tr><th>#</th><th>'+$("#name_for_lable").val()+'</th><th>'+$("#phone_for_lable").val()+'</th><th>'+$("#age_for_lable").val()+'</th><th>'+$("#dob_for_lable").val()+'</th><th>'+$("#relation_for_lable").val()+'</th><th>'+$("#gender_for_lable").val()+'</th></tr>';
                for(var i=0;i<data.item_list.length;i++){

                    txt=txt+'<tr><td>'+j+'</td>';
                    txt=txt+'<td>'+data.item_list[i].name+'</td>';
                    txt=txt+'<td>'+data.item_list[i].mobile_no+'</td>';
                    txt=txt+'<td>'+data.item_list[i].age+'</td>';
                    txt=txt+'<td>'+data.item_list[i].dob+'</td>';
                    txt=txt+'<td>'+data.item_list[i].relation+'</td>';
                    txt=txt+'<td>'+data.item_list[i].gender+'</td></tr>';  
                    j++;
                }
            
                txt=txt+'</tbody>';
                 document.getElementById("itemdata").innerHTML=txt;
            }
        });

}

function view_address(id)
{   
    $.ajax({
            url: $("#url_path").val()+"/getaddress"+"/"+id,
            success: function( data ) {    
              var txt="";
              var j=1;
              txt=txt+'<tbody><tr><th>#</th><th>'+$("#add_for_lable").val()+'</th>';
                for(var i=0;i<data.item_list.length;i++)
                {
                     // alert(data.item_list[i].city);
                    txt=txt+'<tr><td>'+j+'</td>';
                    txt=txt+'<td>'+$("#house_no_lable").val()+' - '+data.item_list[i].house_no+'<br>'+$("#landmark_lable").val()+' - '+data.item_list[i].address+'<br>'+$("#pincode_lable").val()+' - '+data.item_list[i].pincode+'<br>'+data.item_list[i].city+','+data.item_list[i].state+'</td></tr>';  
                    j++;
                }
                txt=txt+'</tbody>';
                document.getElementById("addressdata").innerHTML=txt;
            }
        });
}


$(document).ready(function () {
    $('#FRQTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/frq_datatable/'+$("#package_id").val()+"/"+$("#type").val(),
        columns: [{
                data: 'id',
                name: 'id'
            }, {
                data: 'question',
                name: 'question'
            }
            , {
                data: 'ans',
                name: 'ans'
            }, {
                data: 'action',
                name: 'action'
            }
        ],
        order: [
            [0, "DESC"]
        ]
    });
});

function edit_frq(id){
  $.ajax({
        url: $("#url_path").val()+"/getfrq"+"/"+id,
        method:"get",
        success: function( data ) {
            console.log(data);
            var str = JSON.parse(data);
            $("#edit_id").val(str.id);
            $("#edit_question").val(str.question);
            $("#edit_answer").val(str.ans);
        }
    });
}

$(document).ready(function () {
    $('#ParameterTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: $("#url_path").val() + '/parameter_datatable',
        columns: [{
                data: 'id',
                name: 'id'
            }, {
                data: 'name',
                name: 'name'
            }
            , {
                data: 'short_desc',
                name: 'short_desc'
            }, {
                data: 'price',
                name: 'price'
            },{
                data: 'frq',
                name: 'frq'
            },{
                data: 'lab_report',
                name: 'lab_report'
            }, {
                data: 'action',
                name: 'action'
            }
        ],columnDefs: [{
            targets: 4,
            render: function (data) {
                            
                if (data != null) {
                    var path = $("#url_path").val() +'/frq'+"/"+data+"/2";
                    return '<a href="'+path+'" class="btn btn-primary">'+$("#view_frq_lable").val()+'</a>';
                } else {
                    return '';
                }
            }
        },{
            targets: 5,
            render: function (data) {
                            
                if (data != null) {
                    var path = $("#url_path").val() +'/storage/app/public/sample_report'+"/"+data;
                    return '<a href="'+path+'" class="btn btn-primary" target="_blank">'+$("#view_report").val()+'</a>';
                } else {
                    return '';
                }
            }
        }],
        order: [
            [0, "DESC"]
        ]
    });
});



function checkcurrentpwd(val){
    $.ajax({
        url: $("#url_path").val()+"/checkadminpassword"+"/"+val,
        method:"get",
        success: function( data ) {
            console.log(data);
            if(data==1){
                var msg = $("#cpwd").val();
                alert(msg);
                $("#oldPassword").val("");
            }
        }
    });
}

function changesubcategory(val){
   $.ajax({
        url: $("#url_path").val()+"/getsubcategorybycategory"+"/"+val,
        method:"get",
        success: function( data ) {            
            $("#subcategory_id").html(data);
        }
    });
}

function selecttesttype(type,id){
    $.ajax({
        url: $("#url_path").val()+"/gettestids"+"/"+type,
        method:"get",
        success: function( data ) {            
            $("#type_id_"+id).html(data);
        }
    });
}

function changepopulartype(val){
    if(val==""){
        alert("Please Select Vaild Type");
        $("#type_id").html("");
    }else{
         $.ajax({
            url: $("#url_path").val()+"/searchpopulartype"+"/"+val,
            method:"get",
            success: function( data ) {            
                $("#type_id").html(data);
            }
        });
    }
   
}


function checkbothpassword(val){
    console.log(val);
    var msg = $("#newpwd").val();    
    var newpassword = $("#newPassword").val();
    if(newpassword!=val){
        alert(msg);
        $("#newPassword").val("");
        $("#confirmPassword").val("");
    }
}
function delete_record(url){
    var msg = $("#record").val();
    if (confirm(msg)) {   
        if($("#is_demo_flag").val()=='1'){
            alert($("#admin_demo_msg").val());
        }else{
            window.location.href = url; 
        }                   
    } else {
        window.location.reload();
    }
}

function delete_user_detail(url){
    var msg = $("#record").val();
    // alert(url);
    if (confirm(msg)) {   
         if($("#is_demo_flag").val()=='1'){
            alert($("#admin_demo_msg").val());
         }else{
            window.location.href = url; 
         }                   
    } else {
        window.location.reload();
    }
}

$(document).ready(function () {
 $('#upload_image').on('change', function (e) {
    readURL(this, "basic_img");
  });
});


function readURL(input, field) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
      $("#basic_img1").val(e.target.result);
      $('#' + field).attr('src', e.target.result);
    }
    reader.readAsDataURL(input.files[0]);
  }
}

function checkconfirmpassword(val){
    if(val!=$("#password").val()){
        alert($("#pass_match_msg").val());
        // alert("Password and Confirm Password Must Be Same");
        $("#cpassword").val("");
    }
}

  // Add row
  var row=1;
  $(document).on("click", "#add-row", function () {
  var new_row = '<tr id="row'+row+'"><td><select class="form-control" name="testdetail['+row+'][type]" required onchange="selecttesttype(this.value,'+row+')"><option value="">'+$("#select_type_labal").val()+'</option><option value="1">'+$("#parameter_labal").val()+'</option><option value="2">'+$("#profile_labal").val()+'</option></select></td><td><select class="form-control" name="testdetail['+row+'][type_id]" id="type_id_'+row+'"  required ><option value="">'+$("#select_test_labal").val()+'</option></select></td><td><input class="delete-row btn btn-primary" type="button" value="'+$("#delete_labal").val()+'" /></td></tr>';
    
  $('#test-body').append(new_row);
  row++;
  return false;
  });
  
  // Remove criterion
  $(document).on("click", ".delete-row", function () {
  //  alert("deleting row#"+row);
    if(row>0) {
      $(this).closest('tr').remove();
      row--;
    }
  return false;
  });


