$(document).ready(function () {
    debugger;
    LoadVendor()
    Loadcity()
});
$('#plvendor_master').click(function () {
    debugger;
    LoadVendor();
    $('.asidefilterclass').addClass('hidden');
    $('#asideVendor_Master').removeClass('hidden');
    $('.groupclass').addClass('hidden');
    $('#asideVendor_Mastergroupclass').removeClass('hidden');
    $('.Filterclass').addClass('hidden');
    $('#asideVendor_MasterFilterclass').removeClass('hidden');

});
$('#VM_filter').click(function () {
    LoadVendor();
})
$('#VM_clear').click(function () {
    $('#VMFContactName').val('');
    $('#VMFMobileNo').val('');
    $('#VMFEmailId').val('');
    $('#VMFLincenceNo').val('');
    $('#VMFFaxNo').val('');
    $('#VMFGSTNo').val('');
    $('#VMFPANNo').val('');
    LoadVendor();
})
$('#vendorsubmit').on("click", function () {
    debugger;

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

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

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

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

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

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


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

    var indicator = '', vendor_master_id = ''
    if ($('#vendorsubmit').html() == 'Submit') {
        indicator = 'IS',
        vendor_master_id = 0
    }
    else {
        indicator = 'UP',
        vendor_master_id = $('#HdVendorid').val()
    }

    var submit = {
        Dml_Indicator: indicator,
        Printing_name: $('#printname').val(),
        Category_name: $('#catogaryname').val(),
        Contact_person: $('#Vencontactname').val(),
        Address: $('#vendoraddress').val(),
        City: $('#vendorcity').val(),
        Pincode: $('#vendorpincode').val(),
        Phone_number1: $('#vendorphoneno1').val(),
        Phone_number2: $('#vendorphoneno2').val(),
        Mobile_number: $('#vendormobile').val(),
        Email_id: $('#vendoremailid').val(),
        Credit_limit_days: $('#vendorcreditlimit').val(),
        IEC_code: $('#vendorieccode').val(),
        Licence_no: $('#licenceno').val(),
        Licence_expiry_date: date,
        Fax_no: $('#faxno').val(),
        GST_no: $('#gstno').val(),
        Pan_no: $('#panno').val(),
        Credit_limit_amount: $('#vencredit').val(),
        Active: $('#active').val(),
        Notes: $('#vendornotes').val(),
        Hospital_id: $('#HospitalId').val(),
        Created_by: $('#hiddenUser_id').val(),
        vendor_master_id: vendor_master_id,
        Branch_id: '0'
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteVendormaster',
        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="#vendor_master"]').tab('show');
                gobackvendor()
                LoadVendor()
            }
            else {
                toastr.success('Somthing Went Wrong');
            }
        },
    });
});

function LoadVendor() {
    debugger;

    values = {
        Dml_Indicator: "S",
        hospital_id: $('#HospitalId').val(),
        Contact_person: $('#VMFContactName').val(),
        Mobile_number: $('#VMFMobileNo').val(),
        Email_id: $('#VMFEmailId').val(),
        Licence_no: $('#VMFLincenceNo').val(),
        Fax_no: $('#VMFFaxNo').val(),
        GST_no: $('#VMFGSTNo').val(),
        Pan_no: $('#VMFPANNo').val()
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteVendormaster',
        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_vendor tbody').empty();
                    $('#tbl_vendor').dataTable().fnClearTable();
                    $('#tbl_vendor').dataTable().fnDraw();
                    $('#tbl_vendor').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=Vendoredit(this,"' + emp.vendor_master_id + '")><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        del = ' <a  data-toggle="tooltip" title="Delete"  id="Vendordel_' + emp.vendor_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=Vendordel(this,"' + emp.vendor_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.printing_name + '</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_vendor tbody').html(tr);
                    datatable('tbl_vendor', 'Vendor_List', 'Legal');
                }
            }
            else {
                console.log(data);
            }
        }
    });
}

function Vendoredit(event, id) {
    debugger
    //$('#form_back').show();
    //$('#vendor_form').show();
    //$('#vendor_table_div').hide();

    $('#vendor_form').show();
    $('#vendor_table_div').hide();
    $('#divmaster').hide();
    $('#regtab').hide();
    $('#divvendor').show();

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


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

    $.ajax({
        async: true,
        type: 'POST',
        url: '/BloodBank/insert_update_deleteVendormaster',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Edit),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            var emp = data1.data.Table;
            $("#catogaryname").val(emp[0].category_name);
            $("#printname").val(emp[0].printing_name);
            $("#Vencontactname").val(emp[0].contact_person);
            $("#vendoraddress").val(emp[0].address);
            $("#vendorcity").val(emp[0].city);
            $("#vendorpincode").val(emp[0].pincode);
            $("#vendorphoneno1").val(emp[0].phone_number1);
            $("#vendorphoneno2").val(emp[0].phone_number2);
            $("#vendormobile").val(emp[0].mobile_number);
            $("#vendoremailid").val(emp[0].email_id);
            $("#vendorcreditlimit").val(emp[0].credit_limit_days);
            $("#vendorieccode").val(emp[0].IEC_code);
            $("#licenceno").val(emp[0].licence_no);
            $("#licenceexprydate").val(emp[0].licence_expiry_date);
            $("#faxno").val(emp[0].fax_no);
            $("#gstno").val(emp[0].GST_no);
            $("#panno").val(emp[0].pan_no);
            $("#vencredit").val(emp[0].credit_limit_amount);
            $("#active").val(emp[0].active);
            $("#vendornotes").val(emp[0].notes);
            $("#HdVendorid").val(emp[0].vendor_master_id);

        }
    })
}

function Vendordel(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 = {
                vendor_master_id: val,
                Dml_Indicator: Dml_Indicator

            }


            $.ajax({
                async: true,
                type: 'POST',
                url: '/BloodBank/insert_update_deleteVendormaster',
                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 ");
                                LoadVendor()

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

function Loadcity() {
    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;
                    $('#vendorcity').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        City = City + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#vendorcity').append(default_opt + City);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
};

function clearvendor() {
    $('#catogaryname').val(0),
     $('#printname').val(''),
     $('#Vencontactname').val(''),
     $('#vendoraddress').val(''),
     $('#vendorcity').val(0),
     $('#vendorpincode').val(''),
     $('#vendorphoneno1').val(''),
     $('#vendorphoneno2').val(''),
     $('#vendormobile').val(''),
     $('#vendoremailid').val(''),
     $('#vendorcreditlimit').val(''),
     $('#vendorieccode').val(''),
    $('#licenceno').val(''),
    $('#licenceexprydate').val(''),
    $('#faxno').val(''),
    $('#gstno').val(''),
     $('#panno').val(''),
     $('#vencredit').val(''),
     $('#active').val(0),
     $('#vendornotes').val('')
}