<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '150px' : '120px'">
      <el-form-item label="项目指南名称" prop="guideName">
        <el-input v-model="dataForm.guideName" placeholder="项目指南名称"></el-input>
      </el-form-item>
      <el-form-item label="类别" prop="guideType">
        <el-select v-model="dataForm.guideType" placeholder="请选择">
          <el-option
              v-for="item in guideType"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="级别" prop="guideLevel">
        <el-select v-model="dataForm.guideLevel" placeholder="请选择">
          <el-option
              v-for="item in guideLevel"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="附件地址" prop="attachmentUrl">
        <el-upload
            class="upload-demo"
            :action="uploadUrl"
            :limit=1
            :on-change="handleChange"
            :http-request="upload"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      uploadUrl: '/system/upload',
      guideType: [{
        value: 0,
        label: '算法类'
      }, {
        value: 1,
        label: '科研类'
      }, {
        value: 2,
        label: '技术开发类'
      }],
      guideLevel: [{
        value: 0,
        label: '区级'
      }, {
        value: 1,
        label: '市级'
      }, {
        value: 2,
        label: '省级'
      }, {
        value: 3,
        label: '国家级'
      }],
      value: '',
      visible: false,
      dataForm: {
        id: '',
        guideName: '',
        guideType: '',
        guideLevel: '',
        attachmentUrl: '',
        creationDate: ''
      }
    }
  },
  computed: {
    dataRule() {
      return {
        fileList: [{
          url: window.SITE_CONFIG['uploadURL'] + '/system/upload'
        }],
        guideName: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        guideType: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        guideLevel: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        attachmentUrl: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        creationDate: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handleChange(file, fileList) {
      // this.fileList = fileList.slice(-3);
    },
    upload(file) {
      let formData = new FormData();
      formData.append("file", file.file);
      formData.append("folderName", "");
      this.$http
          .post(`/system/upload`, formData)
          .then(({data: res}) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.dataForm.attachmentUrl = res.data.src;
          })
          .catch(() => {
          });
    },
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/system/guidedeclaration/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/system/guidedeclaration', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false})
  }
}
</script>
