$(document).ready(function () {
    
    LoadMedicineType();
    LoadPackingList();
    LoadMedicineList();
    LoadDoctorMedicineList();
    
});

//function LoadPharmacyID() {
//    var screen = {
//        doctor_id: $('#HiddenDoctorID').val(),
//        Dml_Indicator: 'S'
//    }
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: '/Dashboard/Dashboard_count',
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(screen),
//        success: function (datas) {
//            if (jQuery.isEmptyObject((datas))) {

//            }
//            else {
//                debugger;
//                var data = JSON.parse(datas);
//                if (data.Table9.length > 0) {
//                    var pharmacyID = data.Table9[0].pharmacy_id;
//                    $('#txtPharmacyID').val(pharmacyID);
                    
//                }
//            }
//        }
//    });
//}

function LoadMedicineType() {
    var DoctorData ={
        Dml_Indicator:"S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/MedicineType',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {

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

            }
            else {
                var data1 = JSON.parse(data);
                var selType = '';
                $('#selMedicineType').empty();
                if (data1.length > 0) {
                    for (var i = 0; i < data1.length; i++) {

                        selType = selType + '<option value="' + data1[i].medicine_type_id + '">' + data1[i].medicine_type + '</option>';

                    }

                    $('#selMedicineType').append(selType);
                }
            }
        }
    });
}

function LoadPackingList() {
    debugger;
    $.ajax({
        async: true,
        type: 'GET',
        url: '/CaseStudy/GetPackingMaster',
        contentType: 'application/json; charset=UTF-8',
        success: function (data) {

            debugger;
            if (jQuery.isEmptyObject((data))) {
                
            }
            else {
                var data1 = JSON.parse(data);
                var selPack = '';
                $('#selPacking').empty();
                if (data1.packing_details.length > 0) {
                    for (var i = 0; i < data1.packing_details.length; i++) {

                        selPack = selPack + '<option value="' + data1.packing_details[i].pharmacy_packing_id + '">' + data1.packing_details[i].pharmacy_packing_details + '</option>';

                    }

                    $('#selPacking').append(selPack);
                }
            }
        }
    });
}

var limitStart = 0;
var limitEnd = 100;
function LoadMedicineList() {

    if ($('#txtPharmacyID').val() == null || $('#txtPharmacyID').val() == '') {
        toastr.warning('Unable to Load Medicines list');
        return false;
    }

    var DoctorData = {
        pharmacy_id: $('#txtPharmacyID').val(),
        limitstart: 0,
        limitend: 100
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/GetMedicineFullPriceDetailsList',
        contentType: 'application/json; charset=UTF-8',
        data:JSON.stringify(DoctorData),
        success: function (data) {

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

            }
            else {
                debugger;
                var data1 = JSON.parse(data);
                $('#tblBody').empty();
                if (data1.price_list.length > 0) {
                    
                    var tr = '';
                    
                    for (var i = 0; i < data1.price_list.length; i++) {
                        
                        tr = tr + '<tr><td>' + data1.price_list[i].medicine_id + '</td><td id=searchCol>' + data1.price_list[i].medicine_type + '</td><td id=searchCol>' + data1.price_list[i].medicine_name + '</td><td id=searchCol>' + data1.price_list[i].generic_name + '</td><td>' + data1.price_list[i].price + '</td><td>' + $("#txtDiscountType option[value='" + data1.price_list[i].discount_type + "']").text() + '</td><td>' + data1.price_list[i].discount + '</td><td>' + $("#txtGSTSlab option[value='" + data1.price_list[i].gst_slab + "']").text() + '</td><td>' + data1.price_list[i].min_quantity + '</td><td>' + data1.price_list[i].min_unit + '</td><td><button onclick=\'EditMedicine(\"' + data1.price_list[i].medicine_id + '\",\"' + data1.price_list[i].medicine_name + '\",\"' + data1.price_list[i].generic_name + '\",\"' + data1.price_list[i].medicine_type_id + '\",\"' + data1.price_list[i].min_quantity + '\",\"' + data1.price_list[i].price + '\",\"' + data1.price_list[i].discount_type + '\",\"' + data1.price_list[i].discount + '\",\"' + data1.price_list[i].gst_slab + '\",\"' + data1.price_list[i].min_unit + '\",\"' + data1.price_list[i].generic_id + '\")\';><span class="edit btn btn-default  btn-sm btn-add"><i class="fa fa-pencil" aria-hidden="true"></i></span></button></td></tr>';
                       
                    }
                   // var body = '<tbody>' + tr + '</tbody>';
                    $('#tblBody').append(tr);
                }
            }
        }
    });
}

