<template>
  <div class="bbsl-common-page">
    <!--查询-->
    <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
                size="mini"
                v-model="queryParam.params.stcd"
                :props="{value: 'stcd', label: 'name', children: 'children'}"
                :options="stnms"
                :show-all-levels="false"
                placeholder="请选择测站名称"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="预警等级">
              <el-select
                style="width: 100%"
                size="mini"
                v-model="queryParam.params.warnGrade"
                clearable
                placeholder="请选择预警等级"
              >
                <el-option
                  v-for="item in yjdjs"
                  :key="item.id"
                  :label="item.name"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="预警时间">
              <el-date-picker
                v-model="sendDate"
                style="width: 240px"
                size="mini"
                type="daterange"
                clearable
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <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-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!--表单-->
    <div class="bbsl-table-content" ref="contentPanel">
      <div class="bbsl-btn-content" ref="btnPanel">
        <el-button
          size="mini"
          icon="el-icon-plus"
          type="primary"
          @click="openFormDiao"
          >新增</el-button
        >
        <el-button
          :disabled="!multipleSelection.length"
          size="mini"
          icon="el-icon-delete"
          type="danger"
          @click="deleteSelect"
          >删除</el-button
        >
      </div>
      <el-table
        stripe
        size="mini"
        :height="tableMaxHeight"
        :style="{width: '100%'}"
        ref="multipleSelection"
        v-loading="loading"
        @row-dblclick="handleDbClick"
        border
        :row-key="getRowKey"
        :data="tableData"
         @selection-change="handleSelectionChange"
        :header-cell-style="{'text-align': 'center'}"
        :cell-style="{'text-align': 'center'}"
      >
        <el-table-column
          type="selection"
          width="55"
          :reserve-selection="true"
        ></el-table-column>
        <el-table-column
          fixed
          sortable
          prop="stnm"
          label="测站名称"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          fixed
          sortable
          width="200"
          prop="warnGrade"
          label="预警等级"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          fixed
          sortable
          prop="sendDate"
          label="预警时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          fixed
          sortable
          width="700"
          prop="alertContent"
          label="预警内容"
          show-overflow-tooltip
        ></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>
    <personFormDialog
      v-if="showFormDia"
      @closeFormDiao="closeFormDiao"
      @refList="getDateList"
      :showFormDia="showFormDia"
      :title="title"
      :pId="pId"
    ></personFormDialog>
  </div>
</template>

<script>
import personWarn from '@/api/warningRelease/personWarn'
import {TableMixin} from '@/mixins/tableMixin'
import personFormDialog from './components/personForm-dialog'
import req from '@/request.js'
export default {
  mixins: [TableMixin],
  components: {
    personFormDialog,
  },
  data() {
    return {
      queryParam: {
        params: {
          stcd: null, //测站代码
          warnGrade: null, //预警等级
        },
        pageBean: {
          pageSize: 20,
          page: 1,
        },
      },
      stnms: [], //测站名称
      yjdjs: [], //预警等级
      tableData: [], //展示的数据嘛
      sendDate: '', //预警时间
      total: 0, //总数据
      multipleSelection: [], // 删除复选框
      title: '',
      showFormDia: false,
      pId: '',
    }
  },
  created() {
    this.getSysDict() //获取字典数据
    this.getDateList() //获取数据
    //获取测站下拉列表
    personWarn.getTree({}, (response) => {
      this.stnms = response.value
      this.stnms.forEach((item) => {
        if (item.children) {
          item.children = JSON.parse(
            JSON.stringify(item.children).replace(/stnm/g, 'name')
          )
        }
      })
    })
  },
  // watch: {
  //   tableData: {
  //     handler(newVal) {
  //       this.stnms.forEach((item) => {
  //         if (item.children) {
  //           item.children.forEach((cc) => {
  //             newVal.forEach((ii) => {
  //               if (cc.parentId === ii['warnType'] && cc.stcd === ii['stcd']) {
  //                 ii['warnType'] = cc.name
  //               }
  //             })
  //           })
  //         }
  //       })
  //     },
  //   },
  // },
  methods: {
    //获取字典数据
    getSysDict() {
      let portal = window.context.portal
      return new Promise((resolve, reject) => {
        req.post(portal + '/sys/sysDictionary/v1/queryAllDictItems').then(
          (response) => {
            this.yjdjs = response.data.yjdj;
            this.getDateList()
          },
          (error) => {
            reject(error)
          }
        )
      })
    },
    //获取数据
    getDateList() {
      this.loading = true
      this.tableData = []
    
      if (this.sendDate) {
         let starTime = new Date(this.sendDate[0]);
         let endTime = new Date(this.sendDate[1]);
        
        this.queryParam.params.startTime =
          starTime.getFullYear() +
          '-' +
          (starTime.getMonth() + 1) +
          '-' +
          starTime.getDate()
       
        this.queryParam.params.endTime =
          endTime.getFullYear() +
          '-' +
          (endTime.getMonth() + 1) +
          '-' +
          endTime.getDate()
      }
      if(!this.queryParam.params.stcd){
        this.queryParam.params.stcd=null
      }
      personWarn.getListTable(this.queryParam, (res) => {
        if (res.state) {
          // console.log(res.value.rows)
          if (res.value.rows) {
            res.value.rows.forEach((item) => {
              item['warnGrade'].trim()
              this.yjdjs.forEach((ii) => {
                if (ii.value == item['warnGrade'].trim()) {
                  item['warnGrade'] = ii.name
                }
              })
            })
          }

          this.tableData = res.value.rows
            // console.log( this.tableData)
          this.$set(this.tableData, this.tableData)
          this.total = res.value.total
          this.loading = false
        } else {
          this.$message(res.message)
        }
      })
    },
    handleChange(children) {
      this.queryParam.params.stcd = children[1]
      // console.log(this.queryParam.params.stcd)
    },
    // // 点击查询
    onClickSearch() {
      this.queryParam.pageBean.page = 1
      this.getDateList()
    },
    // 重置
    resetQuery() {
      this.queryParam.params= {};
      this.sendDate = ''
      this.onClickSearch()
    },
    handleSizeChange(val) {
      this.queryParam.pageBean.pageSize = val
      this.getDateList()
    },
    handleCurrentChange(val) {
      this.queryParam.pageBean.page = val
      this.getDateList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => {
        return item.id
      })
    },
    // // 批量删除
    deleteSelect() {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning',
      })
        .then(() => {
          personWarn
            .remove(this.multipleSelection, (res) => {
              if (res.state) {
                this.$notify({
                  type: 'success',
                  message: '删除成功',
                  duration: 2000,
                  // offset: 80
                })
                // this.multipleSelection = [];
                this.$refs.multipleSelection.clearSelection()
                this.queryParam.pageBean.page = 1
                this.getDateList()
              }
            })
            .catch((res) => {
              this.$notify({
                type: 'error',
                message: res.message,
                duration: 2000,
                offset: 80,
              })
              this.getDateList()
            })
        })
        .catch(() => {})
    },
    openFormDiao() {
      this.title = '新增'
      this.showFormDia = true
    },
    // // 修改
    handleUpdate(row) {
      this.title = '修改'
      this.pId = row.id
      this.showFormDia = true
      //   console.log(row)
    },
    closeFormDiao() {
      this.showFormDia = false
      this.getDateList()
    },
    // // 指定一个key标识这一行的数据
    getRowKey(row) {
      return row.id
    },
  },
}
</script>

<style>
.el-input--small .el-input__inner {
  height: 28px;
}
</style>