AjaxUtil.ctx = /*[[@{/}]]*/'';
layui.use(['form', 'layer','table','tableSelect'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        laydate = layui.laydate,
        tableSelect= layui.tableSelect;
    // 日期
    laydate.render({
        elem: '#beginTime',
        value: new Date(),
        done:function (value,date,endDate) {
            setTimeout(function () {
                    let qs = $('#beginTime').val();
                    let wc = $('#comeTime').val();
                    // 格式转换
                    let qss = new Date(qs);
                    let wcc = new Date(wc);
                    let timeDiff = wcc.getTime() - qss.getTime();
                    let oneMonthsInMilid = 1*30*24*60*60*1000;
                    let zq = timeDiff /(1000*60*60*24);
                    if (wc < qs){
                        layer.msg('完成时间不能早于项目开始时间！',{icon:2},function () {
                            $('#comeTime').val('');
                            $('#xmzq').val('0天');
                            return false;

                        });
                    }else if (qs == wc){
                        layer.msg('开始时间和完成时间不能是同一天！',{icon:2},function () {
                            $('#comeTime').val('')
                            $('#xmzq').val('0天');
                            return false;1
                        });

                    }
                    if (zq != null){
                        $('#xmzq').val(zq+'天');
                        return false;
                    }
                    $('#xmzq').val('天');
                    return false;
                }
                ,1000);

        }
    });
    laydate.render({
        elem: '#beginTimes',
        done:function (value,date,endDate) {
            setTimeout(function () {
                    let qs = $('#beginTimes').val();
                    let wc = $('#comeTimes').val();
                    // 格式转换
                    let qss = new Date(qs);
                    let wcc = new Date(wc);
                    let timeDiff = wcc.getTime() - qss.getTime();
                    let oneMonthsInMilid = 1*30*24*60*60*1000;
                    let zq = timeDiff /(1000*60*60*24);
                    if (wc < qs){
                        layer.msg('完成时间不能早于项目开始时间！',{icon:2},function () {
                            $('#comeTimes').val('');
                            $('#xmzq').val('0天');
                            return false;

                        });
                    }else if (qs == wc){
                        layer.msg('开始时间和完成时间不能是同一天！',{icon:2},function () {
                            $('#comeTimes').val('')
                            $('#xmzq').val('0天');
                            return false;
                        });

                    }
                    if (zq != null){
                        $('#xmzq').val(zq+'天');
                        return false;
                    }
                    $('#xmzq').val('天');
                    return false;
                }
                ,1000);

        }
    });
    laydate.render({
        elem:'#comeTimes',
        done:function (value,date,endDate) {

            let qs = $('#beginTimes').val();
            let wc = $('#comeTimes').val();
            let qss = new Date(qs);
            let wcc = new Date(wc);
            let timeDiff = wcc.getTime() - qss.getTime();
            let oneMonthsInMilid = 1*30*24*60*60*1000;
            let zq = timeDiff /(1000*60*60*24);
            if (wc < qs){
                layer.msg('完成时间不能早于项目开始时间！',{icon:2});
                $('#beginTimes').val('')

            }else if (qs == wc){
                layer.msg('开始时间和完成时间不能是同一天！',{icon:2},function () {
                    $('#beginTimes').val('')

                });
            }
            if (zq != null){
                $('#xmzq').val(zq+'天');
                return false;
            }
            $('#xmzq').val('天');
            return false;

        }
    });
    laydate.render({
        elem:'#comeTime',
        done:function (value,date,endDate) {

            let qs = $('#beginTime').val();
            let wc = $('#comeTime').val();
            let qss = new Date(qs);
            let wcc = new Date(wc);
            let timeDiff = wcc.getTime() - qss.getTime();
            let oneMonthsInMilid = 1*30*24*60*60*1000;
            let zq = timeDiff /(1000*60*60*24);
            if (wc < qs){
                layer.msg('完成时间不能早于项目开始时间！',{icon:2});
                $('#beginTime').val('')

            }else if (qs == wc){
                layer.msg('开始时间和完成时间不能是同一天！',{icon:2},function () {
                    $('#beginTime').val('')

                });
            }
            if (zq != null){
                $('#xmzq').val(zq+'天');
                return false;
            }
            $('#xmzq').val('天');
            return false;

        }
    });
    // 项目负责人
    tableSelect.render({
        elem: '#leaderName',
        checkedKey: 'userId',
        searchKey: 'name',
        searchPlaceholder:'搜索姓名',
        table:{
            url: AjaxUtil.ctx + '/userinfo/rylist',
            cols:[
                [
                    {type:'radio',title:'😊',fixed:'left'}
                    ,{type:'numbers',align:'center',title: '序号',}
                    ,{field:'name',align: 'center',title: '姓名'}
                    ,{field:'deptName',align: 'center',title: '所属部门'}

                ]
            ]
        },
        done:function (elem,data) {
            var NEWJSON =[];
            var NEWJSON1= [];
            layui.each(data.data,function (index,item) {
                NEWJSON.push(item.name);
                NEWJSON1.push(item.userId);
            });
            elem.val(NEWJSON.join(','));
            $('#leader').val(NEWJSON1.join(','));
        }
    });

    // 监听确认按钮
    form.on('submit(saveBtn)', function (data) {
        var formData = {
            title: $('#title').val(),
            descripTion: $('#descripTion').val(),
            xmid: $('#xmid').val(),
            workCode: $('#workCode').val(),
            leader: $('#leader').val(),
            leaderName: $('#leaderName').val(),
            beginTime: $('#beginTime').val(),
            comeTime: $('#comeTime').val(),
            priorityCode: $('#priorityCode').val(),
            riskCode: $('#riskCode').val(),
            reCode: $('#reCode').val(),
            demandCode: $('#demandCode').val(),
            xmzq: $('#xmzq').val(),
            remark: $('#remark').val()
        };

        var index = layer.alert('确认提交吗？', function () {
            var loading = layer.load(0, {shade: 0.1});
            AjaxUtil.post({
                url: AjaxUtil.ctx + "/managerment/adds",
                data: formData,
                success: function (res) {
                    layer.close(index);
                    layer.close(loading);
                    if (res.code === 0) {
                        layer.msg(res.message,{icon:1}, function () {
                            // 重载表格
                            parent.setUpdateFlag();
                            // 关闭弹框
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    } else {
                        layer.msg(res.message, {icon:2});
                    }
                },
                error: function (error) {
                    layer.msg(res.message, {icon:2});
                }
            });
        });
        return false;
    });

var xmid = $('#xmid').val();
    var currTable = table.render({
        elem: '#currentTableId',
        url: AjaxUtil.ctx + '/bug/progresslist/' + xmid,
        method: 'post',
        /* toolbar: '#toolbarDemo',*/
        // defaultToolbar: ['filter', 'exports', 'print'],
        cols: [
            [
                {type: 'numbers', align: 'center',title: '排序'},
                {field: 'progress', width: '44%', title: '进度描述'},
                {field: 'createUser', width: '10%', title: '发布人', align: 'center'},
                {field: 'createTime', width: '25%', title: '发布时间', align: 'center'},
                {title: '操作', width: '13%', toolbar: '#currentTableBar', align: 'center'}
            ]
        ],
        sortName: 'jtsj',
        autoSort: false,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        page: true,
        skin: 'grid',
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.message, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.rows //解析数据列表
            };
        }
    });


    // 监听确认按钮
    form.on('submit(saveBtns)', function (data) {
        var formData = {
            id:$('#id').val(),
            title: $('#title').val(),
            descripTion: $('#descripTion').val(),
            xmid: $('#xmid').val(),
            workCode: $('#workCode').val(),
            leader: $('#leader').val(),
            leaderName: $('#leaderName').val(),
            beginTime: $('#beginTimes').val(),
            comeTime: $('#comeTimes').val(),
            priorityCode: $('#priorityCode').val(),
            riskCode: $('#riskCode').val(),
            reCode: $('#reCode').val(),
            demandCode: $('#demandCode').val(),
            xmzq: $('#xmzq').val(),
            remark: $('#remark').val()
        };

        var index = layer.alert('确认提交吗？', function () {
            var loading = layer.load(0, {shade: 0.1});
            AjaxUtil.post({
                url: AjaxUtil.ctx + "/managerment/updates",
                data: formData,
                success: function (res) {
                    layer.close(index);
                    layer.close(loading);
                    if (res.code === 0) {
                        layer.msg(res.message,{icon:1}, function () {
                            // 重载表格
                            parent.setUpdateFlag();
                            // 关闭弹框
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    } else {
                        layer.msg(res.message, {icon:2});
                    }
                },
                error: function (error) {
                    layer.msg(res.message, {icon:2});
                }
            });
        });
        return false;
    });

    // 监听取消按钮
    $("button[lay-filter='cancleBtn']").click(function () {
        var iframeIndex = parent.layer.getFrameIndex(window.name);
        parent.layer.close(iframeIndex);
    });

});