function LoadMore() {
    debugger;
    limitStart = limitStart + 100;
    limitEnd = 100;

    var DoctorData = {
        pharmacy_id: $('#txtPharmacyID').val(),
        limitstart: limitStart,
        limitend: limitEnd
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/GetMedicineFullPriceDetailsList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {

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

            }
            else {
                var data1 = JSON.parse(data);
                
                if (data1.price_list.length > 0) {
                    var tr = '';
                    for (var i = 0; i < data1.price_list.length; i++) {

                        tr = tr + '<tr><td>' + data1.price_list[i].medicine_id + '</td><td id=searchCol>' + data1.price_list[i].medicine_type_name + '</td><td id=searchCol>' + data1.price_list[i].medicine_name + '</td><td id=searchCol>' + data1.price_list[i].generic_name + '</td><td>' + data1.price_list[i].price + '</td><td>' + $("#txtDiscountType option[value='" + data1.price_list[i].discount_type + "']").text() + '</td><td>' + data1.price_list[i].discount + '</td><td>' + $("#txtGSTSlab option[value='" + data1.price_list[i].gst_slab + "']").text() + '</td><td>' + data1.price_list[i].min_quantity + '</td><td>' + data1.price_list[i].min_unit + '</td><td><button onclick=\'EditMedicine(\"' + data1.price_list[i].medicine_id + '\",\"' + data1.price_list[i].medicine_name + '\",\"' + data1.price_list[i].generic_name + '\",\"' + data1.price_list[i].medicine_type_id + '\",\"' + data1.price_list[i].min_quantity + '\",\"' + data1.price_list[i].price + '\",\"' + data1.price_list[i].discount_type + '\",\"' + data1.price_list[i].discount + '\",\"' + data1.price_list[i].gst_slab + '\",\"' + data1.price_list[i].min_unit + '\",\"' + data1.price_list[i].generic_id + '\")\';><span class="edit btn btn-default  btn-sm btn-add"><i class="fa fa-pencil" aria-hidden="true"></i></span></button></td></tr>';

                    }
                    // var body = '<tbody>' + tr + '</tbody>';
                    $('#tblBody').append(tr);
                }
            }
        }
    });
}

function LoadDoctorMedicineList() {
    if ($('#txtPharmacyID').val() == null || $('#txtPharmacyID').val() == '') {
        toastr.warning('Unable to Load Medicines list');
        return false;
    }

    var DoctorData = {
        pharmacy_id: $('#txtPharmacyID').val(),
        Dml_Indicator:"DFPL"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/GetMedicineFullPriceDetailsList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {

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

            }
            else {
                var data1 = JSON.parse(data);
                $('#tblDoctorBody').empty();
                if (data1.price_list.length > 0) {

                    var tr = '';

                    for (var i = 0; i < data1.price_list.length; i++) {

                        tr = tr + '<tr><td>' + data1.price_list[i].medicine_id + '</td><td id=searchCol>' + data1.price_list[i].medicine_type + '</td><td id=searchCol>' + data1.price_list[i].medicine_name + '</td><td id=searchCol>' + data1.price_list[i].generic_name + '</td><td>' + data1.price_list[i].price + '</td><td>' + $("#txtDiscountType option[value='" + data1.price_list[i].discount_type + "']").text() + '</td><td>' + data1.price_list[i].discount + '</td><td>' + $("#txtGSTSlab option[value='" + data1.price_list[i].gst_slab + "']").text() + '</td><td>' + data1.price_list[i].min_quantity + '</td><td>' + data1.price_list[i].min_unit + '</td><td><button onclick=\'EditMedicine(\"' + data1.price_list[i].medicine_id + '\",\"' + data1.price_list[i].medicine_name + '\",\"' + data1.price_list[i].generic_name + '\",\"' + data1.price_list[i].medicine_type_id + '\",\"' + data1.price_list[i].min_quantity + '\",\"' + data1.price_list[i].price + '\",\"' + data1.price_list[i].discount_type + '\",\"' + data1.price_list[i].discount + '\",\"' + data1.price_list[i].gst_slab + '\",\"' + data1.price_list[i].min_unit + '\",\"' + data1.price_list[i].generic_id + '\")\';><span class="edit btn btn-default  btn-sm btn-add"><i class="fa fa-pencil" aria-hidden="true"></i></span></button></td></tr>';

                    }
                    // var body = '<tbody>' + tr + '</tbody>';
                    $('#tblDoctorBody').append(tr);
                    
                }
            }
        }
    });
}

