<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="showTroubleshooting"
      width="60%"
      :close-on-press-escape="false"
      :before-close="handleClose"
      top="7vh"
    >
      <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="vendorName"
              min-width="10%"
              label="供货方"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorCode"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="供货方 "
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorTaxNumber"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="供货方税号"
              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: {
    title: String,
    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: 'LIKE',
          parentGroup: '',
          property: 'name_',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'matCompanyId',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'status',
          relation: 'AND',
          value: '3',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'orderStatus',
          relation: 'AND',
          value: '0',
        },
      ],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      currentUser: {},
    }
  },
  mixins: [TableMixin],
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.querys[1].value = this.currentUser.org[0].orgId
  },
  methods: {
    // 对顶部选择框进行禁用
    headerCellClassName({row, column, rowIndex, columnIndex}) {
      if (!this.multiple && columnIndex == 0) {
        return 'is-disable'
      }
    },
    rowKey(row) {
      return row.id
    },
    //搜索
    handleSearch() {
      this.loadData()
    },
    //重置
    handleReset() {
      this.querys[0].value = ''
      this.querys[1].value = this.currentUser.org[0].orgId
      this.querys[2].value = '3'
      this.querys[3].value = '0'
      this.loadData()
    },
    //显示弹框
    add() {
      this.showTroubleshooting = true
      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])
      }
      if (this.querys[3].value) {
        this.queryParam.querys.push(this.querys[3])
      }
      this.$http
        .post('${portal}/bizContract/v1/pageVendorGroup', this.queryParam)
        .then((res) => {
          if (res.data.state) {
            this.loading = false
            this.queryParam.pageBean.total = res.data.value.total
            this.queryParam.pageBean.page = res.data.value.page
            this.queryParam.pageBean.pageSize = res.data.value.pageSize
            this.tableData = res.data.value.rows
            this.initTableSelection()
            this.$set(this.tableData)
          }
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      if (!this.multiple && 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('addDetail', 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>
