<template>
    <div class="bbsl-common-page" @click="commonPageClick">
        <div class="bbsl-table-search">
            <el-form v-model="queryParam" :inline="true">
                <el-row :gutter="10">
                    <el-col :span="6">
                        <el-form-item label="行政区划">
                            <el-cascader
                                clearable
                                filterable
                                size="mini"
                                :options="basAdNames"
                                v-model="queryParam.params.addvcd"
                                :props="setKesLabel"
                                @change="onhouChange"
                            >
                                <template slot-scope="{node, data}">
                                    <span>{{ data.name }}</span>
                                    <span v-if="!node.isLeaf">
                                        ({{
                                            data.children === []
                                                ? ''
                                                : data.children.length
                                        }})
                                    </span>
                                </template>
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="测点名称">
                            <el-input
                                size="mini"
                                clearable
                                placeholder="请输入测点名称"
                                v-model="queryParam.params.stnm"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="测站类型">
                            <el-select
                                size="mini"
                                filterable
                                v-model="queryParam.params.sstp"
                                clearable
                                placeholder="请选择测站类型"
                            >
                                <el-option
                                    v-for="item in sstpList"
                                    :key="item.value"
                                    :label="item.name"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="6">
                        <el-form-item>
                            <el-button
                                type="primary"
                                icon="el-icon-search"
                                size="mini"
                                @click="onClickSearch"
                                >搜索
                            </el-button>
                            <el-button
                                icon="el-icon-refresh"
                                size="mini"
                                @click="resetQuery"
                                >重置</el-button
                            >
                            <el-button
                                size="mini"
                                icon="el-icon-download"
                                type="warning"
                                @click="updateExcl"
                                >导出</el-button
                            >
                            <el-button type="text" @click.stop="openMore"
                                >更多
                                <i
                                    :class="
                                        showMore
                                            ? 'el-icon-arrow-up'
                                            : 'el-icon-arrow-down'
                                    "
                                ></i>
                            </el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
                <div
                    class="bbsl-search-more"
                    style="width: 300px"
                    v-if="showMore"
                >
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="测点">
                                <el-select
                                    size="mini"
                                    filterable
                                    v-model="queryParam.params.stcd"
                                    clearable
                                    placeholder="请选择测点"
                                >
                                    <el-option
                                        v-for="item in hysts"
                                        :key="item.id"
                                        :label="item.stnm"
                                        :value="item.stcd"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
            </el-form>
        </div>
        <div class="bbsl-table-content" ref="contentPanel">
            <div class="bbsl-btn-content" ref="btnPanel"></div>
            <el-table
                stripe
                size="mini"
                :height="tableMaxHeight"
                :style="{width: '100%'}"
                v-loading="loading"
                border
                :data="tableData"
                :header-cell-style="{'text-align': 'center'}"
                :cell-style="{'text-align': 'center'}"
            >
                <el-table-column
                    type="index"
                    width="55"
                    fixed
                    label="序号"
                ></el-table-column>
                <el-table-column
                    min-width="110"
                    sortable
                    fixed
                    prop="stcd"
                    label="测站编码"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    min-width="130"
                    sortable
                    fixed
                    prop="stnm"
                    label="测站名称"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    min-width="140"
                    sortable
                    fixed
                    prop="tm"
                    label="上报时间"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    min-width="180"
                    sortable
                    prop="stlc"
                    label="测站地址"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    min-width="150"
                    sortable
                    prop="z"
                    label="水位(m)"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    min-width="150"
                    sortable
                    prop="q"
                    label="流量(m³/s)"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    min-width="150"
                    sortable
                    prop="fsltdz"
                    label="生态流量核定值(m³/s)"
                    show-overflow-tooltip
                >
                </el-table-column>

                <el-table-column label="操作" fixed="right" min-width="100">
                    <template slot-scope="scope">
                        <div>
                            <el-button
                                size="mini"
                                plain
                                label="操作"
                                @click="relationUpdate(scope.row)"
                                >流量曲线</el-button
                            >
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                ref="paginationPanel"
                align="right"
                class="bbsl-pagination"
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryParam.pageBean.page"
                :page-sizes="[20, 30, 40, 50]"
                :page-size="queryParam.pageBean.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            ></el-pagination>
        </div>
        <el-dialog
            :title="this.title + '曲线图'"
            width="80%"
            top="8vh"
            :before-close="handleClose"
            :visible.sync="showFormDias"
        >
            <div class="serchBlock" style="margin: 0 0 10px 0">
                <span>选择日期：</span>
                <el-date-picker
                    style="width: 350px"
                    size="mini"
                    @change="changeChart"
                    v-model="times"
                    format="yyyy-MM-dd HH:mm:ss"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :clearable="false"
                ></el-date-picker>
            </div>
            <div style="width: 100%; height: 500px; display: flex">
                <el-table
                    style="width: 45%"
                    :data="echartLiat"
                    v-loading="loadingChartData"
                    stripe
                    height="500"
                >
                    <el-table-column label="序号" type="index" align="center" />
                    <el-table-column
                        label="时间"
                        prop="tm"
                        align="center"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="流量"
                        prop="q"
                        align="center"
                        width="120"
                        show-overflow-tooltip
                    >
                    </el-table-column>
                    <el-table-column
                        label="水位"
                        prop="zz"
                        align="center"
                        width="120"
                        show-overflow-tooltip
                    >
                    </el-table-column>
                </el-table>
                <div
                    id="echartsBox"
                    style="width: 55%; height: 500px"
                    ref="box"
                ></div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleClose">退 出</el-button>
            </span>
        </el-dialog>
        <sdzDerivedData
            ref="Time"
            :sstp="queryParam.params.sstp"
        ></sdzDerivedData>
    </div>