function EditMedicine(medicine_id, medicine_name, generic_name, medicine_type_id, min_quantity, price,discount_type,discount,gst_slab,min_unit,generic_id) {
    $('#divMedicineID').show();
    $('#txtMedicineID').val(medicine_id);
   
    $('#txtMedicineName').val(medicine_name);
    
    $('#selMedicineType').val(medicine_type_id);
    $('#txtGenericName').val(generic_name);
    $('#txtMinQuantity').val(min_quantity);
    $('#txtPrice').val(price);
  
    $('#txtDiscountType').val(discount_type);
    $('#txtDiscount').val(discount);
    
    $('#txtGSTSlab').val(gst_slab);
    $('#selPacking').val(min_unit);
    $('#txtGenericID').val(generic_id);

    $('#headerTitle').text("Update Medicine Price");
    $('#divAddNew').show();
    $('#btnUpdate').show();
    $('#btnSubmit').hide();
    $('#txtMedicineName').prop('readonly', true);
    $('#txtMedicineID').prop('readonly', true);

    $('#divMedicineList').hide();
    $('#divDoctorMedicineList').hide();
    $('#btnClose').show();
    $('#btnAdd').hide();
}

$('#btnUpdate').click(function () {
    debugger;
    if ($('#txtMedicineID').val() == '' || $('#txtMedicineID').val() == null) {
        $('#PMedicineID').show();
        $('#PMedicineID').fadeOut(8000);
        $('#PMedicineID').html('Enter Medicine ID');
        $('#txtMedicineID').focus();
        $('#txtMedicineID').val('');
        return false;
    }

    if ($('#txtMedicineName').val() == '' || $('#txtMedicineName').val() == null) {
        $('#PMedicineName').show();
        $('#PMedicineName').fadeOut(8000);
        $('#PMedicineName').html('Enter Medicine Name');
        $('#txtMedicineName').focus();
        $('#txtMedicineName').val('');
        return false;
    }

    if ($('#txtPrice').val() == '' || $('#txtPrice').val() == null) {
        $('#PPrice').show();
        $('#PPrice').fadeOut(8000);
        $('#PPrice').html('Enter Price');
        $('#txtPrice').focus();
        $('#txtPrice').val('');
        return false;
    }

    if ($('#txtMinQuantity').val() == '' || $('#txtMinQuantity').val() == null) {
        $('#PMinQty').show();
        $('#PMinQty').fadeOut(8000);
        $('#PMinQty').html('Enter Minimum Quantity');
        $('#txtMinQuantity').focus();
        $('#txtMinQuantity').val('');
        return false;
    }

    if ($('#selPacking').val() == '' || $('#selPacking').val() == null) {
        $('#PMinUnit').show();
        $('#PMinUnit').fadeOut(8000);
        $('#PMinUnit').html('Enter Minimum Unit');
        $('#selPacking').focus();
        $('#selPacking').val('');
        return false;
    }

    var DoctorData = {
        GST_slab: $('#txtGSTSlab').val(),
        P_update_by: null,
        discount: $('#txtDiscount').val(),
        discount_type: $('#txtDiscountType').val(),
        doctor_id: $('#txtPharmacyID').val(),
        generic_id: $('#txtGenericID').val(),
        generic_name: $('#txtGenericName').val(),
        medicine_id: $('#txtMedicineID').val(),
        medicine_name: $('#txtMedicineName').val(),
        medicine_status: "1",
        medicine_type_id: $('#selMedicineType').val(),
        min_quantity: $('#txtMinQuantity').val(),
        min_unit: $('#selPacking').val(),
        pharmacy_id: $('#txtPharmacyID').val(),
        price: $('#txtPrice').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/setmasterprice_details',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {

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

            }
            else {
                var data1 = JSON.parse(data);
                
                toastr.success('Updated successfully');
                $('#txtGenericID').val('');
                $('#btnAdd').show();
                $('#btnClose').hide();
                $('#divMedicineList').show();
                $('#divAddNew').hide();
                LoadMedicineList();
                LoadDoctorMedicineList();
            }
        }
    });
});


