<template>
  <el-dialog title="销售合同" :show-close="false" :visible.sync="dialogFormVisible">
    <div class="table-box">
      <el-table :data="contractList" style="width: 100%;">
        <el-table-column prop="content" label="已生成的销售合同">
          <template slot-scope="{ row }">
            <span style="cursor: pointer;color: #5AC4FC;font-size: 14px;" @click="downloadContract(row)">销售合同</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { exportFile } from '@/utils'
import { downloadSaleContractFile } from '@/api/myTrade/salesContract'

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    contractId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      contractList: []
    }
  },
  watch: {
    visible: {
      handler(val) {
        this.dialogFormVisible = val
      }
    },
    contractId: {
      handler(val) {
        if (val) {
          this.contractList = [{ id: val }]
        } else {
          this.contractList = []
        }
      }
    }
  },
  methods: {

    handleConfirm() {
      this.visible = false
      this.$router.go(-1)
    },
    async downloadContract(val) {
      if (val.id) {
        const idNum = val.id
        const res = await downloadSaleContractFile({ contractId: idNum })

        if (res) {
          exportFile(res)
        }
      }
    }
  }
}
</script>
