layui.extend({
    common    :   'js/common'
}).use(['jquery','layer','common','form','laydate'],function () {
    //  Jquery插件
    var $ = layui.jquery;
    //  弹窗插件
    var layer = layui.layer;
    //  引入表单插件
    var form = layui.form;
    //  引入公共模块
    var common = layui.common;
    //  引入时间组件
    var laydate = layui.laydate;

    //  页面数据
    var pageData = {
        id : $('#id').val(),
        type : '',
        typeItem : '',
        residentialId : $('#residential_id').val(),

        //是否拥有收费类型为押金的套餐
        haveDeposit : false,
    };

    laydate.render({
        elem: '#pro_date'
        ,type: 'date'
        ,range: true
        ,trigger: 'click'
        ,min : 0
        ,done: function(value, date, endDate){
            var dateArray = value.split(" - ");
            $("#begin_date").val(dateArray[0] + ' 00:00:00');
            $("#end_date").val(dateArray[1] + ' 23:59:59');
        }
    });

    //初始化数据

    var faceChargeProjectInit = function(){
        common.sendSyncRequest('/estate/faceChargeProject/getOneFaceChargeProject?id=' + pageData.id,'get',{},function(result){
            var data = result.data;
            //  数据渲染
            if(data){
                //套餐名称
                $('input[name="name"]').val(data.name);
                pageData.type = data.type;
                //收费类型
                var typeSelect = $('#type_item');
                typeSelect.html('');
                typeSelect.append('<option value = "">请选择</option>')
                if (data.type == 1){
                    $('input[name="amount"]').attr('readOnly',true);
                    typeSelect.append('<option value = 11>试用</option>');
                    typeSelect.append('<option value = 12>长期</option>');
                }else if (data.type == 2){
                    $('input[name="amount"]').attr('readOnly',false);
                    typeSelect.append('<option value = 21>押金</option>');
                    typeSelect.append('<option value = 22>套餐</option>');
                }
                $('select[name="typeItem"]').val(data.typeItem);
                //折扣
                if(data.type == 2 && data.typeItem == 22){
                    $('#discount_div').get(0).style.display = "block";
                }
                $('input[name="discount"]').val(data.discount);
                //金额
                $('input[name="amount"]').val(data.amount);
                //天数
                $('input[name="days"]').val(data.days);
                //套餐有效期
                if (data.beginDate != null && data.endDate != null){
                    $('#pro_date').val(data.beginDate.split(' ')[0] + ' - ' + data.endDate.split(' ')[0]);
                }else {
                    $('#pro_date').val('');
                }
                //上架状态
                // $('#putStatus').val(data.putStatus);
                layui.form.render('select');
            }
        });
    }();


    //监听提交
    form.on('submit(submit_button)', function(data){
        if (data.field.type == 2 && data.field.typeItem == 22 && data.field.discount == null){
            data.field.discount = 100;
        }
        common.sendAsyncRequest("/estate/faceChargeProject/updateFaceChargeProject",'POST',data.field,callbackFn);
    });

    //  表单校验
    layui.form.verify({
        haveDeposit: function(value){
            if(value == 21 && pageData.havaDeposit){
                return "已有一个收费为押金的套餐!";
            }
        },
        nLength : function (value) {
            if(value.length < 3 || value.length > 20){
                return "请输入3-20个字符!"
            }
        },
        amount : [
            /((^[1-9]\d*)|^0)(\.\d{1,2}){0,1}$/,
            '只能输入金额!'
        ],
        dayNum : [
            /^[0-9]*[1-9][0-9]*$/,
            '只能输入数字!'
        ],
        discount : [
            /^([1-9]\d?|100)$/,
            '请输入1-100之间的整数!'
        ]
    });

    //监听选收费类型选择
    form.on('select(type_item_even)',function (data) {
        if (pageData.type == 2){
            if (data.value == 21){
                var dto  = {
                    type : pageData.type,
                    typeItem : data.value,
                    residentialId : pageData.residentialId
                };
                common.sendAsyncRequest('/estate/faceChargeProject/getOneFaceChargeProject','GET',dto,function (result) {
                    if (result.data != null){
                        pageData.havaDeposit = true;
                    }
                });
                $('input[name="discount"]').val(100);
                $('#discount_div').get(0).style.display = "none";
            }else {
                $('#discount_div').get(0).style.display = "block"
            }
        }
    });

    //回调函数
    function callbackFn(result) {
        if(result.data){
            layer.msg("修改成功",{
                time: 500
                ,shade : [0.1 , '#000' , true]
            },function () {
                //获取当前弹窗出层的名字
                var index = parent.layer.getFrameIndex(window.name);
                //关闭弹窗层
                parent.layer.close(index);
                // 父页面刷新
                parent.location.reload();
            });
        }else {//添加失败
            layer.msg("修改失败");
        }
    }

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



});