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 = {
        residentialId : $('#residential_id').val(),
        type : $('#type').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');
        }
    });

    //监听提交
    form.on('submit(submit_button)', function(data){
        if (data.field.type == 2 && data.field.amount <= 0){
            layer.msg("收费类型为收费时，金额必须大于零")
            return
        }
        var nowDate = new Date().getTime();
        var endDate = data.field.endDate;
        endDate = endDate.replace("-","/");//替换字符，变成标准格式
        endDate = new Date(Date.parse(endDate));
        if (endDate < nowDate){
            layer.msg("结束时间必须大于当前时间")
            return
        }
        if (data.field.type == 2 && data.field.typeItem == 22 && data.field.discount == null){
            data.field.discount = 100;
        }
        common.sendAsyncRequest("/estate/faceChargeProject/addFaceChargeProject",'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之间的整数!'
        ]
    });

    //判断套餐类型
    var typeInit = function () {
        var typeSelect = $('#type_item');
        typeSelect.html('');
        typeSelect.append('<option value = "">请选择</option>');
        if (pageData.type == 1){
            //添加免费类型的选项
            typeSelect.append('<option value = "11">试用</option>');
            typeSelect.append('<option value = "12">长期</option>');
            //金额禁止输入金额
            $('input[name="amount"]').attr('readOnly',true);
        }else if(pageData.type == 2){
            //添加收费类型的选项
            if ($('#canAddToll').val() == "true"){
                typeSelect.append('<option value = "21" >押金</option>');
            }else {
                typeSelect.append('<option value = "21" disabled >押金</option>');
            }
            typeSelect.append('<option value = "22">套餐</option>');
            //开放金额输入
            $('input[name="amount"]').attr('readOnly',false);
        }
        form.render('select');
    }();

    //监听选收费类型选择
    form.on('select(type_item_even)',function (data) {
        if (pageData.type == 2){
            if (data.value == 21){
                $.ajax({
                    url: '/estate/faceChargeProject/checkPutStatus',
                    type: 'GET',
                    async: false, // 同步
                    data: {
                        type    :   pageData.type,
                        residentialId: pageData.residentialId,
                        putStatus:  1,
                    },
                    success: function (result) {
                        if (result.data){
                            pageData.haveDeposit = result.data;
                        }
                    },
                });
                $('input[name="discount"]').val(100);
                $('#discount_div').get(0).style.display = "none";
            }else if (data.value == 22) {
                $('#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);
    });



});