<template>
  <el-form ref="form" :rules="rules" label-width="100px" :model="form">
    <el-form-item label="报表名称" prop="name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="报表分类" prop="category">
      <el-select
        v-model="form.category"
        placeholder="报表分类"
        style="width: 100%"
      >
        <el-option
          v-for="(item, i) in typeList"
          :key="i"
          :label="item.dataName"
          :value="item.dataName"
        ></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>
<script>
import { getReport, updateReportlist } from '@/api/basicConfig'
export default {
  name: 'ReportNameEdit',
  data() {
    return {
      form: {
        id: '',
        name: '',
        category: ''
      },
      typeList: [
        { dataName: '能源', id: 1 },
        { dataName: '订单', id: 2 },
        { dataName: '设备', id: 3 },
        { dataName: '人员', id: 4 }
      ],
      rules: {
        name: [{ required: true, message: '请输入报表分类名', trigger: 'blur' }]
        // category: [{ required: true, message: '请选择分类', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (id) {
        this.form.id = id
        getReport({ id }).then((res) => {
          if (res.code === 0) {
            this.form.category = res.data.category
            this.form.name = res.data.name
          }
        })
      }
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          //编辑
          updateReportlist({ ...this.form }).then((res) => {
            if (res.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.$emit('successSubmit')
            }
          })
          this.$emit('successSubmit')
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
