<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      title="准入规则"
      :visible.sync="showTroubleshooting"
      width="60%"
      :before-close="handleClose"
      top="5vh"
    >
      <template class="materialInformation">
        <div class="common-page-shadow">
          <div class="common-search-container" ref="contentPanel">
            <el-form v-model="queryParam" :inline="true">
              <el-form-item label="名称">
                <el-input
                  size="small"
                  v-model="querys[0].value"
                  clearable
                  placeholder="请输入名称"
                ></el-input>
              </el-form-item>

              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  icon="el-icon-search"
                  @click="handleSearch"
                  >搜索</el-button
                >
                <el-button
                  size="small"
                  icon="el-icon-refresh"
                  @click="handleReset"
                  >重置</el-button
                >
              </el-form-item>
            </el-form>
          </div>
          <el-table
            ref="multipleSelection"
            v-loading="loading"
            border
            size="mini"
            :data="tableData"
            height="400px"
            :row-key="rowKey"
            :style="{width: '100%', height: '500px'}"
            :max-height="tableMaxHeight"
            :cell-style="{'text-align': 'center'}"
            :header-cell-style="{'text-align': 'center'}"
            :header-cell-class-name="headerCellClassName"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              disabled
              width="55"
              :reserve-selection="true"
            />
            <el-table-column type="index" width="55" label="序号" />
            <el-table-column
              prop="code"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="规则编码"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="name"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="规则名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="enterpriseName"
              min-width="10%"
              label="企业名称"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="matCategoryName"
              min-width="10%"
              label="供应分类"
              align="center"
              resizable
            >
            </el-table-column>
          </el-table>
          <div style="float: right; margin-top: 10px">
            <el-pagination
              ref="paginationPanel"
              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="queryParam.pageBean.total"
            >
            </el-pagination>
          </div>
        </div>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSave">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import basicData from '@/api/basicData'
export default {
  name: 'CmgtSaftyDangerCheckplanFormDialogTroubleshooting', // 排查计划明细
  props: {
    useOrgList: Array,
    multiple: {
      type: Boolean,
      default: true,
    },
    needReturnKey: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      showTroubleshooting: false, // 控制弹层显示与隐藏
      multipleSelection: [],
      selectedValue: [],
      treeData: [],
      defaultProps: {
        id: 'code',
        label: 'name',
        isLeaf: 'leaf',
      },
      querys: [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'code',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'enterpriseId',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'NOT_EQUAL',
          parentGroup: '',
          property: 'id',
          relation: 'AND',
          value: '',
        },
      ],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      currentUser: {},
    }
  },
  mixins: [TableMixin],
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
  },
  methods: {
    // 对顶部选择框进行禁用
    headerCellClassName({row, column, rowIndex, columnIndex}) {
      if (!this.multiple && columnIndex == 0) {
        return 'is-disable'
      }
    },
    rowKey(row) {
      return row.id
    },
    //搜索
    handleSearch() {
      this.queryParam.pageBean.page = 1
      this.loadData()
    },
    //重置
    handleReset() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = ''
      this.queryParam.pageBean.page = 1
      this.loadData()
    },
    //显示弹框
    add(row) {
      this.showTroubleshooting = true
      this.querys[1].value = row.enterpriseId
      this.loadData()
      this.$nextTick(function () {
        this.selectedValue = this.useOrgList
        this.initTableSelection()
      })
    },
    // 初始化表格行的选中状态
    initTableSelection() {
      if (this.selectedValue.length) {
        this.selectedValue = this.selectedValue.filter((item, index) => {
          let selectItem = this.tableData.find((tItem) => tItem.id === item.id)
          if (!!selectItem) {
            this.$refs.multipleSelection.toggleRowSelection(selectItem, true)
          }
          return !selectItem
        })
      }
    },
    // 获取list
    loadData() {
      this.loading = true
      this.tableData = []
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      this.$http
        .post('${portal}/admissionConfig/v1/queryQuaCategory', this.queryParam)
        .then((res) => {
          this.loading = false
          this.queryParam.pageBean.total = res.data.total
          this.tableData = res.data.rows
          this.initTableSelection()
          this.$set(this.tableData)
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    //   if (this.multipleSelection.length > 1) {
    //     this.$refs.multipleSelection.toggleRowSelection(
    //       this.multipleSelection[0],
    //       false
    //     )
    //   }
    },
    // 点击关闭按钮
    handleClose() {
      this.$refs.multipleSelection.clearSelection()
      this.showTroubleshooting = false
    },
    // 保存按钮
    handleSave() {
      let list = []
      list = this.multipleSelection
      this.$emit('ruleData', list)
      this.handleClose()
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData()
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData()
    },
  },
}
</script>

<style scoped
  lang="scss">
.dialog-footer {
  width: 100%;
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

/deep/.el-dialog__body {
  display: flex;
}

.common-search-container {
  padding: 0px;
  padding-left: 10px;
  box-shadow: 0 0 0;
  display: block;
}

/deep/.el-dialog__body {
  height: 100%;
}

/deep/ .el-table-column--selection.is-disable .el-checkbox {
  visibility: hidden;
}
</style>
