<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="文件类型编号" prop="typeCode">
      <el-input v-model="dataForm.typeCode" placeholder="文件类型编号"></el-input>
    </el-form-item>
    <el-form-item label="文件编号" prop="fileCode">
      <el-input v-model="dataForm.fileCode" placeholder="文件编号"></el-input>
    </el-form-item>
    <el-form-item label="文件名称" prop="fileName">
      <el-input v-model="dataForm.fileName" placeholder="文件名称"></el-input>
    </el-form-item>
    <el-form-item label="文件路径URL" prop="fileUrl">
      <el-input v-model="dataForm.fileUrl" placeholder="文件路径URL"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="删除标志，是否有效:1 可用 0不可用" prop="valid">
      <el-input v-model="dataForm.valid" placeholder="删除标志，是否有效:1 可用 0不可用"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="creatorId">
      <el-input v-model="dataForm.creatorId" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建人姓名" prop="creatorName">
      <el-input v-model="dataForm.creatorName" placeholder="创建人姓名"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updaterId">
      <el-input v-model="dataForm.updaterId" placeholder="更新人"></el-input>
    </el-form-item>
    <el-form-item label="更新人姓名" prop="updaterName">
      <el-input v-model="dataForm.updaterName" placeholder="更新人姓名"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          typeCode: '',
          fileCode: '',
          fileName: '',
          fileUrl: '',
          remark: '',
          valid: '',
          creatorId: '',
          creatorName: '',
          createTime: '',
          updaterId: '',
          updaterName: '',
          updateTime: '',
          version: ''
        },
        dataRule: {
          typeCode: [
            { required: true, message: '文件类型编号不能为空', trigger: 'blur' }
          ],
          fileCode: [
            { required: true, message: '文件编号不能为空', trigger: 'blur' }
          ],
          fileName: [
            { required: true, message: '文件名称不能为空', trigger: 'blur' }
          ],
          fileUrl: [
            { required: true, message: '文件路径URL不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          valid: [
            { required: true, message: '删除标志，是否有效:1 可用 0不可用不能为空', trigger: 'blur' }
          ],
          creatorId: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          creatorName: [
            { required: true, message: '创建人姓名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updaterId: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          updaterName: [
            { required: true, message: '更新人姓名不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/monitoring/sysfile/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.typeCode = data.sysFile.typeCode
                this.dataForm.fileCode = data.sysFile.fileCode
                this.dataForm.fileName = data.sysFile.fileName
                this.dataForm.fileUrl = data.sysFile.fileUrl
                this.dataForm.remark = data.sysFile.remark
                this.dataForm.valid = data.sysFile.valid
                this.dataForm.creatorId = data.sysFile.creatorId
                this.dataForm.creatorName = data.sysFile.creatorName
                this.dataForm.createTime = data.sysFile.createTime
                this.dataForm.updaterId = data.sysFile.updaterId
                this.dataForm.updaterName = data.sysFile.updaterName
                this.dataForm.updateTime = data.sysFile.updateTime
                this.dataForm.version = data.sysFile.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/monitoring/sysfile/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'typeCode': this.dataForm.typeCode,
                'fileCode': this.dataForm.fileCode,
                'fileName': this.dataForm.fileName,
                'fileUrl': this.dataForm.fileUrl,
                'remark': this.dataForm.remark,
                'valid': this.dataForm.valid,
                'creatorId': this.dataForm.creatorId,
                'creatorName': this.dataForm.creatorName,
                'createTime': this.dataForm.createTime,
                'updaterId': this.dataForm.updaterId,
                'updaterName': this.dataForm.updaterName,
                'updateTime': this.dataForm.updateTime,
                'version': this.dataForm.version
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
