<div id="app" v-cloak>
    <div class="form-search">
        <el-form ref="vm" :model="vm" :inline="true" :label-width="'70px'">
            <el-form-item label="规则目标" prop="ruleFor" size="mini">
                <el-select v-model="vm.ruleFor" clearable size="mini" filterable placeholder="请输入规则目标">
                    <el-option key="全局" label="全局" value="全局"></el-option>
                    <el-option key="主机" label="主机" value="主机"></el-option>
                    <el-option key="路径" label="路径" value="路径"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="规则标题" prop="ruleTitle" size="mini">
                <el-input v-model="vm.ruleTitle" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入规则标题"></el-input>
            </el-form-item>
                <el-form-item label="规则标题" prop="ruleTitleLike" size="mini">
                    <el-input v-model="vm.ruleTitleLike" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入规则标题模糊查询"></el-input>
                </el-form-item>
            <el-form-item label="规则名称" prop="ruleName" size="mini">
                <el-input v-model="vm.ruleName" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入规则名称"></el-input>
            </el-form-item>
                <el-form-item label="规则名称" prop="ruleNameLike" size="mini">
                    <el-input v-model="vm.ruleNameLike" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入规则名称模糊查询"></el-input>
                </el-form-item>
            <el-form-item label="规则属值" prop="ruleValue" size="mini">
                <el-input v-model="vm.ruleValue" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入规则属值"></el-input>
            </el-form-item>
                <el-form-item label="规则属值" prop="ruleValueLike" size="mini">
                    <el-input v-model="vm.ruleValueLike" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入规则属值模糊查询"></el-input>
                </el-form-item>
            <el-form-item label="默认属值" prop="valueDefault" size="mini">
                <el-input v-model="vm.valueDefault" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入默认属值"></el-input>
            </el-form-item>
            <el-form-item label="属值正则" prop="valueRegex" size="mini">
                <el-input v-model="vm.valueRegex" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入属值正则"></el-input>
            </el-form-item>
                <el-form-item label="规则描述" prop="ruleNoteLike" size="mini">
                    <el-input v-model="vm.ruleNoteLike" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入规则描述模糊查询"></el-input>
                </el-form-item>
            <el-form-item label="开始日期" prop="startDate" size="mini">
                <el-date-picker v-model="vm.startDate" clearable size="mini" placeholder="请输入开始日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期" prop="endDate" size="mini">
                <el-date-picker v-model="vm.endDate" clearable size="mini" placeholder="请输入结束日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item size="mini">
                <el-button-group>
                    <el-button type="primary" size="mini" icon="el-icon-search" @click="onSearch">搜索</el-button>
                    <el-button type="warning" size="mini" icon="el-icon-refresh-left" @click="onReset('vm')">重置</el-button>
                </el-button-group>
            </el-form-item>
        </el-form>
    </div>

    <div class="form-table">
        <el-row type="flex" justify="space-between">
            <el-button-group>
            <#if token.hasRes("/ajax/admin/rules/create")>
                <el-button type="success" size="mini" icon="el-icon-plus" @click="onCreate">新增</el-button>
            </#if>
            <#if token.hasRes("/ajax/admin/rules/delete")>
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="onBitchDelete">删除</el-button>
            </#if>
            </el-button-group>

            <el-button-group style="float: right;">
                <#if token.hasRes("/ajax/admin/rules/template")>
                <el-tooltip effect="dark" content="Excel导入模板下载" placement="bottom">
                    <el-button size="mini" icon="el-icon-date" @click="onTemplate"></el-button>
                </el-tooltip>
                </#if>

                <#if token.hasRes("/ajax/admin/rules/imports")>
                <el-tooltip effect="dark" content="Excel导入" placement="bottom">
                    <el-button size="mini" icon="el-icon-upload2" @click="onImport"></el-button>
                </el-tooltip>
                </#if>

                <#if token.hasRes("/ajax/admin/rules/exports")>
                <el-tooltip effect="dark" content="Excel导出" placement="bottom">
                    <el-button size="mini" icon="el-icon-download" @click="onExport"></el-button>
                </el-tooltip>
                </#if>

                <el-tooltip effect="dark" content="刷新" placement="bottom">
                    <el-button size="mini" icon="el-icon-refresh" @click="onFind"></el-button>
                </el-tooltip>
            </el-button-group>
        </el-row>

        <el-table
                :data="result"
                :size="'mini'"
                :border="true"
                :stripe="true"
                :empty-text="'无数据'"
                @selection-change="onSelectionChange">
            <el-table-column
                    align="center"
                    width="45"
                    type="selection">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="140"
                    prop="id"
                    label="主键">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="100"
                    prop="ruleFor"
                    label="规则目标">
                <template slot-scope="scope">
                    <el-tag size="mini" v-if="scope.row.ruleFor == '全局'">全局</el-tag>
                    <el-tag size="mini" v-if="scope.row.ruleFor == '主机'">主机</el-tag>
                    <el-tag size="mini" v-if="scope.row.ruleFor == '路径'">路径</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="180"
                    prop="ruleTitle"
                    label="规则标题"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="180"
                    prop="ruleName"
                    label="规则名称"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="180"
                    prop="ruleValue"
                    label="规则属值"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="180"
                    prop="valueDefault"
                    label="默认属值"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="180"
                    prop="valueRegex"
                    label="属值正则"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="180"
                    prop="ruleNote"
                    label="规则描述"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="140"
                    prop="createTime"
                    label="创建时间">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="120"
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <wb-table-ops :arg="scope.row">
                        <#if token.hasRes("/ajax/admin/rules/update")>
                        <wb-table-op slot="menu" icon="el-icon-edit" @click="onEdit">编辑</wb-table-op>
                        </#if>
                        <#if token.hasRes("/ajax/admin/rules/delete")>
                        <wb-table-op slot="menu" icon="el-icon-delete" @click="onDelete">删除</wb-table-op>
                        </#if>
                    </wb-table-ops>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                background
                v-if="vm.totalCount > vm.pageSize"
                style="margin-top: 10px"
                @current-change="onPageChange"
                @size-change="onPageSizeChange"
                :current-page="vm.pageNumber"
                :page-size="vm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="vm.totalCount">
        </el-pagination>
    </div>

    <el-dialog
            :fullscreen="false"
            :title="form.formTitle"
            :custom-class="'form-dialog'"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :modal-append-to-body="true"
            :visible.sync="form.formShow">

        <el-page-header slot="title" @back="form.formShow = false" :content="form.formTitle"></el-page-header>

        <el-form
                :ref="'form'"
                :model="form"
                :inline="true"
                :rules="formRules"
                :label-width="'85px'"
                :label-position="'right'">
            <el-form-item label="规则目标" prop="ruleFor" size="mini">
                <el-select v-model="form.ruleFor" clearable size="mini" filterable placeholder="请输入规则目标">
                        <el-option key="全局" label="全局" value="全局"></el-option>
                        <el-option key="主机" label="主机" value="主机"></el-option>
                        <el-option key="路径" label="路径" value="路径"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="规则标题" prop="ruleTitle" size="mini">
                <el-input v-model="form.ruleTitle"
                          clearable
                          size="mini"
                          maxlength="100"
                          show-word-limit
                          placeholder="请输入规则标题"></el-input>
            </el-form-item>
            <el-form-item label="规则名称" prop="ruleName" size="mini" class="full">
                <el-input v-model="form.ruleName"
                          rows="3"
                          clearable
                          size="mini"
                          maxlength="100"
                          type="textarea"
                          show-word-limit
                          placeholder="请输入规则名称"></el-input>
            </el-form-item>
            <el-form-item label="规则属值" prop="ruleValue" size="mini" class="full">
                <el-input v-model="form.ruleValue"
                          rows="3"
                          clearable
                          size="mini"
                          maxlength="100"
                          type="textarea"
                          show-word-limit
                          placeholder="请输入规则属值"></el-input>
            </el-form-item>
            <el-form-item label="默认属值" prop="valueDefault" size="mini" class="full">
                <el-input v-model="form.valueDefault"
                          rows="3"
                          clearable
                          size="mini"
                          maxlength="100"
                          type="textarea"
                          show-word-limit
                          placeholder="请输入默认属值"></el-input>
            </el-form-item>
            <el-form-item label="属值正则" prop="valueRegex" size="mini" class="full">
                <el-input v-model="form.valueRegex"
                          rows="3"
                          clearable
                          size="mini"
                          maxlength="100"
                          type="textarea"
                          show-word-limit
                          placeholder="请输入属值正则"></el-input>
            </el-form-item>
            <el-form-item label="规则描述" prop="ruleNote" size="mini" class="full">
                <el-input v-model="form.ruleNote"
                          rows="10"
                          clearable
                          size="mini"
                          maxlength="500"
                          type="textarea"
                          show-word-limit
                          placeholder="请输入规则描述"></el-input>
            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
			<el-button size="mini" @click="form.formShow = false">取消</el-button>
			<el-button size="mini" type="primary" @click="onSave">保存</el-button>
		</span>
    </el-dialog>
