$(document).ready(function () {
    debugger;
    LoadCustomer()
    LoadCuscity()
});
$('#plCustomer_tab').click(function () {
    debugger;
    LoadCustomer();
    $('.asidefilterclass').addClass('hidden');
    $('#asideCoustomer_Master').removeClass('hidden');
    $('.groupclass').addClass('hidden');
    $('#asideCoustomer_Mastergroupclass').removeClass('hidden');
    $('.Filterclass').addClass('hidden');
    $('#asideCoustomer_MasterFilterclass').removeClass('hidden');

});
$('#CM_filter').click(function () {
    LoadCustomer();
})
$('#CM_clear').click(function () {
    $('#CMFContactName').val('');
    $('#CMFMobileNo').val('');
    $('#CMFEmailId').val('');
    $('#CMFLincenceNo').val('');
    $('#CMFFaxNo').val('');
    $('#CMFGSTNo').val('');
    $('#CMFPANNo').val('');
    LoadCustomer();
})
$('#customersubmit').on("click", function () {
    debugger;

    if ($.trim($('#cusprintname').val()) == "") {
        $('#Pcusprintname').show();
        $('#Pcusprintname').fadeOut(8000);
        $('#Pcusprintname').html('Enter Printing Name');
        $('#cusprintname').focus();
        return false;
    }

    if ($.trim($('#cuscatogaryname').val()) == "") {
        $('#Pcuscatogaryname').show();
        $('#Pcuscatogaryname').fadeOut(8000);
        $('#Pcuscatogaryname').html('Select Catogary Name');
        $('#cuscatogaryname').focus();
        return false;
    }

    if ($.trim($('#cuscontactname').val()) == "") {
        $('#Pcuscontactname').show();
        $('#Pcuscontactname').fadeOut(8000);
        $('#Pcuscontactname').html('Enter Contact Name');
        $('#cuscontactname').focus();
        return false;
    }

    if ($('#cusmobile').val() != "" && $('#cusmobile').val().length != "10") {
        $('#Pcusmobile').show();
        $('#Pcusmobile').fadeOut(8000);
        $('#Pcusmobile').html('Invalid Mobile Number');
        $('#cusmobile').focus();
        return false;
    }

    if ($.trim($('#cusactive').val()) == "") {
        $('#Pactive').show();
        $('#Pactive').fadeOut(8000);
        $('#Pactive').html('Select Active');
        $('#cusactive').focus();
        return false;
    }

    if ($.trim($('#cusemailid').val()) == "") {
        $('#Pcusemailid').show();
        $('#Pcusemailid').fadeOut(8000);
        $('#Pcusemailid').html('Enter Email Id');
        $('#cusemailid').focus();
        return false;
    }


    var date = ''
    if ($('#cuslicenceexprydate').val() != '') {
        var date1 = $('#cuslicenceexprydate').val().split('/');
        date = date1[2] + '-' + date1[1] + '-' + date1[0];
    }

    var indicator = '', customer_master_id = ''
    if ($('#customersubmit').html() == 'Submit') {
        indicator = 'IS',
        customer_master_id = 0
    }
    else {
        indicator = 'UP',
        customer_master_id = $('#Hdcusid').val()
    }

    var submit = {
        Dml_Indicator: indicator,
        Printing_name: $('#cusprintname').val(),
        Category_name: $('#cuscatogaryname').val(),
        Contact_person: $('#cuscontactname').val(),
        Address: $('#cusaddress').val(),
        City: $('#cuscity').val(),
        Pincode: $('#cuspincode').val(),
        Phone_number1: $('#cusphoneno1').val(),
        Phone_number2: $('#cusphoneno2').val(),
        Mobile_number: $('#cusmobile').val(),
        Email_id: $('#cusemailid').val(),
        Credit_limit_days: $('#cuscreditlimit').val(),
        IEC_code: $('#cusieccode').val(),
        Licence_no: $('#cuslicenceno').val(),
        Licence_expiry_date: date,
        Fax_no: $('#cusfaxno').val(),
        GST_no: $('#cusgstno').val(),
        Pan_no: $('#cuspanno').val(),
        Credit_limit_amount: $('#cuscredit').val(),
        Active: $('#cusactive').val(),
        Notes: $('#cusnotes').val(),
        Hospital_id: $('#HospitalId').val(),
        Created_by: $('#hiddenUser_id').val(),
        Customer_master_id: customer_master_id,
        Branch_id: '0'
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteCustomermaster',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(submit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                debugger;
                var data2 = data1.data;

                if (data2.Table[0].Message == 'Inserted') {
                    toastr.success('Inserted Successfully');
                    //setTimeout(function () {
                    //    location.reload();
                    //}, 1000)
                }
                else if (data2.Table[0].Message == 'Updated') {
                    toastr.success('Updated Successfully');
                    //setTimeout(function () {
                    //    location.reload();
                    //}, 1000)
                }
                else {
                    toastr.success('Somthing Went Wrong');
                }
                $('.nav-tabs li').removeClass('active');
                $('a[href="#customer_master"]').tab('show');
                gobackcustomer()
                LoadCustomer()
            }
            else {
                toastr.success('Somthing Went Wrong');
            }
            //$('#Customer_tab').show();
        },
    });
});

