layui.use(['layer', 'form','laydate','util', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var laydate = layui.laydate;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--消息管理
     */
    var MilitaryMedicalHis = {
        tableId: "militaryTable",    //表格id
        condition: {
            name: "",
            timeLimit: ""
        }
    };

    /**
     * 初始化表格的列
     */
    MilitaryMedicalHis.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'name', sort: true, title: '姓名'},
            {field: 'militaryId', sort: true, title: '军官证'},
            {field: 'sexName', sort: true, title: '性别',width:80},
            {field: 'birthday', sort: true, title: '出生日期',
                templet:'<div>{{ layui.util.toDateString(d.birthday, "yyyy-MM-dd") }}</div>'
            },
            {field: 'isSurgeryName', sort: true, title: '是否手术 '},
            {field: 'medicalHistory', sort: true, title: '既往慢性病史'},
            {field: 'otherInfo', sort: true, title: '其它'},
           /*             {field: 'createUserName', sort: true, title: '创建人'},
                        {field: 'createTime', sort: true, title: '创建时间',
                            templet:'<div>{{ layui.util.toDateString(d.birthday, "yyyy-MM-dd") }}</div>'},*/
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 400}
        ]];
    };

    /**
     * 点击查询按钮
     */
    MilitaryMedicalHis.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        queryData['militaryId'] = $("#militaryId").val();
        table.reload(MilitaryMedicalHis.tableId, {where: queryData});
    };

    /**
     * 弹出添加
     */
    MilitaryMedicalHis.openAddMmhc = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加军人',
            content: Feng.ctxPath + '/mmhc/mmhc_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(MilitaryMedicalHis.tableId);
            }
        });
    };

    MilitaryMedicalHis.openAddCollect = function(data){
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '采集信息',
            content: Feng.ctxPath + '/collect/collect_add?militaryId='+data.militaryId,
            end: function () {
                admin.getTempData('formOk') && table.reload(MilitaryMedicalHis.tableId);
            }
        });
    };

    MilitaryMedicalHis.openHistory = function(data){
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '病况历史',
            area: ['90%', '80%'],
            content: Feng.ctxPath + '/index/history_obxList?id='+data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(MilitaryMedicalHis.tableId);
            }
        });
    };

    /**
     * 点击编辑通知
     *
     * @param data 点击按钮时候的行数据
     */
    MilitaryMedicalHis.onEditMmhc = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '军人信息详情',
            content: Feng.ctxPath + '/mmhc/mmhc_update/'+data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(MilitaryMedicalHis.tableId);
            }
        });
    };
    /**
     * 导出excel按钮
     */
    MilitaryMedicalHis.exportExcel = function () {
        var checkRows = table.checkStatus(MilitaryMedicalHis.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
    /**
     * 弹出Excel导入
     */
    MilitaryMedicalHis.openImpMmhc = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '导入军人信息',
            content: Feng.ctxPath + '/mmhc/mmhc_import',
            end: function () {
                admin.getTempData('formOk') && table.reload(MilitaryMedicalHis.tableId);
            }
        });
    };

    MilitaryMedicalHis.openImpNs = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '导入脑损害评估',
            content: Feng.ctxPath + '/mmhc/nsh_import',
            end: function () {
                admin.getTempData('formOk') && table.reload(MilitaryMedicalHis.tableId);
            }
        });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    MilitaryMedicalHis.onDeleteMmhc = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/mmhc/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(MilitaryMedicalHis.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除军人信息 ?", operation);
    };
    /**
     * 点击编辑用户按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    MilitaryMedicalHis.onEditMmh = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑用户',
            content: Feng.ctxPath + '/mmhc/mmhc_update?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(MilitaryMedicalHis.tableId);
            }
        });
    };
    /**
     * 查看改军人的所有医疗信息
     * @param data
     */
    MilitaryMedicalHis.onAllDetailMmh = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '病情明细',
            shade:0,
            //offset: 't',
            area: ['100%', '100%'],
            content: Feng.ctxPath + '/mmhc/mmhc_obx?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(MilitaryMedicalHis.tableId);
            }
        });
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + MilitaryMedicalHis.tableId,
        url: Feng.ctxPath + '/mmhc/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: MilitaryMedicalHis.initColumn()
    });
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        MilitaryMedicalHis.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        MilitaryMedicalHis.openAddMmhc();
    });


    // 导出excel
    $('#btnExp').click(function () {
        MilitaryMedicalHis.exportExcel();
    });

    // 导出病况数据excel
    $('#btnIndex').click(function () {
        //location.href =  '/index/exportTechParse';
        var ajax = new $ax(Feng.ctxPath + "/index/exportTechParse");
        var result = ajax.start();
        console.log(result.data);
        window.location.href = result.data
    });

    // 导出病况数据excel
    $('#btnISl').click(function () {
        //location.href =  '/index/exportTechParse';
        var ajax = new $ax(Feng.ctxPath + "/index/exportphysiology");
        var result = ajax.start();
        console.log(result.data);
        window.location.href = result.data
    });

    // 导入excel
    $('#btnImp').click(function () {
        MilitaryMedicalHis.openImpMmhc();
    });

    $('#btnNs').click(function () {
        MilitaryMedicalHis.openImpNs();
    });

    // 工具条点击事件
    table.on('tool(' + MilitaryMedicalHis.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            MilitaryMedicalHis.onEditMmh(data);
        } else if (layEvent === 'delete') {
            MilitaryMedicalHis.onDeleteMmhc(data);
        }else if (layEvent === 'mAllDetail') {
            MilitaryMedicalHis.onAllDetailMmh(data);
        }else if (layEvent === 'collect') {
            MilitaryMedicalHis.openAddCollect(data);
        }else if (layEvent === 'history') {
            MilitaryMedicalHis.openHistory(data);
        }
    });
});
