"use strict";
layui.extend({
    common    :   'js/common',
    xmSelect    :   'layui/dist/xm-select',
}).use(['jquery','form','common','xmSelect','laydate'],function(){
    var $ = layui.jquery;
    //  引入表单插件
    var form = layui.form;
    //  引入公共模块
    var common = layui.common;
    //  引入下拉搜索菜单
    var xmSelect = layui.xmSelect;

    var laydate = layui.laydate;


    //  页面元素
    var pageData = {
        //  取消按钮
        resetBtn        :   $('button[type="reset"]'),
        //卡号输入框
        cardNumNode : $('input[name="cardNum"]'),
        //是否重名
        notDuplicate : true,
    };

    var $node = {
        selectOrg       :   $('#org_id')
    };


    //日期时间范围
    laydate.render({
        elem: '#card_date'
        ,type: 'date'
        ,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})
        },
    });
    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: ', ',
                },
            }
        },
    });

    //监听卡号失焦
    pageData.cardNumNode.on("blur",function () {
        var cardNum = pageData.cardNumNode.val();
        common.sendAsyncRequest("/flowCard/manage/getOneFlowCard?cardNum=" + cardNum,'GET','',function (result) {
            pageData.notDuplicate = !result.data;
        })
    });

    //  绑定表单提交事件
    layui.form.on("submit(entity_submit_event)",function(data){
        common.timer($('button[lay-filter="entity_submit_event"]'));
        $.ajax({
            url     :   '/device/flowCard/add',
            data    :   data.field,
            type    :   'POST',
            success :   function (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("添加失败");
                }
            }
        });
    });
    //  表单校验
    layui.form.verify({
        // Numbers : [/(^$)|^\d+$/,'只能填写数字'],
        duplicate : function () {
            if (!pageData.notDuplicate){
                return "该卡号已存在。"
            }
        }
    });

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

    var pidSelect = xmSelect.render({
        el: '#org_id',
        radio: true,
        name:'orgId',
        layVerify: 'required',
        prop: {
            name: 'name',
            value: 'id',
        },
        toolbar: {
            show: true,
            list: ['CLEAR']
        },
        clickClose: true,
        filterable: true,
        remoteSearch: true,
        remoteMethod: function (val, cb, show) {
            $.ajax({
                type: 'GET',
                url: '/permissions/org/getListByLikeName',
                data: {
                    name : val,
                    flag    : 1,
                    orgType : 1,
                    isSelect : true
                },
                success: function (result) {
                    cb(result.list);
                },
                error: function () {
                    cb([]);
                }
            });
        },
        //监听选择
        on: function (data) {},
        //显示方式
        model: {
            label: {
                type: 'text',
                text: {
                    //左边拼接的字符
                    left: '',
                    //右边拼接的字符
                    right: '',
                    //中间的分隔符
                    separator: ', ',
                },
            }
        },
    });
});