$('#btnSubmit').click(function () {

    //if ($('#txtMedicineID').val() == '' || $('#txtMedicineID').val() == null) {
    //    $('#PMedicineID').show();
    //    $('#PMedicineID').fadeOut(8000);
    //    $('#PMedicineID').html('Enter Medicine ID');
    //    $('#txtMedicineID').focus();
    //    $('#txtMedicineID').val('');
    //    return false;
    //}

    if ($('#txtMedicineName').val() == '' || $('#txtMedicineName').val() == null) {
        $('#PMedicineName').show();
        $('#PMedicineName').fadeOut(8000);
        $('#PMedicineName').html('Enter Medicine Name');
        $('#txtMedicineName').focus();
        $('#txtMedicineName').val('');
        return false;
    }

    if ($('#txtPrice').val() == '' || $('#txtPrice').val() == null) {
        $('#PPrice').show();
        $('#PPrice').fadeOut(8000);
        $('#PPrice').html('Enter Price');
        $('#txtPrice').focus();
        $('#txtPrice').val('');
        return false;
    }

    if ($('#txtMinQuantity').val() == '' || $('#txtMinQuantity').val() == null) {
        $('#PMinQty').show();
        $('#PMinQty').fadeOut(8000);
        $('#PMinQty').html('Enter Minimum Quantity');
        $('#txtMinQuantity').focus();
        $('#txtMinQuantity').val('');
        return false;
    }

    if ($('#selPacking').val() == '' || $('#selPacking').val() == null) {
        $('#PMinUnit').show();
        $('#PMinUnit').fadeOut(8000);
        $('#PMinUnit').html('Enter Minimum Unit');
        $('#selPacking').focus();
        $('#selPacking').val('');
        return false;
    }


    var DoctorData = {
        GST_slab: $('#txtGSTSlab').val(),
        P_update_by: null,
        discount: $('#txtDiscount').val(),
        discount_type: $('#txtDiscountType').val(),
        doctor_id: $('#txtPharmacyID').val(),
        generic_id: $('#txtGenericID').val(),
        generic_name: $('#txtGenericName').val(),
        medicine_id: $('#txtMedicineID').val(),
        medicine_name: $('#txtMedicineName').val(),
        medicine_status: "1",
        medicine_type_id: $('#selMedicineType').val(),
        min_quantity: $('#txtMinQuantity').val(),
        min_unit: $('#selPacking').val(),
        pharmacy_id: $('#txtPharmacyID').val(),
        price: $('#txtPrice').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/setmasterprice_details',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {

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

            }
            else {
                var data1 = JSON.parse(data);

                toastr.success('Saved successfully');
                $('#txtMedicineID').val('');
                $('#txtMedicineName').val('');
                $('#selMedicineType').val('');
                $('#txtMinQuantity').val('');
                $('#txtPrice').val('');
                $('#selPacking').val('');
                $('#txtGSTSlab').val('');
                $('#txtDiscount').val('');
                $('#txtDiscountType').val('');
                $('#txtGenericID').val('');
                $('#txtGenericName').val('');
            }
        }
    });
});