$(document).ready(function () {
    if ($('#Hdncoupon_code').val() != "" && $('#Hdncoupon_code').val() != null) {
        coupon_voucher_view($('#Hdncoupon_code').val());
        $('#coupon_type,#couponname,#coupon_start_date,#coupon_end_date,#Dcoupondescription,#coupon_discount_amt,#coupon_amt_type,#coupon_min_amt,#coupon_usage_patient,#coup_appli_to,#coup_appli_mode,#coup_tnd,#lab_test_count,#con_walkin,#con_video,#con_first_consultation,#con_all,#lab_first_order,#lab_no_test,#lab_all,#pack_first_order,#pack_all,#pack_homecare,#pack_hospitalcare,#med_first_order,#med_all,#voucher_max_count,#voucher_no_patient,#coupon_issue_method,#coupon_issue_point,#voucher_expire_days').attr("disabled", true);
        $('#labheader-section').collapse('show');
    }
    $('#voucher_max_count,#voucher_no_patient,#coupon_issue_method,#coupon_issue_point,#voucher_expire_days').attr("disabled", true);
    LoadBranch();
});


//on change coupon method
$('#coupon_type').change(function () {
    if ($('#coupon_type').val() == "1") {
        $('#voucher_details_div').hide();
        $('#voucher_max_count,#voucher_no_patient,#voucher_expire_days,#coupon_issue_method,#coupon_issue_point').val('');
    }
    else if ($('#coupon_type').val() == "2") {
        $('#voucher_details_div').show();
        $('#voucher_max_count,#voucher_no_patient,#coupon_issue_method,#coupon_issue_point,#voucher_expire_days').attr("disabled", false);
    }
    else {
        $('#voucher_details_div').hide();
        $('#voucher_max_count,#voucher_no_patient,#voucher_expire_days,#coupon_issue_method,#coupon_issue_point').val('');
    }
});


//coupon applicable to change function
$('#coup_appli_to').change(function () {
    if ($('#coup_appli_to').val() == "2") LoadDoctor();
    else $('#doctor_list_div').hide();

});

