<div id="app" v-cloak>
    <el-card class="box-card search">
        <el-form :inline="true" :model="vm" ref="vm" label-width="90px">
            <el-form-item label="字典名称" prop="dictName">
                <el-input v-model="vm.dictName" clearable size="small" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="字典代码" prop="dictCode">
                <el-input v-model="vm.dictCode" clearable size="small" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="是否有效" prop="valid">
                <el-select v-model="vm.valid" size="small" placeholder="">
                    <el-option label="所有" value=""></el-option>
                    <el-option label="有效" value="true"></el-option>
                    <el-option label="无效" value="false"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="small" icon="el-icon-search" @click="onSearch">搜索</el-button>
                <el-button type="warning" size="small" icon="el-icon-refresh-left" @click="onReset('vm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <el-card class="box-card">
        <el-row>
            <el-col :span="12">
                <el-button type="success" size="small" icon="el-icon-plus" @click="onCreate">新增</el-button>

                <el-button type="warning" size="small" icon="el-icon-download" @click="onExport">导出</el-button>

                <el-dialog class="form" :title="form.title" :visible.sync="form.dialog">
                    <el-form :model="form" :inline="true" :rules="formRules" ref="form" label-width="90px">
                        <el-form-item label="字典名称" prop="dictName">
                            <el-input size="small" v-model="form.dictName"></el-input>
                        </el-form-item>
                        <el-form-item label="字典代码" prop="dictCode">
                            <el-input size="small" v-model="form.dictCode"></el-input>
                        </el-form-item>
                        <el-form-item label="字典版本" required>
                            <el-date-picker size="small"
                                            v-model="form.version"
                                            type="datetime"
                                            value-format="yyyy-MM-dd HH:mm:ss"
                                            placeholder="选择日期时间"
                                            align="right">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="是否有效" prop="valid">
                            <el-switch size="small" v-model="form.valid"></el-switch>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button size="small" @click="form.dialog = false">取 消</el-button>
                        <el-button size="small" type="primary" @click="onSave">保存</el-button>
                    </span>
                </el-dialog>
            </el-col>

            <el-col :span="12">
                <el-button-group style="float: right;">
                    <el-tooltip effect="dark" content="Excel模板下载" placement="bottom">
                        <el-button size="small" icon="el-icon-date" @click="onTemplate"></el-button>
                    </el-tooltip>

                    <el-tooltip effect="dark" content="Excel导入" placement="bottom">
                        <el-button size="small" @click="selectExcel">
                            <i class="el-icon-upload2">
                                <input type="file" style="display: none" @change="onImport($event)" ref="excel">
                            </i>
                        </el-button>
                    </el-tooltip>

                    <el-tooltip effect="dark" content="批量删除" placement="bottom">
                        <el-button size="small" icon="el-icon-delete" @click="onBitchDelete"></el-button>
                    </el-tooltip>

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

        <el-table
                style="margin-top: 10px"
                @selection-change="onSelectionChange"
                empty-text="无数据"
                :data="result"
                size="small"
                style="width: 100%">
            <el-table-column
                    align="center"
                    type="selection"
                    width="50">
            </el-table-column>
            <el-table-column
                    align="center"
                    prop="id"
                    label="主键"
                    width="140">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="80"
                    label="是否有效">
                <template slot-scope="scope">
                    <el-tag size="mini" v-if="scope.row.valid">有效</el-tag>
                    <el-tag size="mini" type="danger" v-if="!scope.row.valid">无效</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    prop="dictName"
                    label="字典名称">
            </el-table-column>
            <el-table-column
                    align="center"
                    prop="dictCode"
                    label="字典代码">
            </el-table-column>
            <el-table-column
                    align="center"
                    prop="version"
                    label="字典版本">
            </el-table-column>
            <el-table-column
                    align="center"
                    prop="createTime"
                    label="创建时间">
            </el-table-column>
            <el-table-column
                    align="center"
                    fixed="right"
                    width="120"
                    label="操作">
                <template slot-scope="scope">
                    <el-dropdown size="mini" type="primary" split-button @click="onCommand(['view',scope.row])"
                                 @command="onCommand">
                        <i class="el-icon-tickets"></i>查看
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item :command="['edit',scope.row]" icon="el-icon-edit">编辑</el-dropdown-item>
                            <el-dropdown-item :command="['delete',scope.row]" icon="el-icon-delete">删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                background
                v-if="vm.totalCount > vm.pageSize"
                style="margin-top: 10px"
                @current-change="onPageChange"
                :current-page="vm.pageNumber"
                :page-size="vm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="vm.totalCount">
        </el-pagination>
    </el-card>
</div>
<script>
    var app = new Vue({
        mixins: [mixin],
        el: "#app",
        data: {
            module: 'system',
            target: 'dict',
            vm: {
                id: '',
                dictName: '',
                dictCode: '',
                valid: "",
                pageNumber: 1,
                pageSize: 10,
                totalCount: 0,
                sortKey:'CREATE_TIME',
                sortType:'DESC'
            },
            select: [],
            result: [],
            form: {
                title: "",
                dialog: false,
                id: '',
                dictName: '',
                dictCode: '',
                version: '',
                valid: false,
                rowVersion: '',
            },
            formRules: {
                dictName: [
                    {required: true, message: '请输入字典名称', trigger: 'blur'},
                    {min: 1, max: 50, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                ],
                dictCode: [
                    {required: true, message: '请输入字典代码', trigger: 'blur'},
                    {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
                ],
                version: [
                    {type: 'date', required: true, message: '字典版本不能为空', trigger: 'change'}
                ],
            },
        },
        methods: {
            onCreate: function () {
                this.form.title = '新增字典';
                this.form.id = '';
                this.form.dictName = '';
                this.form.dictCode = '';
                this.form.version = '';
                this.form.valid = true;
                this.form.dialog = true;
            },
            onCommand: function (args) {
                const cmd = args[0];
                const item = args[1];
                switch (cmd) {
                    case "edit":
                        this.form.title = '编辑字典';
                        this.form.id = item.id;
                        this.form.dictName = item.dictName;
                        this.form.dictCode = item.dictCode;
                        this.form.version = item.version;
                        this.form.valid = item.valid;
                        this.form.rowVersion = item.rowVersion;
                        this.form.dialog = true;
                        break;
                    case "delete":
                        this.onDelete(item);
                        break;
                    case "view":
                        if (!parent || !parent.index) {
                            this.e("找不到上级页面!");
                        } else {
                            parent.index.addTab({
                                title: "字典项管理",
                                name: "dictItem" + item.id,
                                url: "${context}/system/dictItem.htm?dictId=" + item.id
                            });
                        }
                        break;
                    default:
                        this.w("未找到对应的命令");
                        break;
                }
            }
        },
        mounted: function () {
            this.onFind();
        }
    })
</script>
