layui.use(['table', 'layer', 'laydate', 'jquery', 'form'], function () {
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    laydate.render({
        elem: '#reservationTime' //指定元素  元素选择器
        , type: 'date'  //选择时间类型 可选值:year(年) month(年月)  date(年月日)  time(时分秒)  datetime(年月日时分秒)
        , trigger: 'click'
        , format: 'yyyy-MM-dd'  //时间格式  常用时间格式:yyyy-MM-dd HH:mm:ss
        , btns: ['clear', 'now', 'confirm'] //选择框右下角显示的按钮 清除-现在-确定
        , done: function (value, date) {//时间回调
            console.log(value);
            console.log(date);
        }
    });
    getHospitalSelect();
    $('input[name=checkType][value="1"]').prop('checked',true);
    form.render()
    form.on('radio(checkType)', function (data) {
        var values = data.value;
        getTcManSelect(values);
        getTcWomanSelect(values);
    });
    getTcManSelect("1");
    getTcWomanSelect("1");

    form.on('submit(formDemo)', function (data) {
        addReservationData()
        return false;
    });
});

/**
 * 批量添加人员预约
 */
function addReservationData() {
    //医院select选择
    var hospitalVal = $("#hospital").val();

    //男套餐select选择
    var tcMan = $("#tcMan").val();
    //女套餐select选择
    var tcWoman = $("#tcWoman").val();

    //父页面传人员id字符串
    var personId = $("#personId").val();
    //男人数
    var manNum = $("#manNum").val();
    //女人数
    var womenNum = $("#womenNum").val();

    if(manNum >0 && tcMan==0){
        layer.msg("请选择男性套餐！");
        return false;
    }
    if(womenNum >0 && tcWoman==0){
        layer.msg("请选择女性套餐");
        return false;
    }
    if(hospitalVal == 0){
        layer.msg("请选择体检医院");
        return false;
    }
    console.log("hospitalVal="+hospitalVal+" ,tcMan="+tcMan+" ,tcWoman="+tcWoman);
    console.log("personId="+personId+" ,manNum="+manNum+" ,womenNum="+womenNum);
    console.log("tcManName="+$("#tcMan").text().replace("请选择套餐","")+" ,tcWomanName="+$("#tcWoman").text().replace("请选择套餐",""));

    $.ajax({
        type: 'post',
        url: ctxPath + '/retirement/addReservationData',
        dataType: 'json', // 服务器返回数据类型
        data: {
            id:hospitalVal,
            tcManId:tcMan,
            tcWomanId:tcWoman,
            personStr:personId,
            reservationTime:$("#reservationTime").val(),
            tcManName:$("#tcMan").text().replace("请选择套餐",""),
            tcWomanName:$("#tcWoman").text().replace("请选择套餐","")

        },
        async: false,
        success: function (data) {
            debugger;
            var count = data.count ;
            if(count ==1){
                layer.msg("预约成功！", {icon: 1});
                setTimeout(function () {
                    parent.layer.closeAll();
                },1500)
            }else{
                layer.msg("预约失败！", {icon: 2});
            }
        },
        error: function (err) {
            console.log("获取工程下拉列表出错：", err);
        }
    });


}

function getHospitalSelect() {
    $.ajax({
        type: 'post',
        url: ctxPath + '/utilConnection/getHospitalSelect',
        dataType: 'json', // 服务器返回数据类型
        data: {
        },
        async: false,
        success: function (data) {
            $("#hospital").empty();
            var data = data.data;
            var html = '<option value="0">请选择医院</option>';
            for (var i = 0; i < data.length; i++) {
                html += '<option value=\'' + data[i].key + '\'>' + data[i].value + '</option>';
            }
            $("#hospital").html(html);
            layui.form.render('select');  //这里就是我们要渲染的地方了
        },
        error: function (err) {
            console.log("获取工程下拉列表出错：", err);
        }
    });
}


function getTcManSelect(values) {
    $.ajax({
        type: 'post',
        url: ctxPath + '/utilConnection/getSetMeal',
        dataType: 'json', // 服务器返回数据类型
        data: {
            'type':'0',
            'keyWord':'2',
            'checkId':values,
            'higherJob':$("#higherJob").val()
        },
        async: false,
        success: function (data) {
            $("#tcMan").empty();
            var data = data.data;
            var html = '<option value="0">请选择套餐</option>';
            for (var i = 0; i < data.length; i++) {
                html += '<option value=\'' + data[i].key + '\'>' + data[i].value + '</option>';
            }
            $("#tcMan").html(html);
            layui.form.render('select');  //这里就是我们要渲染的地方了
        },
        error: function (err) {
            console.log("获取工程下拉列表出错：", err);
        }
    });

};

function getTcWomanSelect(values) {
    $.ajax({
        type: 'post',
        url: ctxPath + '/utilConnection/getSetMeal',
        dataType: 'json', // 服务器返回数据类型
        data: {
            'type':'1',
            'keyWord':'2',
            'checkId':values,
            'higherJob':$("#higherJob").val()
        },
        async: false,
        success: function (data) {
            $("#tcWoman").empty();
            var data = data.data;
            var html = '<option value="0">请选择套餐</option>';
            for (var i = 0; i < data.length; i++) {
                html += '<option value=\'' + data[i].key + '\'>' + data[i].value + '</option>';
            }
            $("#tcWoman").html(html);
            layui.form.render('select');  //这里就是我们要渲染的地方了
        },
        error: function (err) {
            console.log("获取工程下拉列表出错：", err);
        }
    });


}