<template>
    <div class="equationMaintain">
        <div class="box">
            <div class="box-top">
                <el-form
                    style="overflow: hidden;"
                    :style="filterForm.show ? null : { height: '0px' }"
                    size="mini"
                    label-width="85px"
                >
                    <el-row>
                        <el-col :span="5">
                            <el-form-item label="公式项目编号">
                                <el-input
                                    clearable
                                    v-model="filterForm.data.code"
                                    placeholder="公式项目编号"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                           <el-col :span="5">
                            <el-form-item label="公式项目名称">
                                <el-input
                                    clearable
                                    v-model="filterForm.data.description"
                                    placeholder="公式项目名称"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="5">
                            <el-form-item label="公式" label-width="100px">
                                <el-input
                                    clearable
                                    v-model.trim="filterForm.data.sourcecode"
                                    placeholder="公式"
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                          <el-col :span="5">
                            <el-form-item label="公式版本" label-width="120px">
                                <el-input
                                    clearable
                                    v-model.trim="filterForm.data.clock"
                                    placeholder="公式版本"
                                    style="width: 80%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                            <el-col :span="4">
                            <el-form-item label="类型" label-width="40px">
                                 <el-select
                                    filterable clearable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.tablename"
                                    placeholder="类型"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of tablename.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="box-top-gjl">
                    <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-form
                    size="mini"
                    ref="dialog_form_arr"
                    label-width="0px"
                    :model="tableFormDataObj"
                >
                    <el-table
                        stripe
                        id="singleTable"
                        ref="singleTable"
                        :data="tableData"
                        v-loading="tableLoading"
                        style="width: 100%;"
                        :height="singleTableHeight"
                        border
                        size="mini"
                        highlight-current-row
                        @selection-change="handleSelectionChange"
                        :span-method="objectSpanMethod"
                    >
                        <el-table-column
                            sortable
                            prop="code"
                            label="公式项目编号"
                            min-width="40px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                             <el-table-column
                            sortable
                            prop="clock"
                            label="公式版本"
                            min-width="20px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                         <el-table-column
                            sortable
                            prop="description"
                            label="公式项目名称"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="sourcecode"
                            label="公式"
                            min-width="140px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="tablename"
                            label="类型"
                            min-width="40px"
                            :show-overflow-tooltip="true"
                        >
                            <template slot-scope="scope">
                                <span>{{ tablename.obj[scope.row.tablename] ? tablename.obj[scope.row.tablename] : scope.row.tablename }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column
                            sortable
                            prop="revisor"
                            label="修改人"
                            width="120px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            sortable
                            prop="revisetime"
                            label="修改时间"
                            width="145px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                    </el-table>
                </el-form>
                <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>
    </div>
</template>

<script>
export default {
    name: 'equationMaintain',
    data () {
        return {
            multipleSelection: [],
            tableFormDataObj: {},
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    revisetime: '',
                    revisor: '',
                    code: '',
                    description: '',
                    sourcecode: '',
                    tablename: '',
                    clock: ''
                },
                rules: {

                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 100,
            tableData: [
                // {
                //     id: 'W',
                //     name: '水',
                //     remark: '',
                //     bcode: '',
                //     bdesc1: '',
                //     bdesc2: ''
                // }
            ],
            tablename: {
                obj: {
                    'T_RM_ACT_VALUE': '实绩表',
                    'T_RM_CALPOINT_VALUE': '计量点',
                    'T_RM_BALANCE_VALUE': '平衡表',
                    'T_RM_WORKPROC_PRODUCT_VALUE': '产品',
                    'T_RM_WORKPROC_MATERIAL_VALUE': '原材料',
                    'T_RM_INDEX_VALUE': '指标',
                    'T_RM_PLAN_VALUE': '计划',
                    'T_RM_ENERGY': '能源'
                },
                arr: [
                    {
                        id: 'T_RM_ACT_VALUE',
                        name: '实绩表'
                    },
                    {
                        id: 'T_RM_CALPOINT_VALUE',
                        name: '计量点'
                    },
                    {
                        id: 'T_RM_BALANCE_VALUE',
                        name: '平衡表'
                    },
                    {
                        id: 'T_RM_WORKPROC_PRODUCT_VALUE',
                        name: '产品'
                    },
                    {
                        id: 'T_RM_WORKPROC_MATERIAL_VALUE',
                        name: '原材料'
                    },
                     {
                        id: 'T_RM_INDEX_VALUE',
                        name: '指标'
                    },
                     {
                        id: 'T_RM_PLAN_VALUE',
                        name: '计划'
                    },
                       {
                        id: 'T_RM_ENERGY',
                        name: '能源'
                    }
                ]
            },
            loading: false,
            tableLoading: false
        }
    },
    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;
        },
        // 改变表格显示条数
        tableSizeChange (val) {
            let that = this;
            that.pageSize = val;
            that.getTableData(1);
        },
        // 重置搜索
        tableDataCancel () {
            let that = this;
            that.filterForm.data.id = '';
            that.filterForm.data.name = '';
            that.getTableData(1);
        },
        // 表格中多选按钮的相关方法
        handleSelectionChange (val) {
            let that = this;
            let tableFormDataObj = {};
            for (let item of that.tableData) {
                item.isSelection = false;
            }
            for (let item of val) {
                let obj = {};
                let xId = item.id;
                if (that.tableFormDataObj[xId]) {
                    tableFormDataObj[xId] = that.tableFormDataObj[xId];
                } else {
                    for (let key in item) {
                        obj[key] = item[key];
                    }
                    tableFormDataObj[xId] = obj;
                }
                item.isSelection = true;
            }
            that.tableFormDataObj = tableFormDataObj;
            that.multipleSelection = val;
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this,
                params = {
                    code: that.filterForm.data.code,
                    description: that.filterForm.data.description,
                    sourcecode: that.filterForm.data.sourcecode,
                    tablename: that.filterForm.data.tablename.toString(),
                    clock: that.filterForm.data.clock
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/formulas/getByData?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({
                                revisetime: item.REVISETIME,
                                revisor: item.REVISOR,
                                code: item.CODE,
                                description: item.DESCRIPTION,
                                sourcecode: item.SOURCECODE,
                                tablename: item.TABLENAME,
                                clock: item.CLOCK
                            })
                        }
                        that.tableData = arr;
                        that.total = res.data.total;
                    } else {
                        that.$message.error(res.message);
                    }
                    that.tableLoading = false;
                }).catch(function () {
                    that.tableLoading = false;
                }
                );
        }
    }
}
</script>

<style lang="less">
.equationMaintain {
    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;
            }
        }
        .el-table .el-table__row {
            height: 35px;
        }
        .el-table__body .el-form-item--mini.el-form-item {
            margin: 0px;
            .error {
                overflow: hidden;
                color: #f56c6c;
                font-size: 12px;
                line-height: 1;
            }
        }
    }
}
</style>
