<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="querys[2].value"
            clearable
            placeholder="请输入企业名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="供应商名称">
          <el-input
            size="small"
            v-model="querys[3].value"
            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 v-loading="loading" class="common-table-container flex searchPanel">
      <el-col :span="24" class="common-table-wrapper">
        <div class="common-btn-group">
          <el-button
            size="small"
            icon="el-icon-plus"
            type="primary"
            class="btn btn-plus"
            @click="handleAdd"
          >
            新增
          </el-button>
          <el-button
            size="small"
            slot="reference"
            icon="el-icon-delete"
            type="danger"
            class="btn btn-delete"
            @click="handleDelete"
          >
            {{ this.$t('common.delete') }}
          </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="50px" 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="enterpriseContact"
              min-width="130px"
              label="企业联系人"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="enterpriseContactPhone"
              min-width="130px"
              label="企业联系电话"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="enterpriseContactAddress"
              min-width="130px"
              label="企业联系地址"
              show-overflow-tooltip
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeCode"
              min-width="130px"
              show-overflow-tooltip
              label="问题项目编号"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeName"
              min-width="130px"
              show-overflow-tooltip
              label="问题项目"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="supplierName"
              min-width="130px"
              label="供应商名称"
              align="center"
              show-overflow-tooltip
              resizable
            >
            </el-table-column>
            
            <el-table-column
              prop="recordTime"
              min-width="130px"
              label="记录日期"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="problemCategory"
              min-width="100px"
              label="问题类别"
              align="center"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('gyswtlb', row.problemCategory) }}
              </template>
            </el-table-column>
            <el-table-column
              prop="problemDescription"
              min-width="130px"
              label="问题描述"
              show-overflow-tooltip
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              min-width="130px"
              label="查看附件"
              align="center"
              fixed="right"
              resizable
            >
              <template slot-scope="scope">
                <div>
                  <el-button
                    icon="el-icon-view"
                    size="small"
                    @click="handleView(scope.row)"
                    plain
                    type="primary"
                    >查看附件</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="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 dictUtils from '@/components/dict/DictSelectUtils.js'
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import utils from '@/utils.js'

export default {
  name: 'domesticDialog',
  components: {
    Breadcrumb,
    dictUtils,
  },
  props: {
    editData: Object,
  },
  data() {
    return {
      saveing: false,
      breadcrumbOpt: [
        {breadcrumbName: '供应商准入'},
        {breadcrumbName: '问题清单'},
      ],
      multipleSelection: [],
      tableData: [],
      loading: false,
      dialogVisible: false,
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      searchForm: {
        supplier_name_: '',
      },
      form: {
        accessoryList: [],
      },
      tableLoading: false,
      querys: [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'SUPPLIER_ID_',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'enterpriseId',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'enterpriseName',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'supplierName',
          relation: 'AND',
          value: '',
        },
      ],
    }
  },
  created() {
    this.querys[0].value = this.$route.params.supplierId
    this.querys[1].value = this.$route.params.enterpriseId
    this.loadData()
  },
  methods: {
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    // 顶部搜索
    handleSearch() {
      this.loadData()
    },
    // 重置
    clearSearchForm() {
      this.querys[2].value = ''
      this.querys[3].value = ''
      this.handleSearch()
    },
    loadData() {
      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.queryParam.sorter = [
        {
          direction: 'DESC',
          property: 'create_time_',
        },
      ]
      this.tableLoading = true
      this.$http
        .post('${portal}/supplierIssueManagement/v1/page', this.queryParam)
        .then((res) => {
          if (res.data.state) {
            this.tableData = res.data.value.rows
            this.queryParam.pageBean = {
              page: res.data.value.page,
              pageSize: res.data.value.pageSize,
              total: res.data.value.total,
            }
          }
          this.tableLoading = false
        })
    },
    handleView(row) {
      this.dialogVisible = true
      this.form = row
    },
    //添加
    handleAdd() {
      let value = localStorage.getItem('dataKey')
      let data = JSON.parse(value)
      this.$router.push({
        name: 'addQuestionDialog',
        query: {
          data: data,
        },
      })
    },
    // 删除
    handleDelete() {
      let selection = this.$refs.multipleSelection.selection
      if (selection.length === 0) {
        this.$alert('请选择要删除的数据！', this.$t('common.tips'), {
          confirmButtonText: this.$t('common.confirm'),
          type: 'warning',
        })
      } else {
        let ids = []
        selection.forEach((item) => {
          let str = item.id
          ids.push(str)
        })
        ids = ids.join(',')
        this.$confirm(this.$t('common.sureDelete'), this.$t('common.tips'), {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning',
        }).then(() => {
          this.tableLoading = true
          this.$http
            .remove('${portal}/supplierIssueManagement/v1/?ids=' + ids)
            .then((res) => {
              this.tableLoading = false
              if (res.data.state) {
                this.$message.success('删除成功！')
                this.handleSearch()
              } else {
                this.$message.error('删除失败！')
              }
            })
        })
      }
    },
    //下载按钮回调
    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)
    },
    dialogBeforeClose() {
      this.dialogVisible = false
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      // if (this.multipleSelection.length > 1) {
      //   this.$refs.multipleSelection.toggleRowSelection(
      //     this.multipleSelection[0],
      //     false
      //   )
      // }
    },
    // 关闭//面包屑点击
    handleClose() {
      // this.$router.push({
      //   name: 'onboardingSupplier',
      // })
      let value = localStorage.getItem('dataKey')
      let data = JSON.parse(value)
      if (data.form == 'enterprise') {
        this.$router.push({
          name: 'onboardingSupplier',
        })
      } else {
        this.$router.push({
          name: 'operationalEvaluation',
        })
      }
      localStorage.removeItem('dataKey')
    },
    handleCurrentChange(page) {
      this.params.pageBean = {...this.queryParam.pageBean, page}
      this.loadData(this.queryParam)
    },
    handleSizeChange(pageSize) {
      this.params.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData(this.queryParam)
    },
  },
}
</script>
<style lang="scss" scoped>
.crumbs {
  margin-bottom: 10px;
  box-shadow: 0 0.10417vw 0.625vw 0 rgba(0, 0, 0, 0.1);
}
.common-table-container {
  height: calc(100% - 10px);
}
</style>
  