<template>
    <!-- 指标数据查询 -->
    <div class="indexData">
        <div class="box">
            <div class="box-top">
                <el-form
                    style="overflow: hidden;"
                    :style="filterForm.show ? null : { height: '0px' }"
                    size="mini"
                    label-width="70px"
                >
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="时间粒度">
                                <el-select
                                    filterable clearable
                                    v-model="filterForm.data.timegranid"
                                    placeholder=""
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.timegranid.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="时间">
                                <!-- <zj-timegran-date
                                    v-model="filterForm.data.clock"
                                    style="width: 100%;"
                                    :type="'daterange'"
                                    :timegranId="filterForm.data.timegranid"
                                    :oneFoo="getTableData"
                                    :defaultOffset="-1"
                                ></zj-timegran-date> -->
                                <el-date-picker
                                    v-model="filterForm.data.clock"
                                    style="width: 100%;"
                                    :type="filterForm.data.timegranid === 'DAY' ? 'date' : filterForm.data.timegranid.toLowerCase()"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="工序">
                                <el-select
                                    filterable clearable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.workprocid"
                                    placeholder=""
                                    :disabled="filterForm.disabled.workprocid"
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.workprocid.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-select
                                    filterable clearable
                                    multiple
                                    collapse-tags
                                    v-model="filterForm.data.energyid"
                                    placeholder=""
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.energyid.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="指标类型" prop="indextype">
                                <el-select
                                    filterable clearable
                                    multiple
                                    collapse-tags
                                    style="width: 100%;"
                                    v-model="filterForm.data.indextype"
                                >
                                    <el-option
                                        v-for="item of nameObj.indextype.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-table
                    stripe
                    id="singleTable"
                    ref="singleTable"
                    :data="tableData"
                    v-loading="tableLoading"
                    style="width: 100%;"
                    :height="singleTableHeight"
                    border
                    size="mini"
                    highlight-current-row
                    show-summary
                    :summary-method="getSummaries">
                    <el-table-column type="index" align="center" label="序号" width="40px"></el-table-column>
                    <el-table-column
                        sortable
                        prop="timegranid"
                        label="时间粒度"
                        width="75px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.timegranid.obj[scope.row.timegranid] ? nameObj.timegranid.obj[scope.row.timegranid] : scope.row.timegranid }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                    sortable
                        prop="clock"
                        label="时间"
                        width="78px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        sortable
                        prop="indextype"
                        label="指标类型"
                        width="110px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.indextype.obj[scope.row.indextype] ? nameObj.indextype.obj[scope.row.indextype] : scope.row.indextype }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="workprocid"
                        label="工序"
                        width="120px"
                        :show-overflow-tooltip="true"
                        :sort-by="function(row, index) { return sortFoo(row, index, 'workprocid') }"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.workprocid.obj[scope.row.workprocid] ? nameObj.workprocid.obj[scope.row.workprocid] : scope.row.workprocid }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="energyid"
                        label="能介"
                        width="85px"
                        :show-overflow-tooltip="true"
                        :sort-by="function(row, index) { return sortFoo(row, index, 'energyid') }"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.energyid.obj[scope.row.energyid] ? nameObj.energyid.obj[scope.row.energyid] : scope.row.energyid }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="itemname"
                        label="实绩项目名称"
                        min-width="235px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        sortable
                        prop="cnsj"
                        label="厂内实绩"
                        min-width="120px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        sortable
                        prop="cnbz"
                        label="厂内标准"
                        min-width="120px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span v-if="scope.row.cnbz === 0 || scope.row.cnbz"
                            :class="{color_w: scope.row.cnsj > scope.row.cnbzwv}">
                                {{scope.row.cnbz}}
                            </span>
                            <span v-else>-</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="gjbz"
                        label="国家标准"
                        min-width="120px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span v-if="scope.row.gjbz === 0 || scope.row.gjbz"
                            :class="{color_w: scope.row.cnsj > scope.row.gjbzwv}">
                                {{scope.row.gjbz}}
                            </span>
                            <span v-else>-</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="hybz"
                        label="行业标准"
                        min-width="120px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span v-if="scope.row.hybz === 0 || scope.row.hybz"
                            :class="{color_w: scope.row.cnsj > scope.row.hybzwv}">
                                {{scope.row.hybz}}
                            </span>
                            <span v-else>-</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="factoryid"
                        label="行准工厂"
                        width="90px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.factoryid.obj[scope.row.factoryid] ? nameObj.factoryid.obj[scope.row.factoryid] : '-' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        sortable
                        prop="unitid"
                        label="单位"
                        width="99px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.unitid.obj[scope.row.unitid] ? nameObj.unitid.obj[scope.row.unitid] : scope.row.unitid }}</span>
                        </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>
    </div>
