<template>
  <div class="common-page-content">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        :model="searchForm"
        label-width="auto"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="供应商名称">
          <el-input
            size="small"
            v-model="searchForm.supplierName"
            clearable
            placeholder="请输入供应商名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="container-btn">
        <el-button
          size="small"
          icon="el-icon-search"
          type="danger"
          class="btn btn-plus"
          @click="handleSearch"
        >
          搜索
        </el-button>
        <el-button
          size="small"
          icon="el-icon-refresh-right"
          class="btn btn-plus"
          @click="clearSearchForm"
        >
          重置
        </el-button>
      </div>
    </div>
    <el-row
      class="common-table-container flex searchPanel"
      :style="SearchPanel"
    >
      <el-col :span="24" class="common-table-wrapper">
        <div class="common-btn-group">
          <!-- <el-button
            size="small"
            slot="reference"
            icon="el-icon-delete"
            type="danger"
            class="btn btn-delete"
            @click="handleDelete"
          >
            {{ this.$t('common.delete') }}
          </el-button> -->
          <!-- <el-button
            size="small"
            icon="el-icon-bottom"
            type="primary"
            plain
            class="btn btn-plus"
          >
            导出数据
          </el-button> -->
          <!-- <el-button
            size="small"
            icon="el-icon-plus"
            type="primary"
            :loading="auditing"
            class="btn btn-plus"
            @click="handleAudit"
          >
            提交审核
          </el-button> -->
        </div>
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" min-width="5%" align="center">
            </el-table-column>
            <el-table-column
              prop="enterpriseName"
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="企业名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="supplierName"
              min-width="130px"
              label="供应商名称"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="categoryName"
              min-width="100px"
              label="供应商分类"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="needProvideQualifications"
              min-width="120px"
              label="是否需要上传资质"
              align="center"
              resizable
            >
              <template slot-scope="scope">
                <div>
                  {{ scope.row.needProvideQualifications == 0 ? '否' : '是' }}
                </div>
              </template>
            </el-table-column>
            <el-table-column min-width="130px" align="center" label="操作">
              <template v-slot="{row}">
                <div>
                  <el-button
                    icon="el-icon-upload2"
                    type="primary"
                    plain
                    size="small"
                    @click="handleView(row)"
                    >查看资质附件</el-button
                  >
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="common-pagenation">
          <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>
      </el-col>
    </el-row>
    <!-- 资质管理 -->
    <el-dialog
      title="资质"
      :visible.sync="showVisible"
      width="80%"
      :before-close="close"
      top="5vh"
    >
      <el-table :data="currentData" size="mini" height="500px" style="width: 100%" border>
        <el-table-column
          prop="qualificationCode"
          show-overflow-tooltip
          min-width="110px"
          align="center"
          label="资质编码"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="qualificationName"
          show-overflow-tooltip
          min-width="100px"
          align="center"
          label="资质名称"
          resizable
        >
        </el-table-column>

        <el-table-column
          prop="qualificationTypeName"
          show-overflow-tooltip
          min-width="100px"
          align="center"
          label="资质类型"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="qualificationForm"
          min-width="100px"
          label="资质归属"
          align="center"
          resizable
        >
          <template slot-scope="scope">
            <div>
              <span v-if="scope.row.qualificationForm == 1">企业</span>
              <span v-if="scope.row.qualificationForm == 2">专家</span>
              <span v-if="scope.row.qualificationForm == 3">个人</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="qualificationAcquisitionDate"
          show-overflow-tooltip
          min-width="120px"
          align="center"
          label="有效开始期"
          resizable
        >
        </el-table-column>
        <el-table-column
          prop="expiryDate"
          show-overflow-tooltip
          min-width="120px"
          align="center"
          label="有效截止期"
          resizable
        >
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          min-width="120px"
          align="center"
          label="操作"
          resizable
        >
        <template slot-scope="scope">
          <div>
            <el-button size="small" @click="handleOpen(scope.row)" type="primary">资质附件</el-button>
          </div>
        </template>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="showVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 资质附件 -->
    <el-dialog
      title="资质查看"
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="dialogBeforeClose"
    >
      <el-table
        class="down"
        :data="form.accessoryList"
        border
        stripe
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column prop="name" label="文件名称"></el-table-column>
        <el-table-column prop="createTime" label="上传时间">
          <template slot-scope="scope">
            <div>
              {{ scope.row.createTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column width="150px" label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="text">
              <a
                target="_blank"
                @click="downloadFile(scope.row.name, scope.row.url)"
                >下载</a
              >
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import supplierManger from '@/api/supplierManger.js'
import {mapState} from 'vuex'
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import routerManagement from '@/api/BasicDataManagement/routerManagement.js'
import req from '@/request.js'
import utils from '@/utils.js'
import {file} from 'jszip'
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      currentPage: 1,
      tableMaxHeight: 400,
      dialogVisible: false,
      auditing: false,
      loading: false,
      tableLoading: false,
      loadingBtn: false,
      visible: false,
      showVisible: false,
      tableData: [],
      fullscreenLoading: false,
      multipleSelection: [],
      form: {},
      suppliersQualificationList: [],
      index: null,
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      searchForm: {
        type: 1,
        reviewStatus: 2,
      }, // 顶部搜索对象
      authList: [],
      deleteIds: '', // 是否选中列表解锁删除按钮
      fileList: [], // 文件上传
      edit: false, // 是否是修改功能
      currentData: [], // 删除时排查是否有在审核的数据
    }
  },
  created() {
    this.handleSearch()
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
      if (this.multipleSelection.length > 1) {
        this.$refs.multipleSelection.toggleRowSelection(
          this.multipleSelection[0],
          false
        )
      }
    },
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    handleOpen(row) {
      this.$http
        .post('${portal}/bizQualificationLabel/v1/getById?id=' + row.id)
        .then((res) => {
          let data = res.data.value
          this.form = data
        })
      this.dialogVisible = true
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.$http
        .post(
          '${portal}/supplier/bizSupplierEnterprise/v1/queryByEnterprise',
          this.queryParam
        )
        .then((res) => {
          this.tableLoading = false
          const data = (res && res.data) || {}
          this.tableData = data.rows || []
          this.queryParam.pageBean = {
            page: data.page,
            pageSize: data.pageSize,
            total: data.total,
          }
        })
    },
    cancelConfirm() {
      this.fileList = []
      this.dialogVisible = false
    },
    //下载按钮回调
    downloadFile(name, url) {
      var a = document.createElement('a')
      var event = new MouseEvent('click')
      a.download = name
      a.target = '_blank'
      a.href = url
      a.dispatchEvent(event)
    },
    //重置
    clearSearchForm() {
      this.searchForm = {
        type: 1,
        reviewStatus: 2,
      }
      this.handleSearch()
    },
    // 删除
    deleteHandle(row, index) {
      this.$confirm('是否确认删除该附件', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.form.suppliersQualificationList.forEach((item, index) => {
          item.fileList.forEach((val, i) => {
            if (index == i) {
              item.accessoryList.splice(i, 1)
              row.filename = ''
              row.filetime = ''
              row.url = ''
            }
          })
        })
      })
    },
    getQualificationData(row) {
      let params = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
        querys: [],
      }
      params.querys.push({
        group: 'main',
        operation: 'EQUAL',
        parentGroup: '',
        property: 'qualificationMemberId',
        relation: 'AND',
        value: row.supplierId,
      })
      this.$http
        .post('${portal}/bizQualificationLabel/v1/page', params)
        .then((res) => {
          this.showVisible = true
          this.currentData = res.data.value.rows
        })
    },
    handleView(row) {
      this.getQualificationData(row)
      // this.$http
      //   .get('${portal}/supplier/bizSupplierEnterprise/v1/getById/' + row.id)
      //   .then((res) => {
      //     let data = res.data
      //     this.form = data
      //     if (this.form.accessoryList.length != 0) {
      //       this.form.accessoryList.forEach((val) => {
      //         this.form.suppliersQualificationList.forEach((item) => {
      //           if (val.group == item.id) {
      //             val['qualificationCategoryName'] =
      //               item.qualificationCategoryName
      //           }
      //         })
      //       })
      //     }
      //   })
      // this.dialogVisible = true
    },
    // 顶部搜索
    handleSearch() {
      let obj = this.searchForm
      let querys = []
      for (let i in obj) {
        if (obj[i]) {
          querys.push({
            group: 'main',
            operation:
              i == 'supplierName' || i == 'enterprise_name_' ? 'LIKE' : 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i],
          })
        }
      }

      this.queryParam.querys = querys
      this.loadData(this.queryParam)
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData(this.queryParam)
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData(this.queryParam)
    },
    dialogBeforeClose() {
      this.visible = false
      this.form.accessoryList = []
    },
    close() {
      this.showVisible = false
    },
  },
  computed: {
    ...mapState({
      headers: (state) => {
        return {Authorization: `Bearer ${state.login.currentUser.token}`}
      },
      action: function () {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      },
    }),
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px',
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.cloumn-btn {
  border: 1px solid #2761ff;
  padding: 3px 10px;
  border-radius: 5px;
  &:nth-child(1) {
    margin-right: 5px;
  }
  &:nth-child(2) {
    margin-right: 5px;
  }
  &:nth-child(1):hover {
    background-color: #2761ff;
    color: #fff;
  }
  &:nth-child(2):hover {
    background-color: #2761ff;
    color: #fff;
  }
  /deep/ i,
  span {
    font-size: 16px;
  }
}
.dia-btn {
  width: 100% !important;
}
/deep/ .el-tree-node__content {
  padding: 5px 0;
}

/deep/ .el-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
.iconBtn {
  border-color: none !important;
  width: 100% !important;
  border: 0px;
}
.closeBtn {
  font-size: 14px !important;
  /deep/.el-icon-close {
    font-size: 14px !important;
  }
}
</style>
