"use strict";
layui.extend({
    common    :   'js/common',
    xmSelect    :   'layui/dist/xm-select',
}).use(['jquery','form','common','laydate','layer','xmSelect'],function(){
    var $ = layui.jquery;
    //  引入表单插件
    var form = layui.form;

    var common = layui.common;

    var laydate = layui.laydate;

    var layer = layui.layer;

    var xmSelect = layui.xmSelect;

    //  页面元素
    var $node = {
        //  取消按钮
        resetBtn        :   $('button[type="reset"]'),
        extensionBtn : $('button[lay-filter="extension"]'),
        idInput:$('input[name="id"]'),
        operator    :   $('#operatorValue').val(),
        selectOrg : $('#select_org'),

        //原始结束时间
        initialTime : $('#end_date').val()
    };

    //  表单校验
    layui.form.verify({
        // Numbers : [/(^$)|^\d+$/,'只能填写数字'],
    });

    //日期时间范围
    laydate.render({
        elem: '#card_date'
        ,type: 'datetime'
        ,range: true
        ,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');
        }
    });
    $.ajax({
        type: 'GET',
        url: '/permissions/dict/getByCode/operator',
        success: function (result) {
            operatorDict.update({data:result.list}).setValue([$node.operator])
        },
    });
    var operatorDict = xmSelect.render({
        el: '#operator',
        name: 'operator',
        layVerify: 'required',
        radio: true,
        clickClose: true,
        prop: {
            name: 'dictValue',
            value: 'dictKey',
        },
        on: function (data) {},
        model: {
            label: {
                type: 'text',
                text: {
                    left: '',
                    right: '',
                    separator: ', ',
                },
            }
        },
    });

    //  绑定表单提交事件
    layui.form.on("submit(entity_submit_event)",function(data){
        var $residentialCheck=$('#residential_id :selected');
        data.field.residentialId = $residentialCheck.val();
        $.ajax({
            url     :   '/device/flowCard/edit',
            data    :   data.field,
            type    :   'POST',
            success :   function () {
                $node.resetBtn.click();
                parent.location.reload();
            }
        });
    });

    //  点击取消事件
    $node.resetBtn.on('click',function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    //延期按钮
    form.on('submit(extension_button)',function (data) {
        layer.open({
            title: '选择日期',
            type: 1,
            icon: 3,
            trigger: 'click',
            area: ['400px', '200px'],
            offset: '100px',
            content: '' +
                '<div class="layui-inline">' +
                '      <div class="layui-input-inline">' +
                '        <input type="text" class="layui-input" name="extension" id="extension" ' +
                'style="width: 300px;left: 50px;padding:0px;font-size: 18px;position: absolute;top:30px;text-align: center;"' +
                'placeholder="请选择延长日期和时间">' +
                '      </div>' +
                '    </div>',
            btn:['确定','取消'],
            success:function(layero, index) {
                laydate.render({
                    elem:"#extension",
                    type: 'date',
                    min: $node.initialTime
                }) ;
            },
            yes: function(index) {
                let extensionDate = $("input[name='extension']").val();
                if (extensionDate == "" || extensionDate == "undefined" || extensionDate == null) {
                    layer.close(index);
                } else {
                    $('#end_date').val(extensionDate + ' 23:59:59');
                    $('#card_date').val($("#begin_date").val() + ' - ' + $('#end_date').val());
                    layer.close(index);
                    layer.msg("有效期-结束日期已设置至" + extensionDate + '</br>点击保存后开始生效');
                }
            },
            btn2: function(index){
                layer.close(index);
            }

        });
    });

    //延期按钮禁用
    function intiExtension(){
        let cardDate = $('#card_date').val();
        if (cardDate == null || cardDate == '' || cardDate == undefined){}else {
            $('#card_date').attr("disabled",true).addClass("disabled");
        }
        var img = $node.imageUri.val();
        if (img == null || img == '' || img == undefined){
            $('#extension_button').addClass("layui-btn-disabled").attr("disabled",true);
        }
    }
});