</template>

<script>
import { zCheckNumber1 } from '~/utils/validator.js'
import store from '@/store/index.js';
import timegranDate from '~/components/zg/timegranDate.vue'
import {formatDate, XtcommonSummaries} from '@/utils/util.js';
export default {
    name: 'indexData',
    components: {
        'zj-timegran-date': timegranDate
    },
    data () {
        let start = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1);
        return {
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    timegranid: 'DAY',
                    clock: formatDate(start, 'yyyy-MM-dd'),
                    workprocid: [],
                    energyid: [],
                    propertyid: '',
                    itemid: '',
                    itemname: '',
                    indextype: []
                },
                rules: {

                },
                disabled: {
                    workprocid: false
                }
            },
            tableFormRules: {
                numbers: [
                    { validator: zCheckNumber1, trigger: 'change' }
                ]
            },
            pageNum: 1,
            pageSize: 50,
            total: 0,
            singleTableHeight: 100,
            tableData: [],
            nameObj: {
                timegranid: {
                    obj: {'DAY': '日', 'MONTH': '月', 'YEAR': '年'},
                    arr: [{id: 'DAY', name: '日'}, {id: 'MONTH', name: '月'}, {id: 'YEAR', name: '年'}]
                },
                workprocid: {
                    obj: {},
                    arr: []
                },
                energyid: {
                    obj: {},
                    arr: []
                },
                propertyid: {
                    obj: {},
                    arr: []
                },
                unitid: {
                    obj: {},
                    arr: []
                },
                indextype: {
                    obj: {},
                    arr: []
                },
                factoryid: {
                    obj: {},
                    arr: []
                }
            },
            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.store.dispatch('getUserInfo').then((res) => {
        //     that.userInfo.data = res.data;
        // });
        // 接收路由参数
        if (that.$route.query.workprocid) {
            that.filterForm.disabled.workprocid = true;
            that.filterForm.data.workprocid = that.$route.query.workprocid.split(',');
        }
        that.getWorkprocIdData();
        that.getEnergyIdData();
        that.getPropertyIdData();
        that.getUnitIdData();
        that.getFactoryIdData();
        that.getType();
        // that.getTableData();
    },
    methods: {
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 用于表格特殊列的过滤方法（:sort-by="function(row, index) { return sortFoo() }"）
        sortFoo (row, index, objName, name) {
            name = name || objName;
            let s = this.nameObj[objName].obj[row[name]] ? this.nameObj[objName].obj[row[name]] : row[name];
            return s;
        },
        // 自定义的表格合计方法：只给指定列进行合计
        getSummaries (param) {
            const prop = []; // 合计列绑定的prop
            return XtcommonSummaries(param, prop);
        },
        // 获取工序(指标)
        getWorkprocIdData () {
            let that = this
            let url = 'pass/ems/v1/trmworkprocs/getIdAndName?isreal=1';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.id,
                                name: item.name
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.workprocid.arr = arr;
                        that.nameObj.workprocid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取能介
        getEnergyIdData () {
            let that = this
            let url = 'pass/ems/v1/trmenergys/selectNameAndId/';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.id,
                                name: item.name
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.energyid.arr = arr;
                        that.nameObj.energyid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取属性
        getPropertyIdData () {
            let that = this
            let url = 'pass/ems/v1/trmenergypropertys/getidandname/';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.id,
                                name: item.name
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.propertyid.arr = arr;
                        that.nameObj.propertyid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取标准计量单位
        getUnitIdData () {
            let that = this
            let url = 'pass/ems/v1/trmunits/getIdAndName';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.id,
                                name: item.name
                            })
                            obj[item.id] = item.name;
                            if (item.name === '吨') {
                                that.nameObj.unitid.defaults = that.nameObj.unitid.defaults ? that.nameObj.unitid.defaults : item.id;
                            }
                        }
                        that.nameObj.unitid.arr = arr;
                        that.nameObj.unitid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this;
            let clock;
            if (!that.filterForm.data.clock) {
                that.$message.error('请选择日期');
            }
            if (that.filterForm.data.timegranid === 'MONTH') {
                clock = formatDate(that.filterForm.data.clock, 'yyyy-MM');
            } else if (that.filterForm.data.timegranid === 'YEAR') {
                clock = formatDate(that.filterForm.data.clock, 'yyyy');
            } else {
                clock = formatDate(that.filterForm.data.clock, 'yyyy-MM-dd');
            }
            let params = {
                    timegranid: that.filterForm.data.timegranid,
                    clock: clock,
                    workprocid: that.filterForm.data.workprocid.toString(),
                    energyid: that.filterForm.data.energyid.toString(),
                    itemid: that.filterForm.data.itemid,
                    indextype: that.filterForm.data.indextype.toString()
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/trmindexvalues/getIndexData/?pageNum=' + that.pageNum + '&pageSize=' + that.pageSize;
            that.axios.get(url, {
                params: params
            })
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        for (let i = 0; i < res.data.list.length; i++) {
                            let item = res.data.list[i];
                            arr.push({
                                seq: i,
                                itemid: item.ITEMID,
                                clock: item.CLOCK,
                                timegranid: item.TIMEGRANID,
                                itemname: item.ITEMNAME,
                                unitid: item.UNITID,
                                cnsj: item.CNSJ,
                                cnbz: item.CNBZ,
                                cnbzwv: item.CNBZWV,
                                gjbz: item.GJBZ,
                                gjbzwv: item.GJBZWV,
                                hybz: item.HYBZ,
                                hybzwv: item.HYBZWV,
                                workprocid: item.WORKPROCID,
                                energyid: item.ENERGYID,
                                factoryid: item.FACTORYID,
                                indextype: item.INDEXTYPE
                            })
                        }
                        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.timegranid = 'DAY';
            that.filterForm.data.clock = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1);
            that.filterForm.data.workprocid = [];
            that.filterForm.data.energyid = [];
            that.filterForm.data.indextype = [];
            that.filterForm.data.propertyid = '';
            that.filterForm.data.itemid = '';
            that.filterForm.data.itemname = '';
            // that.getTableData(1);
        },
        // 分类获取所有指标类型
        getType () {
            let that = this
            let url = 'pass/ems/v1/trmindextypes/?pageSize=1000&indexcol=INDEXTYPE';
            that.axios.get(url).then(function (res) {
                if (res.code === '0') {
                    let arr = [];
                    let obj = {};
                    for (let item of res.data.list) {
                        arr.push({
                            id: item.indexid,
                            name: item.indexname
                        })
                        obj[item.indexid] = item.indexname;
                    }
                    that.nameObj.indextype.arr = arr;
                    that.nameObj.indextype.obj = obj;
                } else {
                    that.$message.error(res.message);
                }
            });
        },
        // 获取工厂
        getFactoryIdData () {
            let that = this
            let url = 'pass/ems/v1/trmfactorys/getIAndName';
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        let obj = {};
                        for (let item of res.data) {
                            arr.push({
                                id: item.id,
                                name: item.name
                            })
                            obj[item.id] = item.name;
                        }
                        that.nameObj.factoryid.arr = arr;
                        that.nameObj.factoryid.obj = obj;
                    } else {
                        that.$message.error(res.message);
                    }
                });
        }
    }
}
</script>

<style lang="less">
.indexData {
    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;
                padding: 7px 0;
                border-top: 1px solid #ccc;
                // margin-bottom: 6px;
            }
        }
    }
    .el-table__body .el-form-item--mini.el-form-item {
        margin: 0px;
        .error {
            overflow: hidden;
            color: #f56c6c;
            font-size: 12px;
            line-height: 1;
        }
    }
    .el-table__body input {
        font-weight: normal;
        padding-left: 8px;
        padding-right: 25px;
    }
    .color_w {
        color: red;
    }
}
</style>
