<template>
<!-- 能源介质管网变更查询 -->
    <div class="tubeQuery">
        <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="12">
                            <el-form-item label="日期">
                                <el-date-picker
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    type="datetimerange"
                                    v-model="filterForm.data.creattime"
                                    placeholder=""
                                    style="width: 100%;"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="变更单编号">
                                <el-input clearable
                                    v-model="filterForm.data.changeno"
                                    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-select
                                    v-model="filterForm.data.mediumtype"
                                    placeholder=""
                                    style="width: 100%;"
                                >
                                    <el-option
                                        v-for="item of nameObj.mediumtype.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="12">
                            <el-form-item label="受理时间">
                                <el-date-picker
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    type="datetimerange"
                                    v-model="filterForm.data.changetime"
                                    placeholder=""
                                    style="width: 100%;"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="受理人">
                                <el-input clearable
                                    v-model="filterForm.data.changeman"
                                    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.respunit"
                                    placeholder=""
                                    style="width: 100%;"
                                    @keyup.enter.native="getTableData(1)"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="6">
                            <el-form-item label="单据状态">
                                <el-select
                                    v-model="filterForm.data.billstatus"
                                    placeholder=""
                                    style="width: 100%;"
                                    disabled
                                >
                                    <el-option
                                        v-for="item of nameObj.billstatus.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
                >
                    <el-table-column
                    sortable
                        prop="creattime"
                        label="日期"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="changeno"
                        label="变更单编号"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="mediumtype"
                        label="能介系统"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.mediumtype.obj[scope.row.mediumtype] ? nameObj.mediumtype.obj[scope.row.mediumtype] : scope.row.mediumtype }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                    sortable
                        prop="respunit"
                        label="责任单位"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="tel"
                        label="联系电话"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="changeman"
                        label="受理人"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="changetime"
                        label="受理时间"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                    sortable
                        prop="billstatus"
                        label="单据状态"
                        min-width="100px"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span>{{ nameObj.billstatus.obj[scope.row.billstatus] ? nameObj.billstatus.obj[scope.row.billstatus] : scope.row.billstatus }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        class-name="gn-TableDownloadExcel-none"
                        label="操作"
                        align="center"
                        width="70px"
                        fixed="right"
                    >
                        <template slot-scope="scope">
                            <div>
                                <el-button
                                    type="primary"
                                    size="mini"
                                    @click="but_edit(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="'管网变更单 - 查看'"
            :visible.sync="dialog.editorBox.show"
            width="940px"
            :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="90px"
                >
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="能介系统" prop="mediumtype">
                                <el-select
                                    filterable clearable
                                    v-model="dialog.editorBox.form.data.mediumtype"
                                    placeholder=""
                                    style="width: 100%;"
                                    disabled
                                >
                                    <el-option
                                        v-for="item of nameObj.mediumtype.arr"
                                        :key="item.id"
                                        :value="item.id"
                                        :label="item.name"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="变更单编号" prop="changeno">
                                <el-input clearable
                                    v-model="dialog.editorBox.form.data.changeno"
                                    placeholder=""
                                    style="width: 100%;"
                                    disabled
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="日期" prop="creattime">
                                <el-date-picker
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    type="datetime"
                                    v-model="dialog.editorBox.form.data.creattime"
                                    placeholder=""
                                    style="width: 100%;"
                                    disabled
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="责任单位" prop="respunit">
                                <el-input clearable
                                    v-model="dialog.editorBox.form.data.respunit"
                                    placeholder=""
                                    style="width: 100%;"
                                    disabled
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="联系电话" prop="tel">
                                <el-input clearable
                                    v-model="dialog.editorBox.form.data.tel"
                                    placeholder=""
                                    style="width: 100%;"
                                    disabled
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item label="变更原因" prop="changeaddress">
                        <el-input clearable
                            type="textarea"
                            v-model="dialog.editorBox.form.data.changeaddress"
                            placeholder=""
                            style="width: 100%;"
                            disabled
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="变更描述" prop="changedesc">
                        <el-input clearable
                            type="textarea"
                            v-model="dialog.editorBox.form.data.changedesc"
                            placeholder=""
                            style="width: 100%;"
                            disabled
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="note">
                        <el-input clearable
                            type="textarea"
                            v-model="dialog.editorBox.form.data.note"
                            placeholder=""
                            style="width: 100%;"
                            disabled
                        ></el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="16">
                            <el-form-item label="受理人" prop="changeman">
                                <el-input clearable
                                    v-model="dialog.editorBox.form.data.changeman"
                                    placeholder=""
                                    style="width: 100%;"
                                    disabled
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="受理时间" prop="changetime">
                                <el-date-picker
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    type="datetime"
                                    v-model="dialog.editorBox.form.data.changetime"
                                    placeholder=""
                                    style="width: 100%;"
                                    disabled
                                ></el-date-picker>
                            </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"
                >打 印</el-button> -->
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'tubeQuery',
    data () {
        return {
            activeMenu: '',
            filterForm: {
                show: true,
                data: {
                    creattime: '',
                    changeno: '',
                    mediumtype: '',
                    changetime: '',
                    changeman: '',
                    respunit: '',
                    billstatus: 'created'
                },
                rules: {

                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 100,
            tableData: [
                // {
                //     creattime: '2019-08-26 15:03:56',
                //     changeno: 'TFY_20190826_0001',
                //     mediumtype: '动力',
                //     respunit: '1',
                //     tel: '123233',
                //     changeman: '2',
                //     changetime: '2019-08-26 16:03:56',
                //     billstatus: '暂存'
                // }
            ],
            nameObj: {
                mediumtype: {
                    obj: {
                        'WaterChannel': '水道',
                        'PowerChannel': '动力'
                    },
                    arr: [
                        {
                            id: 'WaterChannel',
                            name: '水道'
                        },
                        {
                            id: 'PowerChannel',
                            name: '动力'
                        }
                    ]
                },
                billstatus: {
                    obj: {
                        'create': '暂存',
                        'created': '已提交'
                    },
                    arr: [
                        {
                            id: 'create',
                            name: '暂存'
                        },
                        {
                            id: 'created',
                            name: '已提交'
                        }
                    ]
                }
            },
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    type: '',
                    form: {
                        data: {
                            mediumtype: '',
                            changeno: '',
                            creattime: '',
                            respunit: '',
                            tel: '',
                            changeaddress: '',
                            changedesc: '',
                            note: '',
                            changeman: '',
                            changetime: ''
                        },
                        rules: {

                        }
                    }
                }
            }
        };
    },
    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 = {
                    creattime: that.filterForm.data.creattime[0],
                    creattimee: that.filterForm.data.creattime[1],
                    changeno: that.filterForm.data.changeno,
                    mediumtype: that.filterForm.data.mediumtype,
                    changetime: that.filterForm.data.changetime[0],
                    changetimee: that.filterForm.data.changetime[1],
                    changeman: that.filterForm.data.changeman,
                    respunit: that.filterForm.data.respunit,
                    billstatus: that.filterForm.data.billstatus
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/tcm0327s/?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({
                                changeno: item.changeno,
                                mediumtype: item.mediumtype,
                                creattime: item.creattime,
                                respunit: item.respunit,
                                tel: item.tel,
                                changeaddress: item.changeaddress,
                                changedesc: item.changedesc,
                                note: item.note,
                                changeman: item.changeman,
                                changetime: item.changetime,
                                billstatus: item.billstatus
                            })
                        }
                        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;
            for (let key in that.filterForm.data) {
                if (that.filterForm.data[key] instanceof Array) {
                    that.filterForm.data[key] = [];
                } else {
                    that.filterForm.data[key] = '';
                }
            }
            that.filterForm.data.billstatus = 'created'
            that.getTableData(1);
        },
        but_edit (row) {
            let that = this;
            for (let key in that.dialog.editorBox.form.data) {
                if (typeof row[key] !== 'undefined') {
                    that.dialog.editorBox.form.data[key] = row[key];
                } else {
                    console.log(key);
                }
            }
            that.dialog.editorBox.show = true;
        },
        dataSave () {
            let that = this;
            that.dialog.editorBox.show = false;
        }
    }
}
</script>

<style lang="less">
.tubeQuery {
    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;
            }
        }
    }
    .nr-textarea {
        .el-textarea__inner {
            min-height: 130px !important;
        }
    }
    .fileUploadBox {
        height: 130px;
        min-height: 130px !important;
        margin-bottom: 18px;
    }
}
</style>
