<template>
  <el-dialog v-loading="loading" v-if="visible" :visible.sync="visible" title="替换">
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="文件:" prop="FILE">
        <upload-file :file-list.sync="form.FILE" :limit="1" :file-size="200" append-to-body accept=".pdf"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="goBack">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { upload } from '@/utils/upload'
import uploadFile from '../../../../components/UploadFile/index.vue'

export default {
  components: { Pagination, uploadFile },
  directives: { waves },
  data() {
    return {
      visible: false,
      form: {
        FILE: [],
        BUS_TEXT_LIBRARY_ID: ''
      },
      rules: {
        FILE: [{ required: true, message: '请选择文件', trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    init(e) {
      this.visible = true
      this.form.BUS_TEXT_LIBRARY_ID = e.BUS_TEXT_LIBRARY_ID
    },
    goBack() {
      this.form = {
        FILE: [],
        BUS_TEXT_LIBRARY_ID: ''
      }
      this.visible = false
    },
    save() {
      const loading = this.$loading({
        lock: true,
        text: '上传中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      const formData = new FormData()
      for (let i = 0; i < this.form.FILE.length; i++) {
        if (this.form.FILE[i].raw) {
          formData.append('FILE', this.form.FILE[i].raw)
        }
      }
      formData.append('BUS_TEXT_LIBRARY_ID', this.form.BUS_TEXT_LIBRARY_ID)
      upload(
        '/textLibrary/updateFile',
        formData
      ).then((data) => {
        loading.close()
        this.visible = false
        this.$emit('logical-end', { result: 'OK' })
      }).catch((e) => {
        loading.close()
        console.log(e)
      })
    }
  }
}

</script>
