<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>选择汇流组</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base"/>
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/lib/font/iconfont.css"/>
    <style>
        html {
            background-color: #fff;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-form" id="searchForm">
        <div class="layui-card">
            <div class="layui-card-body layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <%--<div class="layui-col-sm10">
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" placeholder="请输入员工姓名进行搜索"
                                   name="name">
                        </div>
                    </div>
                    <button type="button" class="layui-btn f-r mr-15 pl-10 pr-10" id="searchBtn" lay-submit
                            lay-filter="LAY-user-back-search">
                        <i class="layui-icon fs-12">&#xe615;</i>
                        查询
                    </button>--%>
                        <button type="button" class="layui-btn f-r mr-15 pl-10 pr-10" id="addBtn">
                            新增
                        </button>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-body hasMsg">
                <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
            </div>
        </div>
    </div>
</div>
<div class="operate-footer">
    <button class="layui-btn sureAdd">确认</button>
    <button class="layui-btn layui-btn-primary" onclick="cancel();">取消</button>
</div>
<script type="text/html" id="choose">
    <input type="radio" lay-skin="primary" name="choose" value="{{d.id}}" data-name="{{d.group_name}}" data-title="{{d.group_number}}" data-type="{{d.total_count}}" data-value="{{d.already_into_count}}"/>
</script>
<div class="hide layui-tab-content" id="addDiv" style="margin-right: 20px">
    <label class="layui-form-label w-120"><span class="red">*</span>本组电站个数：</label>
    <div class="layui-input-block ml-120">
        <input class="layui-input" name="totalCount" oninput="value=value.replace(/[^\d]/g,'')">
    </div>
</div>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer', 'form'], function () {
        $ = layui.$;
        tableExt = layui.tableExt;
        layer = layui.layer;
        form = layui.form;
        table = tableExt.getLayTable();

        tableExt.render({
            elem: '#test'
            , height: 'full-160'
            , page : false //开启分页
            , url: '${ctx}/combinerBoxGroupSet/boxGroupData?entryId=${entryId}'
            , cols: [[
                {title: '序号', type: 'numbers'}
                , {field: 'id', title: '选择', align: 'center', templet: '#choose'}
                , {field: 'group_number', title: '汇流组编号', align: 'center'}
                , {field: 'group_name', title: '汇流组名称', align: 'center'}
                , {field: 'total_count', title: '本组电站个数', align: 'center'}
                , {field: 'already_into_count', title: '已入电站个数', align: 'center'}
                , {field: 'province', title: '安装地址', align: 'center'
                    ,templet:function(d){
                        return d.province+d.city+d.area;
                        }
                    ,expFormat:function (d) {
                        return d.province+d.city+d.area;
                    }
            }
            ]],
            done: function(res){
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('test', {
                where: field
            });
        });

        $('input').on('keydown', function(event){
            if(event.keyCode == 13){
                //执行相关操作
                $("#searchBtn").click();
            }
        });

        var fg=false;
        $('.sureAdd').click(function(){
            var $checkObj=$('input[name=choose]:checked');
            var groupId = $checkObj.val();
            if(isBlank(groupId)){
                layer.msg('请选择汇流组!');
                return;
            }
            if(fg){
                return;
            }
            var groupName= $checkObj.attr("data-name");
            var groupNumber=$checkObj.attr("data-title");
            var totalCount=$checkObj.attr("data-type");
            fg=true;
            $.ajax({
                type: 'post',
                url: '${ctx}/combinerBoxGroupSet/saveOneOwner',
                data: {
                    entryId: '${entryId}',
                    boxGroupId:groupId
                },
                success: function (result) {
                    if (result.code === '200') {
                        layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);//关闭当前页
                            parent.saveGroup2(groupId,groupName,groupNumber,totalCount);
                        });
                    } else {
                        layer.msg('操作失败！' + result.errMsg);
                        fg=false;
                    }
                }
            })
        });

        $("#addBtn").click(function(){
            var idx= layer.open({
                type: 1,
                area: ['450px', '280px'],
                content: $("#addDiv"),
                title: '新增',
                btn: ['确认', '取消'],
                btnAlign: 'c',
                yes: function () {
                    var totalCount = $('input[name="totalCount"]').val();
                    if(isBlank(totalCount)){
                        layer.msg("本组电站个数不能为空!");
                        return;
                    }
                    var index= layer.load(2, {shade: [0.5,'#fff'], content:''});
                    $.ajax({
                        type: 'post',
                        url: '${ctx}/combinerBoxGroupSet/saveOneOwner',
                        data: {
                             entryId: '${entryId}',
                            totalCount:totalCount
                        },
                        success: function (data) {
                            if(data.code =='200'){
                                layer.msg('保存成功！', {icon:1,time:1000},function(){
                                    layer.close(idx);
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);//关闭当前页
                                    parent.saveGroup(totalCount);
                                })
                            }else{
                                layer.msg("保存失败！"+data.errMsg)
                            }
                        },complete: function () {
                            layer.close(index);
                        },
                        error:function (data) {
                            layer.close(index);
                        }
                    });

                }
            })

        })

    });

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }

    function cancel() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);//关闭当前页
    }

</script>
</body>
</html>