function LoadDoctor() {
    $('#doctor_list_div').show();
    var roleId = $('#hiddenRole_id').val();
    var DoctorId = $('#HiddenDoctorID').val();
    var DoctorData = {
        Dml_Indicator: "DL",
        branch_id: $('#pkg_branch_id').val(),
        hospital_id: $('#HospitalId').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CalendarConfiguration/HosiptalSelect',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data1 = JSON.parse(data)
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {

                    var tr = '';
                    var SelRole = '';
                    $('#doctor_list').empty();
                    var default_opt = '<option value="">--Select--</option>';
                    var j = 0;
                    if (data1.length > 0) {
                        for (var i = 0; i < data1.length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data1[i].doctor_id + '">' + data1[i].first_name + '</option>';
                        }
                        $('#doctor_list').html(default_opt + SelRole);
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

$('#btnAddCoup').click(function () {
    if ($.trim($('#coupon_type').val()).length == "") {
        $('#Pcoupon_type').show();
        $('#Pcoupon_type').fadeOut(8000);
        $('#Pcoupon_type').html('Select Discount Type');
        $('#coupon_type').focus();
        return false;
    }
    if ($.trim($('#couponname').val()).length == "") {
        $('#Pcouponname').show();
        $('#Pcouponname').fadeOut(8000);
        $('#Pcouponname').html('Enter Discount Name');
        $('#couponname').focus();
        $('#couponname').val('');
        return false;
    }
    if ($.trim($('#pkg_branch_id').val()) == "" || $('#pkg_branch_id').val() == '0') {
        $('#Ppkg_branch_id').show();
        $('#Ppkg_branch_id').fadeOut(8000);
        $('#Ppkg_branch_id').html('Select Branch');
        $('#pkg_branch_id').focus();
        $('#pkg_branch_id').val('');
        return false;
    }
    if ($.trim($('#coupon_start_date').val()).length == "") {
        $('#Pcoupon_start_date').show();
        $('#Pcoupon_start_date').fadeOut(8000);
        $('#Pcoupon_start_date').html('Enter Discount Start Date');
        $('#coupon_start_date').focus();
        $('#coupon_start_date').val('');
        return false;
    }
    if ($.trim($('#coupon_end_date').val()).length == "") {
        $('#Pcoupon_end_date').show();
        $('#Pcoupon_end_date').fadeOut(8000);
        $('#Pcoupon_end_date').html('Enter Discount End Date');
        $('#coupon_end_date').focus();
        $('#coupon_end_date').val('');
        return false;
    }
    if ($.trim($('#Dcoupondescription').val()).length == "") {
        $('#PDcoupondescription').show();
        $('#PDcoupondescription').fadeOut(8000);
        $('#PDcoupondescription').html('Enter Discount Description');
        $('#Dcoupondescription').focus();
        $('#Dcoupondescription').val('');
        return false;
    }
    if ($.trim($('#coupon_discount_amt').val()) == "") {
        $('#Pcoupon_discount_amt').show();
        $('#Pcoupon_discount_amt').fadeOut(8000);
        $('#Pcoupon_discount_amt').html('Enter Discount Amount');
        $('#coupon_discount_amt').focus();
        $('#coupon_discount_amt').val('');
        return false;
    }
    if ($.trim($('#coupon_min_amt').val()).length == "") {
        $('#Pcoupon_min_amt').show();
        $('#Pcoupon_min_amt').fadeOut(8000);
        $('#Pcoupon_min_amt').html('Enter Minimum Order Amount');
        $('#coupon_min_amt').focus();
        $('#coupon_min_amt').val('');
        return false;
    }
    if ($.trim($('#coupon_usage_patient').val()) == "") {
        $('#Pcoupon_usage_patient').show();
        $('#Pcoupon_usage_patient').fadeOut(8000);
        $('#Pcoupon_usage_patient').html('Enter No of Usage');
        $('#coupon_usage_patient').focus();
        $('#coupon_usage_patient').val('');
        return false;
    }
    if ($.trim($('#coup_appli_to').val()) == "2") {
        if ($.trim($('#doctor_list').val()) == "") {
            $('#Pdoctor_list').show();
            $('#Pdoctor_list').fadeOut(8000);
            $('#Pdoctor_list').html('Enter No of Usage');
            $('#doctor_list').focus();
            return false;
        }
    }
    if ($.trim($('#coupon_type').val()) == "2") {
        if ($.trim($('#voucher_max_count').val()).length == "") {
            $('#Pvoucher_max_count').show();
            $('#Pvoucher_max_count').fadeOut(8000);
            $('#Pvoucher_max_count').html('Enter Max number of Vouchers to be issued');
            $('#voucher_max_count').focus();
            $('#voucher_max_count').val('');
            return false;
        }
        if ($.trim($('#voucher_no_patient').val()).length == "") {
            $('#Pvoucher_no_patient').show();
            $('#Pvoucher_no_patient').fadeOut(8000);
            $('#Pvoucher_no_patient').html('Enter No of Vouchers per patient');
            $('#voucher_no_patient').focus();
            $('#voucher_no_patient').val('');
            return false;
        }
        if ($.trim($('#coupon_issue_method').val()).length == "") {
            $('#Pcoupon_issue_method').show();
            $('#Pcoupon_issue_method').fadeOut(8000);
            $('#Pcoupon_issue_method').html('Select Voucher Issue Method');
            $('#coupon_issue_method').focus();
            return false;
        }
        if ($.trim($('#coupon_issue_point').val()).length == "") {
            $('#Pcoupon_issue_point').show();
            $('#Pcoupon_issue_point').fadeOut(8000);
            $('#Pcoupon_issue_point').html('Select Voucher Issue Point');
            $('#coupon_issue_point').focus();
            return false;
        }
        if ($.trim($('#voucher_expire_days').val()).length == "") {
            $('#Pvoucher_expire_days').show();
            $('#Pvoucher_expire_days').fadeOut(8000);
            $('#Pvoucher_expire_days').html('Enter No of expiry days for voucher');
            $('#voucher_expire_days').focus();
            $('#voucher_expire_days').val('');
            return false;
        }
    }
    if (($('#con_walkin').prop("checked") == false) && ($('#con_video').prop("checked") == false) && ($('#con_first_consultation').prop("checked") == false) && ($('#con_all').prop("checked") == false) && ($('#lab_first_order').prop("checked") == false) && ($('#lab_no_test').prop("checked") == false) && ($('#lab_all').prop("checked") == false) && ($('#med_first_order').prop("checked") == false) && ($('#med_all').prop("checked") == false) && ($('#pack_first_order').prop("checked") == false) && ($('#pack_all').prop("checked") == false) && ($('#pack_homecare').prop("checked") == false) && ($('#pack_hospitalcare').prop("checked") == false)) {
        $('#Poffertype').show();
        $('#Poffertype').fadeOut(8000);
        toastr.warning('Select Any one of the Offer Type');
        return false;
    }
    if ($('#lab_no_test').prop("checked") == true) {
        if ($.trim($('#lab_test_count').val()) == "") {
            $('#Plab_test_count').show();
            $('#Plab_test_count').fadeOut(8000);
            $('#Plab_test_count').html('Enter no of lab test');
            $('#lab_test_count').focus();
            $('#lab_test_count').val('');
            return false;
        }
    }
    if ($.trim($('#coup_tnd').val()) == "") {
        $('#Pcoup_tnd').show();
        $('#Pcoup_tnd').fadeOut(8000);
        $('#Pcoup_tnd').html('Enter terms and conditions');
        $('#coup_tnd').focus();
        $('#coup_tnd').val('');
        return false;
    }
    var start_date = $("#coupon_start_date").val().split('/');
    var s_date = start_date[2] + "-" + start_date[1] + "-" + start_date[0];
    var end_date = $('#coupon_end_date').val().split('/');
    var e_date = end_date[2] + "-" + end_date[1] + "-" + end_date[0];
    var walkin = '0', video = '0', first_consul = '0', con_all = '0', med_all = '0', med_first = '0', lab_all = '0', lab_no_of_test = '0', lab_first = '0', pack_all = '0', pack_first = '0', pack_homecare = '0', pack_hospitalcare = '0';
    if ($('#con_walkin').prop("checked") == true) {
        walkin = 1;
    }
    if ($('#con_video').prop("checked") == true) {
        video = 1;
    }
    if ($('#con_first_consultation').prop("checked") == true) {
        first_consul = 1;
    }
    if ($('#con_all').prop("checked") == true) {
        con_all = 1;
    }
    if ($('#lab_first_order').prop("checked") == true) {
        lab_first = 1;
    }
    if ($('#lab_no_test').prop("checked") == true) {
        lab_no_of_test = 1;
    }
    if ($('#lab_all').prop("checked") == true) {
        lab_all = 1;
    }
    if ($('#med_first_order').prop("checked") == true) {
        med_first = 1;
    }
    if ($('#med_all').prop("checked") == true) {
        med_all = 1;
    }
    if ($('#pack_first_order').prop("checked") == true) {
        pack_first = 1;
    }
    if ($('#pack_all').prop("checked") == true) {
        pack_all = 1;
    }
    if ($('#pack_homecare').prop("checked") == true) {
        pack_homecare = 1;
    }
    if ($('#pack_hospitalcare').prop("checked") == true) {
        pack_hospitalcare = 1;
    }
    var package_data = {
        indicator_id: "I",
        coupon_name: $('#couponname').val(),
        coupon_description: $('#Dcoupondescription').val(),
        coupon_type: $('#coupon_type').val(),
        voucher_max_count: $('#voucher_max_count').val(),
        voucher_per_patient: $('#voucher_no_patient').val(),
        voucher_issue_method: $('#coupon_issue_method').val(),
        voucher_issue_point: $('#coupon_issue_point').val(),
        voucher_expire_days: $('#voucher_expire_days').val(),
        coupon_amount: $('#coupon_discount_amt').val(),
        amount_type: $('#coupon_amt_type').val(),
        no_of_uses: $('#coupon_usage_patient').val(),
        applicable_to: $('#coup_appli_to').val(),
        applicable_mode: $("#coup_appli_mode").val(),
        doctor_id: $('#doctor_list').val(),
        coupon_start_date: s_date,
        coupon_end_date: e_date,
        minimum_order_amount: $('#coupon_min_amt').val(),
        walkin_allowed: walkin,
        video_allowed: video,
        first_con_allowed: first_consul,
        all_appointments: con_all,
        medicine_all: med_all,
        medicine_first: med_first,
        lab_first: lab_first,
        lab_all: lab_all,
        lab_no_of_test: lab_no_of_test,
        lab_no_of_test_count: $('#lab_test_count').val(),
        package_first: pack_first,
        package_all: pack_all,
        package_homecare: pack_homecare,
        package_hospitalcare: pack_hospitalcare,
        ternms_conditions: $('#coup_tnd').val(),
        created_by: $('#HiddenDoctorID').val(),
        branch_id: $('#pkg_branch_id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/coupon_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                var b = data1.Table;
                if (b[0].msg == "0") {
                    toastr.success("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>OK</button>", 'Coupon Already Exists',
                        {
                            closeButton: true,
                            progressBar: true,
                            allowHtml: true,
                            timeOut: 20000000,

                            onShown: function (toast) {
                                $("#confirmationRevertYes").click(function () {
                                    //location.reload();
                                })
                            }
                        })
                }
                else {
                    if ($('#coupon_type').val() == "1") {

                        toastr.success("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>OK</button>", 'Coupon Created Successfully',
                     {
                         closeButton: true,
                         progressBar: true,
                         allowHtml: true,
                         timeOut: 20000000,

                         onShown: function (toast) {
                             $("#confirmationRevertYes").click(function () {
                                 location.reload();
                             })
                         }
                     })
                    }
                    else {
                        
                        toastr.success("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>OK</button>", 'Voucher Created Successfully',
                     {
                         closeButton: true,
                         progressBar: true,
                         allowHtml: true,
                         timeOut: 20000000,

                         onShown: function (toast) {
                             $("#confirmationRevertYes").click(function () {
                                 location.reload();
                             })
                         }
                     })
                    }
                }
            }
            else {
                console.log(data1);
            }
        },
    });
});

function coupon_voucher_view(id) {
    var coupon_voucher_view_data = {
        indicator_id: "CDV",
        coupon_code: id,
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/coupon_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(coupon_voucher_view_data),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {

                    console.log(data);
                    data = data.Table;
                    if (data.length > 0) {
                        $('#btnAddCoup').hide();
                        $('#couponname').val(data[0].coupon_name);
                        $('#Dcoupondescription').val(data[0].coupon_description);
                        $('#coupon_type').val(data[0].coupon_method);
                        $('#coupon_start_date').val(data[0].coupon_start_date);
                        $('#coupon_end_date').val(data[0].coupon_end_date);
                        $('#coupon_discount_amt').val(data[0].coupon_amount);
                        $('#coupon_amt_type').val(data[0].amount_type);
                        $('#coupon_min_amt').val(data[0].minimum_order_amount);
                        $('#coupon_usage_patient').val(data[0].no_of_uses);
                        $('#coup_appli_to').val(data[0].applicable_to);
                        if (data[0].lab_no_of_test != null) $('#lab_test_count').val(data[0].lab_no_of_test);
                        $('#coup_tnd').val(data[0].terms_and_conditions);
                        if (data[0].walkin_allowed == "1") $('#con_walkin').prop("checked", data[0].walkin_allowed);
                        if (data[0].video_allowed == "1") $('#con_video').prop("checked", data[0].video_allowed);
                        if (data[0].first_consultation_allowed == "1") $('#con_first_consultation').prop("checked", data[0].first_consultation_allowed);
                        if (data[0].all_appointments_allowed == "1") $('#con_all').prop("checked", data[0].all_appointments_allowed);

                        if (data[0].lab_first_order_allowed == "1") $('#lab_first_order').prop("checked", data[0].lab_first_order_allowed);
                        if (data[0].lab_no_of_test_allowed == "1") $('#lab_no_test').prop("checked", data[0].lab_no_of_test_allowed);
                        if (data[0].lab_all_order_allowed == "1") $('#lab_all').prop("checked", data[0].lab_all_order_allowed);

                        if (data[0].medicine_all_order_allowed == "1") $('#med_all').prop("checked", data[0].medicine_all_order_allowed);
                        if (data[0].medicine_first_order_allowed == "1") $('#med_first_order').prop("checked", data[0].medicine_first_order_allowed);

                        if (data[0].package_all_order_allowed == "1") $('#pack_all').prop("checked", data[0].package_all_order_allowed);
                        if (data[0].package_first_order_allowed == "1") $('#pack_first_order').prop("checked", data[0].package_first_order_allowed);
                        if (data[0].package_home_care_allowed == "1") $('#pack_homecare').prop("checked", data[0].package_home_care_allowed);
                        if (data[0].package_hospital_care_allowed == "1") $('#pack_hospitalcare').prop("checked", data[0].package_hospital_care_allowed);

                        if (data[0].coupon_method == "2") {
                            $('#voucher_details_div').show();
                            $('#voucher_max_count').val(data[0].max_no_vouchers);
                            $('#voucher_no_patient').val(data[0].voucher_per_patient);
                            $('#coupon_issue_method').val(data[0].voucher_issue_method);
                            $('#coupon_issue_point').val(data[0].voucher_issue_point);
                            $('#voucher_expire_days').val(data[0].no_of_days_expire);
                        }
                    }
                }
            }
            else {
                console.log(data);
            }
        },
    });
}
function check(id) {
    debugger;
    if (id == 1) {
        if ($("#con_first_consultation").prop("checked") == true) {
            $("#con_all").prop("disabled", !this.checked);
        }
        else {
            $("#con_all").prop("disabled", false);
        }
    }
    if (id == 2) {
        if ($("#con_all").prop("checked") == true) {
            $("#con_first_consultation").prop("disabled", !this.checked);
        }
        else {
            $("#con_first_consultation").prop("disabled", false);
        }
    }
    if (id == 3) {
        if ($("#lab_first_order").prop("checked") == true) {
            $("#lab_all").prop("disabled", true);
        }
        else {
            $("#lab_all").prop("disabled", false);
        }
    }
    if (id == 4) {
        if ($("#lab_all").prop("checked") == true) {
            $("#lab_first_order").prop("disabled", true);
        }
        else {
            $("#lab_first_order").prop("disabled", false);
        }
    }
    if (id == 5) {
        if ($("#med_first_order").prop("checked") == true) {
            $("#med_all").prop("disabled", true);
        }
        else {
            $("#med_all").prop("disabled", false);
        }
    }
    if (id == 6) {
        if ($("#med_all").prop("checked") == true) {
            $("#med_first_order").prop("disabled", true);
        }
        else {
            $("#med_first_order").prop("disabled", false);
        }
    }
    if (id == 7) {
        if ($("#pack_first_order").prop("checked") == true) {
            $("#pack_all").prop("disabled", true);
        }
        else {
            $("#pack_all").prop("disabled", false);
        }
    }
    if (id == 8) {
        if ($("#pack_all").prop("checked") == true) {
            $("#pack_first_order").prop("disabled", true);
        }
        else {
            $("#pack_first_order").prop("disabled", false);
        }
    }

}

