<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[1].value"
                  clearable
                  placeholder="请输入项目编号"
                ></el-input>
              </el-form-item>
              <el-form-item label="项目名称">
                <el-input
                  size="small"
                  v-model="querys[2].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-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane label="物资" name="1"></el-tab-pane>
            <el-tab-pane label="项目" name="2"></el-tab-pane>
          </el-tabs>
          <el-table
            ref="multipleSelection"
            v-loading="loading"
            border
            size="mini"
            :data="tableData"
            height="100%"
            :row-key="rowKey"
            :style="{width: '100%', height: '500px'}"
            :max-height="tableMaxHeight"
            :cell-style="{'text-align': 'center'}"
            :header-cell-style="{'text-align': 'center'}"
            @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="projectNumber"
              min-width="10%"
              label="项目编号"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeTitle"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="项目名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeReleaseTime"
              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: {
    useOrgList: Array,
  },
  data() {
    return {
      showTroubleshooting: false, // 控制弹层显示与隐藏
      multipleSelection: [],
      selectedValue: [],
      activeName: '1',
      url: '',
      querys: [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'procureOrgId',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'projectNumber',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'noticeTitle',
          relation: 'AND',
          value: '',
        } 
      ],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
        sorter:[{
            direction: 'DESC',
            property: 'create_time_',
        }]
      },
      currentUser:{},
    }
  },
  mixins: [TableMixin],
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.querys[0].value = this.currentUser.org[0].orgId
  },
  methods: {
    rowKey(row) {
      return row.id
    },
    //搜索
    handleSearch() {
      this.loadData()
    },
    //重置
    handleReset() {
      this.querys[0].value = this.currentUser.org[0].orgId
      this.querys[1].value = ''
      this.querys[2].value = ''
      this.loadData()
    },
    //显示弹框
    add() {
      this.showTroubleshooting = true
      this.loadData()
      this.$nextTick( ()=> {
        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
        })
      }
    },
    handleClick() {
      if (this.activeName == 1) {
        this.url = '/matApproval/v1/page'
      }
      if (this.activeName == 2) {
        this.url = '/projectApproval/v1/page'
      }
      this.loadData()
    },
    // 获取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.activeName == 1) {
        this.url = '/matApproval/v1/page'
      }
      if (this.activeName == 2) {
        this.url = '/projectApproval/v1/page'
      }
      this.$http.post('${portal}' + this.url, this.queryParam).then((res) => {
        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()
      })
    },
    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
      list.forEach(item=>{
        if(this.activeName==1){
            item.noticeType = 1
        }
        if(this.activeName==2){
            item.noticeType = 2
        }
      })
      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: 600px;
}

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