<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-input clearable
                        size="mini"
                        placeholder="请输入堤防名称"
                        v-model="queryParam.params.dikeName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="堤防级别">
              <el-select @change="changedikeGrad"
                         size="mini"
                         v-model="queryParam.params.dikeGrad"
                         clearable
                         placeholder="请选择堤防级别">
                <el-option v-for="item in dikeGrads"
                           :key="item.id"
                           :label="item.name"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="6">
            <el-form-item label="堤防代码">
              <el-input
                size="mini"
                clearable
                placeholder="请输入堤防代码"
                v-model="queryParam.params.dikeCode"
              ></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="6">
            <el-form-item label="堤防类型">
              <el-select @change="changedikeType"
                         size="mini"
                         v-model="queryParam.params.dikeType"
                         clearable
                         placeholder="请选择堤防类型">
                <el-option v-for="item in dikeTypes"
                           :key="item.id"
                           :label="item.name"
                           :value="item.value"></el-option>
              </el-select>
            </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-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: 250px"
             v-if="showMore">
          <el-row>
          </el-row>
        </div>
      </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"
                @row-dblclick="handleDbClick"
                :style="{width: '100%'}"
                ref="multipleSelection"
                v-loading="loading"
                border
                :data="tableData"
                :row-key="getRowKey"
                @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 type="index"
                         width="55"
                         label="序号"
                         fixed></el-table-column>
        <el-table-column min-width="80"
                         sortable
                         prop="dikeCode"
                         label="堤防代码"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column min-width="160"
                         sortable
                         prop="dikeName"
                         label="堤防名称"
                         show-overflow-tooltip></el-table-column>

        <el-table-column min-width="140"
                         sortable
                         prop="dikeTypeName"
                         label="堤防类型"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column min-width="160"
                         sortable
                         prop="dikeLen"
                         label="堤防长度(m)"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column min-width="160"
                         sortable
                         prop="dikePatt"
                         label="堤防型式"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column min-width="160"
                         sortable
                         prop="dikeGrad"
                         label="堤防级别"
                         show-overflow-tooltip>
        </el-table-column>
        <!-- <el-table-column
          min-width="150"
          sortable
          prop="compDate"
          label="建成时间"
          show-overflow-tooltip
        ></el-table-column> -->
        <el-table-column label="操作"
                         min-width="110">
          <template slot-scope="scope">
            <div>
              <el-button size="mini"
                         @click="relationStation(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>
    <form-dialog v-if="showFormDia"
                 @closeFormDiao="closeFormDiao"
                 @refList="getDateList"
                 :showFormDia="showFormDia"
                 :title="title"
                 :pId="pId"></form-dialog>
    <StresRelation v-if="StshowFormDias"
                   @closeStshowForm="closeStshowForm"
                   :StshowFormDias="StshowFormDias"
                   @refList="getDateList"
                   :relation="relation"
                   :rowId="rowId"></StresRelation>
  </div>