function difference() {
    debugger;
    var start_date = $("#coupon_start_date").val();
    var end_date = $("#coupon_end_date").val();
    if (Date.parse(start_date) >= Date.parse(end_date)) {
        $('#Pcoupon_end_date').show();
        $('#Pcoupon_end_date').fadeOut(2000);
        $('#Pcoupon_end_date').html('Invalid Date');
        $('#coupon_end_date').focus();
        $("#coupon_end_date").val('');
    }
}
$("#coupon_start_date").change(function () {
    var start_date = $("#coupon_start_date").val();
    var end_date = $("#coupon_end_date").val();
    if (end_date != '' || end_date != null) {
        if (Date.parse(start_date) >= Date.parse(end_date)) {
            $('#Pcoupon_end_date').show();
            $('#Pcoupon_end_date').fadeOut(2000);
            $('#Pcoupon_end_date').html('Invalid Date');
            $('#coupon_end_date').focus();
            $("#coupon_end_date").val('');
        }
    }
})
//Load Branch
function LoadBranch() {
    document.getElementById("pkg_branch_id").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "LBRAN"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    $('#pkg_branch_id').append(default_opt + SubDepartment);
                    if ($('#hiddenRole_id').val() == 1) {
                        $('#pkg_branch_id').val($('#HospitalBranchID').val());
                        $('#pkg_branch_id').attr('disabled', true);
                    }
                    // $('#pkg_branch_id').val(val);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
