<template>
  <el-dialog v-if="visible" :append-to-body="appendToBody" :visible.sync="visible" :v-loading="loading" :title="title" width="1600" @close="close">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="服务有限期" prop="DEADLINE">
        <el-date-picker
          v-model="form.DEADLINE"
          style="width: 100%;"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item prop="CONTRACT" label="文件">
        <upload-file :file-list.sync="form.CONTRACT" :limit="1"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { upload } from '@/utils/upload'
import Pagination from '@/components/Pagination'
import glancePdf from '../../../util/glancePdf'
import UploadFile from '../../../../components/UploadFile'

export default {
  directives: { waves },
  components: { UploadFile, Pagination, glancePdf },
  props: {
    appendToBody: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '服务期详情'
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      form: {
        DEADLINE: [],
        CONTRACT: []
      },
      rules: {
        DEADLINE: [
          { required: true, message: '请选择日期', trigger: 'blur' }
        ],
        CONTRACT: [
          { required: true, message: '请选择文件', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init(e) {
      console.log(e)
      this.visible = true
      this.heirloom = e
      this.form.ENTERPRISE_ID = e.ENTERPRISE_ID
    },
    close() {
      this.visible = false
      this.form = {
        ENTERPRISE_ID: ''
      }
      this.$emit('getResult', { heirloom: this.heirloom })
    },
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const formData = new FormData()
          formData.append('DEADLINE_START', this.form.DEADLINE[0])
          formData.append('DEADLINE_END', this.form.DEADLINE[1])
          formData.append('CONTRACT', this.form.CONTRACT[0].raw)
          formData.append('ENTERPRISE_ID', this.heirloom.ENTERPRISE_ID)
          upload('/enterprises/addContract', formData).then((res) => {
            if (res.errorMessage) {
              this.$message({
                message: res.errorMessage,
                type: 'error'
              })
            } else {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.close()
            }
          })
        }
      })
    }
  }
}
</script>
