<template>
  <el-dialog v-if="visible" :append-to-body="appendToBody" :visible.sync="visible" :v-loading="loading" :title="title" width="1600" @close="close">
    <el-table v-loading="loading" ref="multipleTable" :data="list" :row-key="getRowKey" :header-cell-style="{background:'#f5f7fa','font-weight': 'bold','color': '#000'}" tooltip-effect="dark" border fit highlight-current-row>
      <el-table-column :reserve-selection="true" type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="MAXUSE" label="新增使用人数" />
      <el-table-column prop="CREATOR" label="变更人" />
      <el-table-column prop="CREATTIME" label="变更时间" />
      <el-table-column prop="REMAKE" label="备注" />
      <el-table-column prop="CONTRACT" label="合同">
        <template slot-scope="{row}" align="center" >
          <el-button type="text" @click="checkPdf(row)">查看</el-button>
          <el-divider v-if="searchFrom.isCorp === '1'" direction="vertical"/>
          <el-button v-if="searchFrom.isCorp === '1'" type="text" @click="updatePdf(row)">编辑合同</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <pagination :total="pageInfo.total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
    </div>
    <div style="display: flex;justify-content: flex-end; margin-top: 10px;margin-right: 10px">
      <!--      <el-button v-if="searchFrom.isCorp === '1'" type="primary" @click="addContract">新增合约期</el-button>-->
      <el-button @click="close">返回</el-button>
    </div>
    <glance-pdf ref="glancePdf" append-to-body/>
    <!--    <add-contract ref="addContract" append-to-body @getResult="getList"/>-->
    <upload-pdf ref="uploadPdf" title="编辑合同" accept=".pdf,.PDF" append-to-body @getChoose="confirm"/>
  </el-dialog>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { requestFN } from '@/utils/request'
import Pagination from '@/components/Pagination'
import { upload } from '@/utils/upload'
import uploadPdf from '@/components/uploadPdf'
import glancePdf from '@/components/glancePdf'

export default {
  directives: { waves },
  components: { Pagination, uploadPdf, glancePdf },
  props: {
    appendToBody: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '变更记录'
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      list: [],
      pageInfo: {
        total: 0,
        page: 1,
        limit: 10
      },
      heirloom: {},
      searchFrom: {
        ENTERPRISE_ID: '',
        CORPINFO_ID: '',
        isCorp: '1'
      },
      config: config
    }
  },
  methods: {
    init(e) {
      this.visible = true
      this.heirloom = e
      this.searchFrom.CORPINFO_ID = e.CORPINFO_ID
      this.getList()
    },
    getList() {
      this.loading = true
      requestFN(
        '/expireCorpinfo/renewalList?showCount=' + this.pageInfo.limit + '&currentPage=' + this.pageInfo.page,
        this.searchFrom
      ).then((data) => {
        this.loading = false
        this.list = data.varList
        this.pageInfo.total = data.page.totalResult
      }).catch((e) => {
        this.loading = false
      })
    },
    getRowKey(row) {
      return row.CORPENTERPRISECONTACT_ID
    },
    checkPdf(row) {
      if (row.CONTRACT) {
        this.$refs.glancePdf.init({ url: config.fileUrl + row.CONTRACT })
      } else {
        this.$message({
          message: '文件为空',
          type: 'warning'
        })
      }
    },
    updatePdf(row) {
      this.$refs.uploadPdf.init(row)
    },
    // addContract() {
    //   this.$refs.addContract.init(this.heirloom)
    // },
    confirm(e) {
      const formData = new FormData()
      formData.append('CONTRACT', e.info[0].raw)
      formData.append('RENEWAL_ID', e.heirloom.RENEWAL_ID)
      upload('/enterprises/uploadPdf', formData).then((res) => {
        if (res.errorMessage) {
          this.$message({
            message: res.errorMessage,
            type: 'error'
          })
        } else {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    close() {
      this.visible = false
      this.searchFrom = {
        ENTERPRISE_ID: '',
        isCorp: '1'
      }
    }
  }
}
</script>