</template>
<script>
import sdzDerivedData from './components/sdzDerivedData.vue'
import sdzQuery from '@/api/monitor/otherMonitor/sdzQuery.js'
import req from '@/request.js'
import * as echarts from 'echarts'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import {TableMixin} from '@/mixins/tableMixin'
export default {
    name: 'sdzQuery',
    components: {
        // FormDialog,
        // hysRelation
        sdzDerivedData,
    },
    mixins: [TableMixin],
    data() {
        return {
            title: '',
            loading: false,
            queryParam: {
                pageBean: {
                    pageSize: 20,
                    page: 1,
                },
                params: {
                    sstp: null,
                    stcd: null, //水电站名称
                    addvcd: null, //水电站类型（参考字典）
                },
            },
            showFormDias: false,
            total: 0,
            tableData: [],
            times: [],
            echartLiat: [],
            hysts: [], //水电站名称
            engScals: [], //工程规模
            hystTypes: [], //水电站类型
            basAdNames: [], //行政区
            setKesLabel: {
                value: 'code',
                label: 'name',
                children: 'children',
            },
            timeValue: [],
            sstpList: [],
            query: {
                stcd: null,
                stnm: null,
                startTime: null,
                endTime: null,
            },
            timeList: [],
            zzList: [],
            qList: [],
            jzList: [],
            dzList: [],
            showMore: false,
            StationId: '',
            loadingChartData: false,
        }
    },
    created() {
        this.StationId = window.sessionStorage.getItem('StationId')
        this.sstpList = dictUtils.getDictItemsFromCache('czlb-stzlx')
        this.getNames()
        if (
            this.StationId != '' &&
            this.StationId != null &&
            this.StationId != undefined
        ) {
            this.queryParam.params.stnm = this.StationId
            this.queryParam.pageBean.page = 1
            window.sessionStorage.removeItem('StationId')
        } else {
            this.getSysDict()
        }
        this.getDateList()
        sdzQuery.getbasAdName({}, (res) => {
            this.basAdNames = res.value
        })
        // this.query.startTime = null //每次打开弹窗时清空上次手动选择的时间
        // this.query.endTime = null //每次打开弹窗时清空上次手动选择的时间
        // this.times = [] //每次打开弹窗时清空上次手动选择的时间
        // var moment = require('moment') //打开时默认时间为一天前至获取当前年月日时分秒
        // var _today = moment()
        // let endTime = moment().format('L') //当前年月日
        // let time = moment().format('LTS') //当前时分秒
        // endTime = endTime.replace(/\//g, '-')
        // endTime = endTime + ' ' + time
        // let startTime = _today
        //   .subtract(1, 'days')
        //   .format('YYYY-MM-DD') /*前一天的时间*/
        // 			console.log(startTime);
        // startTime = startTime + ' ' + time
        // this.query.startTime = startTime
        // this.query.endTime = endTime
        // this.times=[startTime,endTime]
        // console.log(this.times);
    },
    methods: {
        // 导出表格弹窗显示
        updateExcl() {
            this.$refs.Time.openShare()
        },
        openMore() {
            this.showMore = !this.showMore
        },
        changeChart(val) {
            this.echartLiat = []
            this.loadingChartData = true
            if (val) {
                this.query.startTime = val[0]
                this.query.endTime = val[1]
                this.timeList = []
                this.zzList = []
                this.qList = []
                this.jzList = []
                this.dzList = []
                sdzQuery.getChartsList(this.query, (res) => {
                    if (res.value.length) {
                        this.echartLiat = res.value
                        this.echartLiat.forEach((e) => {
                            this.timeList.push(e.tm)
                            this.zzList.push(e.zz)
                            this.qList.push(e.q)
                            this.jzList.push(e.jz)
                            this.dzList.push(e.dz)
                        })
                        // this.dates = this.echartLiat.map(item => {
                        //   return item.tm
                        // })
                        // this.dangerdz = this.echartLiat.map(item => {
                        //   return item.dz
                        // })
                        // this.dangerjz = this.echartLiat.map(item => {
                        //   return item.jz
                        // })
                        // this.dangerzs = this.echartLiat.map(item => {
                        //   return item.zz
                        // })
                        // this.dangerQ = this.echartLiat.map(item => {
                        //   return item.q
                        // })
                    }
                    this.$nextTick(() => {
                        this.getCharts()
                    })
                    this.loadingChartData = false
                })
            } else {
                this.$message.warning('请选择查询时间范围！')
                this.$nextTick(() => {
                    this.getCharts()
                })
                this.loadingChartData = false
                this.query.startTime = ''
                this.query.endTime = ''
                this.times = []
                this.timeList = []
                this.zzList = []
                this.qList = []
                this.jzList = []
                this.dzList = []
                return
                sdzQuery.getChartsList(this.query, (res) => {
                    if (res.value.length) {
                        this.echartLiat = res.value
                        this.echartLiat.forEach((e) => {
                            this.timeList.push(e.tm)
                            this.zzList.push(e.zz)
                            this.qList.push(e.q)
                            this.jzList.push(e.jz)
                            this.dzList.push(e.dz)
                        })
                        // this.dates = this.echartLiat.map(item => {
                        //   return item.tm
                        // })
                        // this.dangerdz = this.echartLiat.map(item => {
                        //   return item.dz
                        // })
                        // this.dangerjz = this.echartLiat.map(item => {
                        //   return item.jz
                        // })
                        // this.dangerzs = this.echartLiat.map(item => {
                        //   return item.zz
                        // })
                        // this.dangerQ = this.echartLiat.map(item => {
                        //   return item.q
                        // })
                        this.$nextTick(() => {
                            this.getCharts()
                        })
                    }
                    this.loadingChartData = false
                })
            }
        },
        handleClose() {
            this.echartLiat = []
            this.showFormDias = false
        },
        onhouChange(val) {
            if (val) {
                this.queryParam.params.addvcd = val[val.length - 1]
            } else {
                this.queryParam.params.addvcd = null
            }
        },
        relationUpdate(val) {
            this.echartLiat = []
            this.loadingChartData = true
            this.title = val.stnm
            this.query.startTime = null //每次打开弹窗时清空上次手动选择的时间
            this.query.endTime = null //每次打开弹窗时清空上次手动选择的时间
            // this.times = [] //每次打开弹窗时清空上次手动选择的时间
            this.query.stcd = val.stcd //点击打开曲线图时赋值
            this.query.stnm = val.stnm //点击打开曲线图时赋值
            var moment = require('moment') //打开时默认时间为一天前至获取当前年月日时分秒
            var _today = moment()
            this.showFormDias = true
            let endTime = moment().format('L') //当前年月日
            let time = moment().format('LTS') //当前时分秒
            endTime = endTime.replace(/\//g, '-')
            endTime = endTime + ' ' + time
            let startTime = _today
                .subtract(1, 'days')
                .format('YYYY-MM-DD') /*前一天的时间*/
            startTime = startTime + ' ' + time
            this.query.startTime = startTime
            this.query.endTime = endTime
            this.times = [startTime, endTime]
            let data = {
                query: {
                    stnm: val.stnm,
                    stcd: val.stcd,
                    startTime: startTime,
                    endTime: endTime,
                    sttp: val.sttp,
                },
            }
            this.timeList = []
            this.zzList = []
            this.qList = []
            this.jzList = []
            this.dzList = []
            sdzQuery.getChartsList(data.query, (res) => {
                this.echartLiat = res.value
                console.log(res.value)
                if (res.value.length) {
                    this.echartLiat.forEach((e) => {
                        this.timeList.push(e.tm)
                        this.zzList.push(e.zz)
                        this.qList.push(e.q)
                        this.jzList.push(e.jz)
                        this.dzList.push(e.dz)
                    })
                    // this.dates = this.echartLiat.map(item => {
                    //   return item.tm
                    // })
                    // this.dangerdz = this.echartLiat.map(item => {
                    //   return item.dz
                    // })
                    // this.dangerjz = this.echartLiat.map(item => {
                    //   return item.jz
                    // })
                    // this.dangerzs = this.echartLiat.map(item => {
                    //   return item.zz
                    // })
                    // this.dangerQ = this.echartLiat.map(item => {
                    //   return item.q
                    // })
                    this.$nextTick(() => {
                        this.getCharts()
                    })
                } else {
                    this.timeList = []
                    this.zzList = []
                    this.qList = []
                    this.jzList = []
                    this.dzList = []
                    this.$nextTick(() => {
                        this.getCharts()
                    })
                }
                this.loadingChartData = false
            })
            //打开关联
        },
        getNames() {
            sdzQuery.getTshy({}, (res) => {
                this.hysts = res.value
            })
        },
        getCharts() {
            let chartDom = document.getElementById('echartsBox')
            let myChart = echarts.init(chartDom)
            let option = {
                title: {
                    text: '',
                },
                tooltip: {
                    trigger: 'axis',
                },
                legend: {
                    data: [
                        '流量(m³/s)',
                        '当前水位(m)',
                        '警戒流量阈值(m³/s)',
                        '危险流量阈值(m³/s)',
                    ],
                },
                grid: {
                    left: '8%',
                    right: '4%',
                    bottom: '6%',
                    containLabel: true,
                },
                // toolbox: {
                //   feature: {
                //     saveAsImage: {},
                //   },
                // },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: this.timeList,
                },
                yAxis: {
                    type: 'value',
                    scale: true,
                },
                series: [
                    {
                        name: '流量(m³/s)',
                        type: 'line',
                        smooth: true,
                        emphasis: {
                            focus: 'series',
                        },
                        data: this.qList,
                        itemStyle: {
                            color: 'green',
                        },
                    },
                    {
                        name: '当前水位(m)',
                        type: 'line',
                        smooth: true,
                        emphasis: {
                            focus: 'series',
                        },
                        data: this.zzList,
                        itemStyle: {
                            color: '#0091ff',
                        },
                    },
                    {
                        name: '警戒流量阈值(m³/s)',
                        type: 'line',
                        smooth: true,
                        emphasis: {
                            focus: 'series',
                        },
                        data: this.jzList,
                        itemStyle: {
                            color: 'yellow',
                        },
                    },
                    {
                        name: '危险流量阈值(m³/s)',
                        type: 'line',
                        smooth: true,
                        emphasis: {
                            focus: 'series',
                        },
                        data: this.dzList,
                        itemStyle: {
                            color: 'red',
                        },
                    },
                ],
            }
            option && myChart.setOption(option)
        },
        //获取字典数据
        getSysDict() {
            let portal = window.context.portal
            return new Promise((resolve, reject) => {
                req.post(
                    portal + '/sys/sysDictionary/v1/queryAllDictItems'
                ).then(
                    (response) => {
                        resolve(response.data)
                        this.hystTypes = response.data.sdzlx //水电站类型
                        this.engScals = response.data.gcgm //工程规模
                        this.getDateList()
                    },
                    (error) => {
                        reject(error)
                    }
                )
            })
        },
        getDateList() {
            this.loading = true
            this.tableData = []
            sdzQuery.ListGet(this.queryParam, (res) => {
                this.total = res.value.total
                for (let i = 0; i < this.hystTypes.length; i++) {
                    for (let j = 0; j < res.value.rows.length; j++) {
                        if (
                            this.hystTypes[i].value ===
                            res.value.rows[j].hystType
                        ) {
                            res.value.rows[j].hystTypeName =
                                this.hystTypes[i].name
                        } else {
                        }
                    }
                }
                for (let i = 0; i < this.engScals.length; i++) {
                    for (let j = 0; j < res.value.rows.length; j++) {
                        if (
                            this.engScals[i].value === res.value.rows[j].engScal
                        ) {
                            res.value.rows[j].engScalName =
                                this.engScals[i].name
                        } else {
                        }
                    }
                }
                this.tableData = res.value.rows
                this.loading = false
            })
        },
        handleSizeChange(val) {
            this.queryParam.pageBean.pageSize = val
            this.getDateList()
        },
        handleCurrentChange(val) {
            this.queryParam.pageBean.page = val
            this.getDateList()
        },
        // // 点击查询
        onClickSearch() {
            this.queryParam.pageBean.page = 1
            this.getDateList()
        },
        // 重置
        resetQuery() {
            this.queryParam.params = {}
            this.getDateList()
        },
    },
}
</script>
<style lang="scss" scoped>
.btn-content {
  display: flex;
}

.el-select-dropdown__item span {
  color: rgb(51, 51, 51) !important;
}

.el-select-dropdown__item {
  color: #9a9a9a !important;
}

.el-select-dropdown__item.hover,
.el-select-dropdown__item:hover {
  background: #f5f7fa;
}

.btn-content :first-child {
  margin: 0 0 0 0 !important;
}

// ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
//   width: 6px;
//   height: 15px;
// }
.sampling {
  ::v-deep .el-dialog__body {
    padding: 30px 20px 0 20px;
  }
}

::-webkit-scrollbar {
  background: #fff !important;
}

.bbsl-pagination {
  bottom: 10px !important;
}
</style>
