<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="CORP_NAME" label="机构名称"/>
      <el-table-column prop="ENTERPRISE_NAME" label="工作企业" />
      <el-table-column prop="DEADLINE_START" label="服务开始时间" />
      <el-table-column prop="DEADLINE_END" label="服务结束时间" />
      <el-table-column prop="DEADLINE_END" label="合同">
        <template slot-scope="{row}" align="center" >
          <el-button type="text" @click="checkPdf(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 @click="close">返回</el-button>
    </div>
    <glance-pdf ref="glancePdf" append-to-body/>
    <add-contract ref="addContract" append-to-body @getResult="getList"/>
    <upload-excel ref="uploadExcel" title="编辑合同" accept=".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 glancePdf from '../../../util/glancePdf'
import addContract from './addContract'
import UploadExcel from '../../../util/uploadExcel'
import { upload } from '@/utils/upload'

export default {
  directives: { waves },
  components: { addContract, Pagination, glancePdf, UploadExcel },
  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: ''
      },
      config: config
    }
  },
  methods: {
    init(e) {
      this.visible = true
      this.heirloom = e
      this.searchFrom.ENTERPRISE_ID = e.ENTERPRISE_ID
      this.searchFrom.CORPINFO_ID = e.SERVICES
      this.getList()
    },
    getList() {
      this.loading = true
      requestFN(
        '/enterprises/getContract?showCount=' + this.pageInfo.limit + '&currentPage=' + this.pageInfo.page,
        this.searchFrom
      ).then((data) => {
        this.loading = false
        this.list = data.list
        this.pageInfo.total = data.page.totalResult
      }).catch((e) => {
        this.loading = false
      })
    },
    getRowKey(row) {
      return row.ENTERPRISE_ID
    },
    checkPdf(row) {
      this.$refs.glancePdf.init({ url: config.fileUrl + row.CONTRACT })
    },
    updatePdf(row) {
      this.$refs.uploadExcel.init(row)
    },
    confirm(e) {
      const formData = new FormData()
      formData.append('CONTRACT', e.info[0].raw)
      formData.append('CORPENTERPRISECONTACT_ID', e.heirloom.CORPENTERPRISECONTACT_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: '',
        CORPINFO_ID: ''
      }
    }
  }
}
</script>
