layui.extend({
    common    :   'js/common',
    searchInput: 'layui/lay/mymodules/search-input'
}).use(['jquery','table','layer','form','common','upload','searchInput','laydate'], function () {
    var $ = layui.jquery;
    //  引入表单插件
    var form = layui.form;
    var common = layui.common;
    //  弹窗插件
    var layer = layui.layer;
    //  引入表格插件
    var table = layui.table;
    //  图片上传
    var upload = layui.upload;
    //  日期选择插件
    var laydate = layui.laydate;
    //图片服务器地址
    var fastDfsServer = $('#fastDfsServer').val();

    /********************************  基础信息-- 开始******************************************/
    //项目的是否有保存操作
    //监听提交--保存project项目基础信息
    form.on('submit(insertProject)', function(data){
        common.sendAsyncRequest("/implement/project/edit",'POST',data.field,function (result) {
            if(result.code=='200' && result.data){
                layer.msg("保存成功",{time:1000,shade : [0.1 , '#000' , true]},function () {
                    $(data.elem).attr("style","display:none;");
                    $(data.elem).next().attr("style","display:block;");
                    $(data.form).find('input').attr('disabled',true);
                    $(data.form).find('input').addClass("layui-disabled");
                });
            }else {//添加失败
                layer.msg("保存失败");
            }
        });
    });
    //监听修改按钮--修改project项目基础信息
    form.on('submit(editProject)', function(data){
        $(data.elem).attr("style","display:none;");
        $(data.elem).prev().attr("style","display:block;");
        $(data.form).find('input').attr('disabled',false);
        $(data.form).find('input').removeClass('layui-disabled');
        $(data.form).find('input[id="residentialName"]').attr("disabled",true);
        $(data.form).find('input[id="residentialName"]').addClass("layui-disabled");
        $(data.form).find('input[id="region"]').attr('disabled',true);
        $(data.form).find('input[id="region"]').addClass("layui-disabled");
    });
    /********************************  基础信息-- 结束******************************************/

    /********************************  合同信息-- 开始******************************************/
    //签约日期
    laydate.render({
        elem: '#signingDate'
        ,type: 'datetime'
        ,trigger: 'click'
    });
    // 综合服务方
    layui.searchInput({
        searchInput: $('#integratedServiceName'),
        searchList: $('#select_integratedServiceName'),
        //  获取数据
        getDataFn: function (name, asyncFn) {
            $.ajax({
                type: 'POST',
                url: '/implement/integrated/service/showList',
                data: {
                    name: name,
                    current:1,
                    size:10
                },
                success: function (result) {
                    asyncFn(result.data.records, {
                        id: 'id',
                        name: 'name'
                    });
                }
            });
        },
        //  选中回调
        selectedFn  :   function(data){
            $('input[name="integratedServiceId"]').val(data.id);
            $('#integratedServiceName').val(data.name);
        }
    })

    //监听提交--保存Contract合同信息
    form.on('submit(insertContract)', function(data){
        var imgUri='';
        //获取所有图片地址
        $("input[name='imgUri']").each(function(index,elem){
            if(index==0){
                imgUri = $(elem).val();
            }else {
                imgUri = imgUri+","+$(elem).val();
            }

        })
        //如果不这样做，data中的imgUri默认只会拿第一张图片的值
        data.field.imgUri=imgUri;
        var projectId = $('#projectId').val();
        if(projectId){
            data.field['projectId'] = projectId;
            common.sendAsyncRequest("/implement/contract/editByProjectId",'POST',data.field,function (result) {
                if(result.code=='200' && result.data){
                    layer.msg("保存成功",{time:1000,shade : [0.1 , '#000' , true]},function () {
                        $(data.elem).attr("style","display:none;");
                        $(data.elem).next().attr("style","display:block;");
                        $(data.form).find('input').attr('disabled',true);
                        $(data.form).find('input').addClass("layui-disabled");
                        $(data.form).find('select').attr('disabled',true);
                        $(data.form).find('select').addClass("layui-disabled");
                        $(data.form).find('textarea').attr('disabled',true);
                        $(data.form).find('textarea').addClass("layui-disabled");
                        $(data.form).find('.layui-laypage-next').hide();
                        $(data.form).find('.layui-laypage-prev').hide();
                        $(data.form).find('.layui-layer-close').hide();
                        $("#uploadImage").hide();
                        form.render('radio');
                        form.render('select');
                    });
                }else {//添加失败
                    layer.msg("保存失败");
                }
            });
        }
    });
    //监听修改按钮--修改Contract合同信息
    form.on('submit(editContract)', function(data){
        $(data.elem).attr("style","display:none;");
        $(data.elem).prev().attr("style","display:block;");
        $(data.form).find('input').attr('disabled',false);
        $(data.form).find('input').removeClass('layui-disabled');
        $(data.form).find('select').attr('disabled',false);
        $(data.form).find('select').removeClass('layui-disabled');
        $(data.form).find('textarea').attr('disabled',false);
        $(data.form).find('textarea').removeClass('layui-disabled');
        $(data.form).find('.layui-laypage-next').show()
        $(data.form).find('.layui-laypage-prev').show()
        $(data.form).find('.layui-layer-close').show();
        $("#uploadImage").show();
        form.render('radio');
        form.render('select');
        //添加lay-verify="required"
        $(data.form).find('input[type="text"]').attr('lay-verify',"required");
        $(data.form).find('input[type="number"]').attr('lay-verify',"required");
    });

    //定义加载层id
    var loadId;
    //多图片上传
    upload.render({
        elem: '#uploadImage'
        ,url: '/common/file/upload'
        ,accept: 'images' //图片
        ,exts: 'jpg|png|jpeg'
        ,multiple: true
        ,before: function(obj){
            //开启加载动画
            loadId = layer.msg('图片上传中', {
                icon: 16,
                shade: 0.4,
                time:false //取消自动关闭
            });
        }
        ,done: function(res){  //上传完毕
            //关闭加载动画
            layer.close(loadId);
            if(res.code=="200"){//上传成功
                $('#previewImage').append('' +
                    '<div class="layer-photos-content">' +
                    '<img class="layui-upload-img" src="'+fastDfsServer+ res.data.fullPath +'">' +
                    '<input type="hidden" name="imgUri" value="'+res.data.fullPath+'" />' +
                    '<span class="layui-layer-setwin"><a class="layui-layer-ico layui-layer-close layui-layer-close2" href="javascript:;"></a></span>' +
                    '<a href="javascript:;" class="layui-laypage-next"><i class="layui-icon"></i></a>' +
                    '<a href="javascript:;" class="layui-laypage-prev "><i class="layui-icon"></i></a>' +
                    '</div>'
                );
            }else{
                layer.msg("图片上传失败");
            }
        }
        ,error: function(){
            //关闭加载动画
            layer.close(loadId);
            layer.msg("图片上传失败");
        }
    });
    //图片事件
    //移动-右
    $(document).on('click','.layui-laypage-next',function () {
        var html = $(this).parent().next();
        $(html).insertBefore($(this).parent());
    });
    //移动-左
    $(document).on('click','.layui-laypage-prev',function () {
        var html = $(this).parent();
        $(html).insertBefore($(this).parent().prev());
    });
    //点击删除
    $(document).on('click','.layui-layer-close',function () {
        $(this).parents(".layer-photos-content").remove();
    })
    /********************************  合同信息-- 结束******************************************/

    /********************************  立项书项目时间-- 开始******************************************/
    //监听提交--保存ProjectBook立项书项目时间
    form.on('submit(insertProjectBook)', function(data){
        var projectId = $("#projectId").val();
        if(projectId){
            //项目id
            data.field['projectId'] = projectId;
            //时间范围处理
            var rectificationDate=$("#rectificationDate").val();
            //项目整改时间
            if(rectificationDate){
                var rectificationDateArray=rectificationDate.split(" - ");
                var beginRectificationDate=rectificationDateArray[0];
                data.field.beginRectificationDate=beginRectificationDate;
                var endRectificationDate=rectificationDateArray[1];
                data.field.endRectificationDate=endRectificationDate;
            }
            var productionDate=$("#productionDate").val();
            //设备生产时间
            if(productionDate){
                var productionDateArray=productionDate.split(" - ");
                var beginProductionDate=productionDateArray[0];
                data.field.beginProductionDate=beginProductionDate;
                var endProductionDate=productionDateArray[1];
                data.field.endProductionDate=endProductionDate;
            }
            common.sendAsyncRequest("/implement/projectBook/editByProjectId",'POST',data.field,function (result) {
                if(result.code=='200' && result.data){
                    layer.msg("保存成功",{time:1000,shade : [0.1 , '#000' , true]},function () {
                        $(data.elem).attr("style","display:none;");
                        $(data.elem).next().attr("style","display:block;");
                        $(data.form).find('input').attr('disabled',true);
                        $(data.form).find('input').addClass("layui-disabled");
                    });
                }else {//添加失败
                    layer.msg("保存失败");
                }
            });
        }
    });
    //监听修改按钮--修改ProjectBook立项书项目时间
    form.on('submit(editProjectBook)', function(data){
        $(data.elem).attr("style","display:none;");
        $(data.elem).prev().attr("style","display:block;");
        $(data.form).find('input').attr('disabled',false);
        $(data.form).find('input').removeClass('layui-disabled');
    });
    //项目整改时间
    laydate.render({
        elem: '#rectificationDate'
        ,type: 'month'
        ,range: true
        ,trigger: 'click'
    });
    //设备生产时间
    laydate.render({
        elem: '#productionDate'
        ,type: 'month'
        ,range: true
        ,trigger: 'click'
    });
    //施工日期
    laydate.render({
        elem: '#constructionDate'
        ,type: 'month'
        ,trigger: 'click'
    });
    //项目验收和交接日期
    laydate.render({
        elem: '#acceptanceDate'
        ,type: 'month'
        ,trigger: 'click'
    });
    //设备出入库测试及物流发货日期
    laydate.render({
        elem: '#deliveryDate'
        ,type: 'month'
        ,trigger: 'click'
    });
    /********************************  立项书项目时间-- 结束******************************************/

    //回调函数
    function callbackFn(result) {
        if(result.code=='200'){
            layer.msg("保存成功",{time:1000,shade : [0.1 , '#000' , true]},function () {
            });
        }else {//添加失败
            layer.msg("添加失败");
        }
    }
});


