<template>
  <el-dialog :visible.sync="dialogFormVisible" title="添加闸井" width="700px" append-to-body>
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" size="small" class="form-container">
        <el-row>
          <el-form-item class="selectForm-container-item">
            <el-input v-model.trim="listQuery.keywords" placeholder="闸井地址/编号" clearable/>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-select v-model="listQuery.wellType" placeholder="选择闸井类型" clearable>
              <el-option
                v-for="item in wellTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" size="small" @click="fetchData">搜索</el-button>
        </el-row>
      </el-form>
    </div>
    <!-- 查询结果 -->
    <div class="tablediv">
      <el-table v-loading="loading" :data="list" size="small" max-height="300" class="table" border @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column :index="indexMethod" label="#" align="center" type="index" />
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
          <template slot-scope="scope">
            <span :class="column.class">{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.offset"
        :total="total"
        :page-sizes="[5,10]"
        :page-size="listQuery.limit"
        align="center"
        layout="total, prev, pager, next"
        background
        small
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getNoGroupWells } from '@/api/group'
import { addWellsToGroup } from '@/api/group'
import DeptSelect from '@/components/DeptSelect'
import { getWellType } from '@/api/well'

export default {
  name: 'AddWell',
  components: { DeptSelect },
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      multipleSelection: [],
      groupId: '',
      listQuery: {
        keywords: '',
        wellType: '',
        beginTime: '',
        endTime: '',
        deptid: '',
        offset: 1,
        limit: 10,
        sort: 'id'
      },
      form: { // 表单
        deptid: '',
        user: ''
      },
      wellTypeList: [],
      columns: [
        {
          text: '闸井编号',
          value: 'wellCode'
        },
        {
          text: '详细地址',
          value: 'position'
        },
        {
          text: '闸井类型',
          value: 'wellTypeName'
        },
        {
          text: '产权单位',
          value: 'deptName'
        }
      ],
      list: [], // 井列表数据
      total: 0,
      loading: false, // 加载动态效果
      btnLoading: false
    }
  },
  computed: {
    titleText: function() {
      return this.roleName + ''
    }
  },
  created() {
    this.fetchWellType()
  },
  methods: {
    // 初始化对话框
    initDialog: function(groupId) {
      this.loading = true
      this.groupId = groupId
      this.dialogFormVisible = true
      this.btnLoading = false
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getNoGroupWells(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    saveData() {
      // 获取选择的井列表
      if (this.checkSelection()) {
        const list = []
        this.multipleSelection.forEach(function(value, index) {
          list.push(value.id)
        })
        const params = {
          id: this.groupId,
          wellIds: list
        }
        this.btnLoading = true
        addWellsToGroup(params).then(response => {
          if (response.code === 200) {
            this.$message.success('添加成功')
            this.cancel()
          }
          this.btnLoading = false
        }).catch(() => {
          this.btnLoading = false
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 获取闸井类型
    fetchWellType() {
      getWellType().then(response => {
        this.wellTypeList = []
        const wellTypes = this.$store.getters.wellTypes
        for (const wellType of response.data) {
          if (wellTypes.indexOf(wellType.value) !== -1) {
            this.wellTypeList.push(wellType)
          }
        }
      })
    },
    // 检查多选情况，未选择返回false，已有选择返回true
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 取消
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

  .el-dialog{
    min-width:350px !important;
  }
  .scrollbar{
    max-height: 400px;
  }
  .el-tree{
  }
  .el-select{
    width: 100%;
    overflow-x: hidden;
    .el-scrollbar__wrap{
      overflow-x: hidden;
    }
  }
  .list{
    max-height:300px;
  }
  .pagination-container{
    margin-top: 10px;
  }
  .dialog-footer {
    margin-top: -40px;
    text-align: right;
  }
</style>
