layui.extend({
    common    :   'js/common'
}).use(['jquery','layer','form','common','laydate'], function () {
    var $ = layui.jquery;
    //  引入表单插件
    var form = layui.form;
    var common = layui.common;
    //  弹窗插件
    var layer = layui.layer;
    //  日期选择插件
    var laydate = layui.laydate;


    //监听提交
    form.on('submit(insertConfirm)', function(data){
        common.sendAsyncRequest("/implement/projectCompletion/insert",'POST',data.field,callbackFn);
    });

    //竣工日期
    laydate.render({
        elem: '#completionDate'
        ,type: 'datetime'
    });

    //回调函数
    function callbackFn(result) {
        if(result.code=='200'){
            layer.msg("添加成功",{time:1000,shade : [0.1 , '#000' , true]},function () {
                // 父页面刷新
                parent.location.reload();
                //获取当前弹窗出层的名字
                var mylay = parent.layer.getFrameIndex(window.name);
                //关闭弹窗层
                parent.layer.close(mylay);
            });
        }else {//添加失败
            layer.msg("添加失败");
        }
    }
    //监听input的值变化
    $("#pillar").bind("input propertychange",function(){
        var pillar = $("#pillar").val();
        var wallHung = $("#wallHung").val();
        var rainCover = $("#rainCover").val();
        if(!wallHung){
            wallHung = 0;
        }
        if(!rainCover){
            rainCover = 0;
        }
        $('#totalNumber').val(parseInt(pillar)+parseInt(wallHung)+parseInt(rainCover));
    })
    $("#wallHung").bind("input propertychange",function(){
        var pillar = $("#pillar").val();
        var wallHung = $("#wallHung").val();
        var rainCover = $("#rainCover").val();
        if(!pillar){
            pillar = 0;
        }
        if(!rainCover){
            rainCover = 0;
        }
        $('#totalNumber').val(parseInt(pillar)+parseInt(wallHung)+parseInt(rainCover));
    });
    $("#rainCover").bind("input propertychange",function(){
        var pillar = $("#pillar").val();
        var wallHung = $("#wallHung").val();
        var rainCover = $("#rainCover").val();
        if(!pillar){
            pillar = 0;
        }
        if(!wallHung){
            wallHung = 0;
        }
        $('#totalNumber').val(parseInt(pillar)+parseInt(wallHung)+parseInt(rainCover));
    });

    //取消按钮
    $(document).on("click",".layui-layer-btn1",function(){
        //获取当前弹窗出层的名字
        var mylay = parent.layer.getFrameIndex(window.name);
        //关闭弹窗层
        parent.layer.close(mylay);
    });
});