</div>
<script>
    var app = new Vue({
        mixins: [mixinForMgr],
        el: "#app",
        data: {
            module: 'admin',
            target: 'rules',
            vm: {//条件及分页参数
                ruleFor: null,
                ruleTitle: null,
                ruleTitleLike: null,
                ruleName: null,
                ruleNameLike: null,
                ruleValue: null,
                ruleValueLike: null,
                valueDefault: null,
                valueRegex: null,
                ruleNoteLike: null,
                startDate: null,
                endDate: null,
                pageNumber: 1,
                pageSize: 10,
                totalCount: 0,
                sortKey:'CREATE_TIME',
                sortType:'DESC'
            },
            form: {//待提交表单
                formTitle: "",
                formShow: false,
                id: null,
                ruleFor: null,
                ruleTitle: null,
                ruleName: null,
                ruleValue: null,
                valueDefault: null,
                valueRegex: null,
                ruleNote: null,
                rowVersion: null
            },
            formRules: {
                ruleFor: [
                ],
                ruleTitle: [
                    {min: 1, max: 100, message: '规则标题长度在 1 到 100 个字符', trigger: 'blur'}
                ],
                ruleName: [
                    {min: 1, max: 100, message: '规则名称长度在 1 到 100 个字符', trigger: 'blur'}
                ],
                ruleValue: [
                    {min: 1, max: 100, message: '规则属值长度在 1 到 100 个字符', trigger: 'blur'}
                ],
                valueDefault: [
                    {min: 1, max: 100, message: '默认属值长度在 1 到 100 个字符', trigger: 'blur'}
                ],
                valueRegex: [
                    {min: 1, max: 100, message: '属值正则长度在 1 到 100 个字符', trigger: 'blur'}
                ],
                ruleNote: [
                    {min: 1, max: 500, message: '规则描述长度在 1 到 500 个字符', trigger: 'blur'}
                ],
            }
        },
        methods: {
            onCreate: function () {
                this.form.formTitle = "配置规则新增";
                this.form.formShow = true;
                this.form.id = "";
                this.form.ruleFor = "";
                this.form.ruleTitle = "";
                this.form.ruleName = "";
                this.form.ruleValue = "";
                this.form.valueDefault = "";
                this.form.valueRegex = "";
                this.form.ruleNote = "";
            },
            onEdit: function (item) {
                this.form.formTitle = "配置规则编辑";
                this.form.formShow = true;
                this.form.id = item.id;
                this.form.ruleFor = item.ruleFor;
                this.form.ruleTitle = item.ruleTitle;
                this.form.ruleName = item.ruleName;
                this.form.ruleValue = item.ruleValue;
                this.form.valueDefault = item.valueDefault;
                this.form.valueRegex = item.valueRegex;
                this.form.ruleNote = item.ruleNote;
                this.form.rowVersion = item.rowVersion;
            }
        },
        mounted: function () {
            this.onFind();
        },
    })
</script>