</template>
<script>
import FormDialog from './components/dikeForm-dialog.vue'
import dikeManager from '@/api/reservoirManagement/dikeManager.js'
import StresRelation from './components/StresRelationdike.vue'
import req from '@/request.js'
import { TableMixin } from '@/mixins/tableMixin'
export default {
  name: 'dikeManager',
  components: {
    FormDialog,
    StresRelation
  },
  mixins: [TableMixin],
  data () {
    return {
      reSets: [{ name: '关联测站', value: 'relationStation', id: '1' }],
      loading: false,
      relation: '',//关联参数
      StshowFormDias: false,
      rowId: '',
      multipleSelection: [], // 复选框
      queryParam: {
        pageBean: {
          pageSize: 20,
          page: 1,
        },
        params: {
          dikeName: null, //堤防名称
          dikeType: null, //堤防类型（参考字典）
          dikeGrad: null, //堤防级别（参考字典
        },
      },
      total: 0,
      title: '',
      showFormDia: false,
      tableData: [],
      pId: '',
      dikeTypes: [], //堤防类型
      dikeGrads: [], //堤防级别
      dikePatts: [], //堤防形式
    }
  },
  created () {
    this.getDateList()
    this.getSysDict()
  },
  methods: {

    relationStation (row) {//打开测站关联
      this.row = row;
      this.relation = this.row.dikeCode;
      this.rowId = this.row.id;
      this.StshowFormDias = true;
    },
    closeStshowForm () {//关闭测站关联
      this.StshowFormDias = false;
      this.getDateList()
    },
    // openMore() {
    //     this.showMore = !this.showMore
    // },
    changedikeGrad (val) {
      if (val) {
        this.queryParam.params.dikeGrad = val
      } else {
        this.queryParam.params.dikeGrad = null
      }
    },
    changedikeType (val) {
      if (val) {
        this.queryParam.params.dikeType = val
      } else {
        this.queryParam.params.dikeType = null
      }
    },
    //获取字典数据
    getSysDict () {
      let portal = window.context.portal
      return new Promise((resolve, reject) => {
        req.post(portal + '/sys/sysDictionary/v1/queryAllDictItems').then(
          (response) => {

            resolve(response.data)
            this.dikePatts = response.data.dfxs; //堤防形式
            this.dikeTypes = response.data.dflx //堤防类型
            this.dikeGrads = response.data.dfjb //堤防级别

            this.getDateList()
          },
          (error) => {
            reject(error)
          }
        )
      })
    },
    getDateList () {
      this.loading = true
      this.tableData = []
      dikeManager.ListGet(this.queryParam, (res) => {
        this.tableData = res.value.rows
        this.total = res.value.total
        for (let i = 0; i < this.dikeTypes.length; i++) {
          for (let j = 0; j < this.tableData.length; j++) {
            if (this.dikeTypes[i].value === this.tableData[j].dikeType) {
              this.tableData[j].dikeTypeName = this.dikeTypes[i].name
            } else {
              // this.tableData[j].dikeTypeName='';
            }
          }
        }
        this.dikePatts.forEach((e) => {
          this.tableData.forEach((i) => {
            if (i.dikePatt === e.value) {
              i.dikePatt = e.name
            }
          })
        })
        this.dikeGrads.forEach((e) => {
          this.tableData.forEach((i) => {
            if (i.dikeGrad === e.value) {
              i.dikeGrad = e.name
            }
          })
        })
        this.$set(this.tableData, this.tableData)
        this.loading = false
      })
    },
    openFormDiao () {
      this.title = '新增'
      this.showFormDia = true
    },
    closeFormDiao () {
      this.showFormDia = false
      this.getDateList()
    },
    handleSizeChange (val) {
      this.queryParam.pageBean.pageSize = val
      this.getDateList()
    },
    handleCurrentChange (val) {
      this.queryParam.pageBean.page = val
      this.getDateList()
    },
    // // 修改
    handleUpdate (row) {
      this.title = '修改';
      this.pId = row.id;
      this.showFormDia = true;
    },
    handleSelectionChange (val) {
      this.multipleSelection = val.map((item) => {
        return item.id
      })
    },
    // // 指定一个key标识这一行的数据
    getRowKey (row) {
      return row.id
    },
    // // 批量删除
    deleteSelect () {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning',
      })
        .then(() => {
          dikeManager
            .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(() => { })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning',
      })
        .then(() => {
          dikeManager
            .remove(row.id, (res) => {
              if (res.state) {
                this.$notify({
                  type: 'success',
                  message: '删除成功',
                  duration: 2000,
                  // offset: 80
                })
                this.queryParam.pageBean.page = 1
                this.getDateList()
              }
            })
            .catch((res) => {
              this.$notify({
                type: 'error',
                message: res.message,
                duration: 2000,
                offset: 80,
              })
              this.getDateList()
            })
        })
        .catch(() => { })
    },
    // // 点击查询
    onClickSearch () {
      this.queryParam.pageBean.page = 1
      this.getDateList()
    },
    // 重置
    resetQuery () {
      this.queryParam.params = {}
      this.onClickSearch()
    },
  },
}
</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>