function LoadCustomer() {
    debugger;

    values = {
        Dml_Indicator: "S",
        hospital_id: $('#HospitalId').val(),
        Contact_person: $('#CMFContactName').val(),
        Mobile_number: $('#CMFMobileNo').val(),
        Email_id: $('#CMFEmailId').val(),
        Licence_no: $('#CMFLincenceNo').val(),
        Fax_no: $('#CMFFaxNo').val(),
        GST_no: $('#CMFGSTNo').val(),
        Pan_no: $('#CMFPANNo').val()
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteCustomermaster',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(values),
        success: function (data) {
            console.log(data);
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    $('#tbl_customer tbody').empty();
                    $('#tbl_customer').dataTable().fnClearTable();
                    $('#tbl_customer').dataTable().fnDraw();
                    $('#tbl_customer').dataTable().fnDestroy();

                    var j = 0, b = '', tr = '', edit = '', del = '', active_inactive = '', active = '', other_info = '';

                    b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++

                        edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="SM_E1" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=Customeredit(this,"' + emp.customer_master_id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = ' <a  data-toggle="tooltip" title="Delete"  id="Customerdel_' + emp.customer_master_id + '" class="btn deleteicon-btn btn-xs"  data-id="SM_D1" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form"  href="javascript:void(0);" onclick=Customerdel(this,"' + emp.customer_master_id + '","D")><i class="fa fa-trash" aria-hidden="true"></i></a>';
                        other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + ' </div></div>';

                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.contact_person + '</td><td>' + emp.address + '</td><td >' + emp.phone_number1 + '</td><td >' + emp.phone_number2 + '</td><td >' + emp.mobile_number + '</td><td >' + emp.email_id + '</td><td >' + emp.licence_no + '</td><td >' + emp.fax_no + '</td><td >' + emp.GST_no + '</td><td >' + emp.pan_no + '</td><td>' + other_info + '</td></tr>';

                    });
                    $('#tbl_customer tbody').html(tr);
                    datatable('tbl_customer', 'Customer_list', 'Legal');
                }
            }
            else {
                console.log(data);
            }
        }
    });
}

function Customeredit(event, id) {
    debugger
    //$('#form_back').show();
    //$('#customer_form').show();
    //$('#customer_table_div').hide();

    $('#customer_form').show();
    $('#customer_table_div').hide();
    $('#divmaster').hide();
    $('#regtab').hide();
    $('#divcustomer').show();

    $('#customersubmit').html('Update')


    var Edit = {
        Customer_master_id: id,
        Dml_Indicator: 'E'
    };

    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteCustomermaster',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#cuscatogaryname").val(emp[0].category_name);
            $("#cusprintname").val(emp[0].printing_name);
            $("#cuscontactname").val(emp[0].contact_person);
            $("#cusaddress").val(emp[0].address);
            $("#cuscity").val(emp[0].city);
            $("#cuspincode").val(emp[0].pincode);
            $("#cusphoneno1").val(emp[0].phone_number1);
            $("#cusphoneno2").val(emp[0].phone_number2);
            $("#cusmobile").val(emp[0].mobile_number);
            $("#cusemailid").val(emp[0].email_id);
            $("#cuscreditlimit").val(emp[0].credit_limit_days);
            $("#cusieccode").val(emp[0].IEC_code);
            $("#cuslicenceno").val(emp[0].licence_no);
            $("#cuslicenceexprydate").val(emp[0].licence_expiry_date);
            $("#cusfaxno").val(emp[0].fax_no);
            $("#cusgstno").val(emp[0].GST_no);
            $("#cuspanno").val(emp[0].pan_no);
            $("#cuscredit").val(emp[0].credit_limit_amount);
            $("#cusactive").val(emp[0].active);
            $("#cusnotes").val(emp[0].notes);
            $("#Hdcusid").val(emp[0].customer_master_id);

        }
    })
}
function Customerdel(event, val, Dml_Indicator) {
    debugger;

    if (val != '') {
        status = "Are You Sure, Do you want to Delete?";
    }
    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
{

    closeButton: true,
    progressBar: true,
    allowHtml: true,
    timeOut: 20000000,

    onShown: function (toast) {
        $("#confirmationRevertYes").click(function () {
            debugger;

            var servicevalues = {
                Customer_master_id: val,
                Dml_Indicator: Dml_Indicator

            }


            $.ajax({
                async: true,
                type: 'POST',
                url: '/BloodBank/insert_update_deleteCustomermaster',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(servicevalues),
                success: function (data) {
                    debugger;

                    var data2 = JSON.parse(data);


                    if (jQuery.isEmptyObject((data2.data))) {

                    }
                    else {

                        if (data2.msg == "Success") {
                            debugger;
                            data1 = data2.data;
                            if (Dml_Indicator == 'D') {
                                toastr.success("Deleted Successfully ");
                                LoadCustomer()

                            }
                            else {
                                toastr.warning("Somthing Went Wrong");
                                return false;
                            }
                        }
                    }
                }
            });
        });
    }
});
}


function LoadCuscity() {
    var state = {
        state_id: '1',
        Dml_Indicator: "CI"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var City;
                    $('#cuscity').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        City = City + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#cuscity').append(default_opt + City);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
};

function clearcustomer() {
    $('#cuscatogaryname').val(0),
     $('#cusprintname').val(''),
     $('#cuscontactname').val(''),
     $('#cusaddress').val(''),
     $('#cuscity').val(0),
     $('#cuspincode').val(''),
     $('#cusphoneno1').val(''),
     $('#cusphoneno2').val(''),
     $('#cusmobile').val(''),
     $('#cusemailid').val(''),
     $('#cuscreditlimit').val(''),
     $('#cusieccode').val(''),
    $('#cuslicenceno').val(''),
    $('#cuslicenceexprydate').val(''),
    $('#cusfaxno').val(''),
    $('#cusgstno').val(''),
     $('#cuspanno').val(''),
     $('#cuscredit').val(''),
     $('#cusactive').val(0),
     $('#cusnotes').val('')
}