<template>
<!-- 指标类型维护 -->
    <div class="indexTypeMaintain">
        <div class="box">
            <div class="box-top">
                <el-form
                    style="overflow: hidden;"
                    :style="filterForm.show ? null : { height: '0px' }"
                    size="mini"
                    label-width="90px"
                >
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="类别类" label-width="60px">
                                <el-select
                                filterable clearable
                                    v-model="filterForm.data.indexcol"
                                    placeholder=""
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.indexcol.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="指标类型代码">
                                <el-input clearable
                                    v-model="filterForm.data.indexid"
                                    placeholder=""
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="指标类型名称">
                                <el-input clearable
                                    v-model="filterForm.data.indexname"
                                    placeholder=""
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="box-top-gjl">
                    <el-button
                        class="button"
                        type="primary"
                        size="mini"
                        icon="el-icon-plus"
                        v-privilege="activeMenu + 'ADD'"
                        @click="but_add"
                    >新增</el-button>
                    <div style="float:right; text-align: right;">
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-search"
                            v-privilege="activeMenu + 'QUERY'"
                            @click="getTableData(1)"
                        >查询</el-button>
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-refresh"
                            v-privilege="activeMenu + 'QUERY'"
                            @click="tableDataCancel();"
                        >重置</el-button>
                        <el-button
                            size="mini"
                            :icon="filterForm.show ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
                            :title="filterForm.show ? '收起搜索区' : '展开搜索区'"
                            @click="filterForm.show = !filterForm.show;
                                $nextTick(() => {
                                    singleTableHeight = getRoleHeight($refs['singleTable'].$el) - 45;
                                }
                            );"
                        >{{ filterForm.show ? '收起' : '展开' }}</el-button>
                    </div>
                </div>
            </div>
            <div class="box-bottom">
                <el-table stripe
                    id="singleTable"
                    ref="singleTable"
                    :data="tableData"
                    v-loading="tableLoading"
                    style="width: 100%;"
                    :height="singleTableHeight"
                    border
                    size="mini"
                    highlight-current-row
                >
                    <el-table-column
                    sortable
                        prop="indexcol"
                        label="类别列"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.indexcol.obj[scope.row.indexcol] ? nameObj.indexcol.obj[scope.row.indexcol] : scope.row.indexcol }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                    sortable
                        prop="indexid"
                        label="指标类型代码"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="indexcode"
                        label="指标类型序号"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="indexname"
                        label="指标类型名称"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="indexdesc"
                        label="指标类型描述"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        class-name="gn-TableDownloadExcel-none"
                        label="操作"
                        align="center"
                        width="140px"
                        fixed="right"
                    >
                        <template slot-scope="scope">
                            <div>
                                <el-button
                                    type="primary"
                                    size="mini"
                                    v-privilege="activeMenu + 'PUT'"
                                    @click="but_edit(scope.row)"
                                >修改</el-button>
                                <el-button
                                    type="danger"
                                    size="mini"
                                    v-privilege="activeMenu + 'DELETE'"
                                    @click="but_del(scope.row)"
                                >删除</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    :page-sizes="[10, 20, 50, 100, 500, 1000]"
                    :page-size="pageSize"
                    :current-page.sync="pageNum"
                    @size-change="tableSizeChange"
                    @current-change="getTableData()"
                    style="text-align: right;margin-top: 10px;"
                ></el-pagination>
            </div>
        </div>
        <el-dialog
            :title="'数据 - ' + (dialog.editorBox.type === 'add' ? '新增' : '修改')"
            :visible.sync="dialog.editorBox.show"
            width="620px"
            :close-on-click-modal="false"
            :show-close="!loading"
        >
            <div>
                <el-form
                    ref="dialog_form"
                    :model="dialog.editorBox.form.data"
                    :rules="dialog.editorBox.form.rules"
                    size="mini"
                    label-width="110px"
                >
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="类别列" prop="indexcol">
                                <el-select
                                    filterable clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.indexcol"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                >
                                    <el-option
                                        v-for="item of nameObj.indexcol.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="指标类型代码" prop="indexid">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.indexid"
                                    :disabled="dialog.editorBox.type !== 'add'"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="指标类型名称" prop="indexname">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.indexname"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="指标类型描述" prop="indexdesc">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.indexdesc"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                           <el-col :span="12">
                            <el-form-item label="指标类型序号" prop="indexcode">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.indexcode"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button @click="dialog.editorBox.show = false" :loading="loading">取 消</el-button>
                <el-button
                    type="primary"
                    @click="dataSave"
                    :loading="loading"
                >保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'indexTypeMaintain',
    data () {
        const checkNo = (rule, value, callback) => {
            if (value) {
                let str = /^[a-zA-Z]+$/;
                if (!str.test(value)) {
                    callback(new Error('由字母组成'));
                }
            }
            callback();
        };
        return {
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    indexcol: '',
                    indexid: '',
                    indexname: '',
                    indexcode: ''
                },
                rules: {

                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 100,
            tableData: [
                // {
                //     indexcol: '指标类型',
                //     indexid: '',
                //     indexname: '',
                //     indexdesc: ''
                // }
            ],
            nameObj: {
                indexcol: {
                    obj: {
                        'INDEXTYPE': '指标类型',
                        'TYPE1': '指标类型1',
                        'TYPE2': '指标类型2',
                        'TYPE3': '指标类型3',
                        'TYPE4': '指标类型4'
                    },
                    arr: [
                        {
                            id: 'INDEXTYPE',
                            name: '指标类型'
                        },
                        {
                            id: 'TYPE1',
                            name: '指标类型1'
                        },
                        {
                            id: 'TYPE2',
                            name: '指标类型2'
                        },
                        {
                            id: 'TYPE3',
                            name: '指标类型3'
                        },
                        {
                            id: 'TYPE4',
                            name: '指标类型4'
                        }
                    ]
                }
            },
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    type: '',
                    form: {
                        data: {
                            indexcol: '',
                            indexid: '',
                            indexname: '',
                            indexdesc: '',
                            indexcode: ''
                        },
                        rules: {
                            indexcol: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            indexid: [
                                { required: true, message: '该项不能为空', trigger: 'change' },
                                { validator: checkNo, trigger: 'change' }
                            ],
                            indexname: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            indexcode: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ]
                        }
                    }
                }
            }
        }
    },
    created () {
        this.activeMenu = window.localStorage.getItem('activeMenu');
    },
    mounted () {
        let that = this;
        window.PEDataObj = {
            // 将数据绑定到window上，供main页面使用
            vm: that,
            // tableArr：用于导出成Excel的表格的信息
            tableArr: [
                {
                    name: '',
                    id: 'singleTable'
                }
            ]
        };
        that.$nextTick(() => {
            // 立即获取的height有一定偏差，通过setTimeout延迟来解决
            setTimeout(() => {
                that.singleTableHeight = that.getRoleHeight(that.$refs['singleTable'].$el) - 45;
            }, 1);
        });
        that.getTableData();
    },
    methods: {
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this,
                params = {
                    indexcol: that.filterForm.data.indexcol,
                    indexid: that.filterForm.data.indexid,
                    indexname: that.filterForm.data.indexname
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/trmindextypes/like/?pageNum=' + that.pageNum + '&pageSize=' + that.pageSize;
            that.axios.get(url, {
                params: params
            })
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        for (let item of res.data.list) {
                            arr.push({
                                indexcol: item.indexcol,
                                indexid: item.indexid,
                                indexname: item.indexname,
                                indexdesc: item.indexdesc,
                                indexcode: item.indexcode
                            })
                        }
                        that.tableData = arr;
                        that.total = res.data.total;
                    } else {
                        that.$message.error(res.message);
                    }
                    that.tableLoading = false;
                }).catch(function () {
                    that.tableLoading = false;
                });
        },
        // 改变表格显示条数
        tableSizeChange (val) {
            let that = this;
            that.pageSize = val;
            that.getTableData(1);
        },
        // 重置搜索
        tableDataCancel () {
            let that = this;
            that.filterForm.data.indexcol = '';
            that.filterForm.data.indexid = '';
            that.filterForm.data.indexname = '';
            that.getTableData(1);
        },
        but_add () {
            let that = this;
            that.dialog.editorBox.form.data.indexcol = '';
            that.dialog.editorBox.form.data.indexid = '';
            that.dialog.editorBox.form.data.indexname = '';
            that.dialog.editorBox.form.data.indexdesc = '';
            that.dialog.editorBox.form.data.indexcode = '';
            that.dialog.editorBox.type = 'add';
            that.dialog.editorBox.show = true;
            setTimeout(() => {
                that.$refs['dialog_form'].clearValidate();
            }, 100);
        },
        but_edit (row) {
            let that = this;
            that.dialog.editorBox.form.data.indexcol = row.indexcol;
            that.dialog.editorBox.form.data.indexid = row.indexid;
            that.dialog.editorBox.form.data.indexname = row.indexname;
            that.dialog.editorBox.form.data.indexdesc = row.indexdesc;
            that.dialog.editorBox.type = 'edit';
            that.dialog.editorBox.show = true;
        },
        but_del (row) {
            let that = this;
            that.$confirm('是否删除该条数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                state: 'warning'
            }).then(() => {
                that.axios.delete('pass/ems/v1/trmindextypes/' + row.indexcol + ',' + row.indexid)
                    .then(function (res) {
                        if (res.code === '0') {
                            that.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            that.getTableData();
                        } else {
                            that.$message.error(res.message);
                        }
                    }).catch(function () {
                    });
            }).catch(() => {
            });
        },
        dataSave () {
            let that = this;
            let SubmitData = {
                indexcol: that.dialog.editorBox.form.data.indexcol,
                indexid: that.dialog.editorBox.form.data.indexid,
                indexname: that.dialog.editorBox.form.data.indexname,
                indexdesc: that.dialog.editorBox.form.data.indexdesc,
                indexcode: that.dialog.editorBox.form.data.indexcode
            };
            that.$refs['dialog_form'].validate((valid) => {
                if (valid) {
                    if (that.dialog.editorBox.type === 'add') {
                        that.loading = true;
                        that.axios.post('pass/ems/v1/trmindextypes/', SubmitData, {
                            contentType: 'application/json'
                        })
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '新增成功',
                                        type: 'success'
                                    });
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    } else {
                        that.loading = true;
                        that.axios.put('pass/ems/v1/trmindextypes/', SubmitData)
                            .then(function (res) {
                                if (res.code === '0') {
                                    that.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });
                                    that.getTableData();
                                    that.dialog.editorBox.show = false;
                                } else {
                                    that.$message.error(res.message);
                                }
                                that.loading = false;
                            }).catch(function () {
                                that.loading = false;
                            });
                    }
                }
            });
        }
    }
}
</script>

<style lang="less">
.indexTypeMaintain {
    min-width: 700px;
    height: 100%;
    .box {
        height: 100%;
        padding: 15px 15px 0 15px;
        .box-top {
            .el-form-item {
                margin-bottom: 7px;
            }
            .box-top-gjl {
                overflow: hidden;
                // margin-top: -7px;
                padding: 7px 0;
                border-top: 1px solid #ccc;
                // border-bottom: 1px solid #ccc;
                // margin-bottom: 6px;
            }
        }
    }
}
</style>
