<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <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" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card layui-form">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="HZSGL_HZSGL_HZSGL_T" html='<li class="layui-this"><a href="${ctx}/partnerManagement/toPartnerPage">资方管理</a></li>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset"  onclick="reset()" >
                    <i class="layui-icon fs-12">&#xe9aa;</i>
                    重置
                </button>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " id="searchBtn" data-type="reload">
                    <i class="layui-icon fs-12">&#xe615;</i>查询
                </button>
            </ul>
        </div>

        <div class="layui-card-body layuiadmin-card-header-auto" >
            <form class="layui-form layui-card" action="" id="searchForm">
                <div class="layui-form-item">

                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">合作商名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" class="layui-input" id="name" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">授权项目：</label>
                        <div class="layui-input-block">
                            <div id="authorizedId"></div>
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">负责人：</label>
                        <div class="layui-input-block">
                            <input type="text" name="personInCharge" class="layui-input" id="personInCharge" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">联系电话：</label>
                        <div class="layui-input-block">
                            <input type="text" name="mobile" class="layui-input" id="mobile" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-col-lg4 layui-col-sm6">
                        <label class="layui-form-label">创建时间：</label>
                        <div class="layui-input-block">
                            <div class="layui-col-xs5">
                                <input type="text" name="createTimeMin" id="createTimeMin" class="layui-input"  placeholder="开始日期" autocomplete="off">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs5">
                                <input type="text" name="createTimeMax" id="createTimeMax" class="layui-input"  placeholder="结束日期" autocomplete="off">
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <div class="layui-card-body">
            <table class="layui-hide " id="test" lay-filter="test"></table>
        </div>

    </div>
</div>
<div id="toolbarDemo" class="layui-form-item layui-hide">
    <sfTags:pagePermission authFlag="HZSGL_HZSGL_XZ_B" html='<button class="layui-btn layui-btn-sm" id="addBtn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增 </button>'/>
</div>
</div>
<script type="text/html" id="table-toolbar">
    <sfTags:pagePermission authFlag="HZSGL_HZSGL_BJ_B" html='<a href="#" style="font-size:14px" class="layui-table-link layui-icon" lay-event="edit">编辑&nbsp;&nbsp;</a>'/>
    <sfTags:pagePermission authFlag="HZSGL_HZSGL_SC_B" html='<a href="#" style="font-size:14px" class="layui-table-link layui-icon" lay-event="delete">删除</a>'/>
</script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/data.js"></script>
<script src="${ctxPlugin}/lib/xm-select.js"></script>
<script type="text/javascript">
    var saveMark = false;
    //初始数据
    var $form;
    var form;
    var upload;
    var $;
    var table;
    var projectSelect;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laypage','laydate','form','excel'], function(){
        $ = layui.$
            ,form = layui.form
            ,tableExt = layui.tableExt;
        table = layui.table;
        var excel = layui.excel;
        var laydate = layui.laydate;

        projectSelect = xmSelect.render({
            el: '#authorizedId',
            name: 'authorizedId',
            filterable: true,
            size: 'mini',
            model: {
                label: {
                    type: 'block',
                    block: {
                        //最大显示数量, 0:不限制
                        showCount: 2,
                        //是否显示删除图标
                        showIcon: true,
                    }
                }
            },
            toolbar: {
                show: true
            },
            height: '300px',
            data: [
                <c:forEach items="${projects}" var="project" varStatus="vs">
                <c:if test="${vs.count > 1}">, </c:if>
                {name: '${project.columns.name}', value: '${project.columns.id}'}
                </c:forEach>
            ]
        });
        projectSelect.setValue('${partner.authorizedId}'.split(","));

        //开始日期
        var insStart = laydate.render({
            elem: '#createTimeMin'
            ,type: 'date'
            ,done: function(value, date){
                //更新结束日期的最小日期
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                insEnd.config.elem[0].focus();
            }
        });
        //结束日期
        var insEnd = laydate.render({
            elem: '#createTimeMax'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        tableExt.render({
            elem: '#test'
            , height: 'full-130'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/partnerManagement/ajaxForPartnerData'
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , page: true
            , toolbar:"#toolbarDemo"
            , method:"post"
            , limits: [20, 50, 100]
            , limit: 20
            , cols: [[
                {field: 'number', minWidth: 50, title: '序号', align: 'center', type: 'numbers',rowspan:'2', fixed: 'left'}
                ,{field:'id', title: '操作',toolbar: '#table-toolbar',align: 'center', fixed: 'left'}
                , {field: 'name', title: '合作商名称', minWidth: 150, align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'authorizedName', title: '授权项目', minWidth: 150, align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'login_name', title: '登录账号', minWidth: 150, align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'person_in_charge', title: '负责人', minWidth: 150, align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'mobile', minWidth: 180, title: '联系电话', align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'address', title: '合作商地址', minWidth: 400, align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'create_time', title: '创建时间', minWidth: 200, align: 'center',exportable:true ,rowspan:'2'}
            ],
            ]
            ,exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '合作商信息',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '合作商信息',
                where: function (){
                    console.log(_serializeJson($("#searchForm")));//当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        table.on('toolbar(test)',function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    title: '新增',
                    content: '${ctx}/partnerManagement/toPartnerForm',
                    area: ['50%', '55%']
                });
            }
        });

        //监听事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {//编辑
                var editId = data.id;
                layer.open({
                    type: 2,
                    title: '编辑',
                    content: '${ctx}/partnerManagement/toPartnerForm?id='+editId,
                    area: ['50%', '55%'],
                });
            } else if (obj.event === 'delete') {
                var editId = data.id;
                layer.confirm('确定删除该合作商?',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                deletePartner(editId);
                                //关闭弹窗
                                layer.close(index);
                                console.log("回车确认时事件")
                                //取消连续回车键，导致不断弹出询问框
                                $(':focus').blur();
                            }
                        };
                        //监听键盘事件
                        $(document).on('keydown', this.enterEsc);
                        //esc键触发取消
                        this.escQuit = (ev) => {
                            let code_val = ev.which;
                            if(code_val == 0x1B){
                                $('.layui-layer-btn1').click();
                                return false;
                            }
                        };
                        $(document).on('keydown', this.escQuit);
                    },
                    end: function () {
                        //取消键盘监听事件
                        $(document).off('keydown', this.enterEsc);
                        $(document).off('keydown', this.escQuit);
                    },
                    yes: function () {
                        //确认执行的代码
                        deletePartner(editId);
                    },
                    no: function () {

                    }
                });
            }
        });

        function isBlank(val) {
            if (val == null || val == '' || val == undefined) {
                return true;
            }
            return false;
        }
        $('#searchBtn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        var active = {
            reload: function(){
                reload();
            }
        };
    });

    function reload(){
        table.reload('test', {
            where: {
                name: $('#name').val().trim(),
                authorizedId: projectSelect.getValue('valueStr'),
                personInCharge: $('input[name=personInCharge]').val().trim(),
                mobile: $('input[name=mobile]').val().trim(),
                createTimeMin:$("#createTimeMin").val().trim(),
                createTimeMax:$("#createTimeMax").val().trim()
            }
        });
    }

    function deletePartner(editId){
        $.post('${ctx}/partnerManagement/deletePartner', {id: editId}, function(result){
            if (result.code === '200') {
                layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                    window.location.reload();
                });
            } else {
                layer.msg('操作失败');
            }
        })
    }

    function  denglsj(id,userId) {
        console.log("siteInfo" + id +" userId : "+ userId)
        parent.denglusdj(id,userId);
    }

    function reset(){
        layui.form.render();
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input").val("");
        projectSelect.reset();
    }

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