actionsDialog.vue 3.77 KB
<template>
  <el-dialog :title="$t('template.' + textMap[dialogStatus])" :visible.sync="dialogVisible" width="30%">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
      <el-form-item :label="$t('template.title')" prop="title">
        <el-input v-model="temp.title" />
      </el-form-item>
      <el-form-item :label="$t('template.primary_industy')" prop="primaryIndustry">
        <el-input v-model="temp.primaryIndustry" />
      </el-form-item>
      <el-form-item :label="$t('template.deputy_industry')" prop="deputyIndustry">
        <el-input v-model="temp.deputyIndustry" />
      </el-form-item>
      <el-form-item :label="$t('template.status')">
        <el-select v-model="temp.status" class="filter-item" placeholder="Please select">
          <el-option v-for="item in statusOptions" :key="item.key" :label="$t('template.' + item.label)" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('template.content')" prop="content">
        <el-input v-model="temp.content" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" />
      </el-form-item>
      <el-form-item :label="$t('template.example')" prop="example">
        <el-input v-model="temp.example" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">
        {{ $t('dialog.cancel') }}
      </el-button>
      <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
        {{ $t('dialog.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createTemplate, updateTemplate } from '@/api/template'
export default {
  data() {
    return {
      dialogStatus: '',
      dialogVisible: false,
      statusOptions: [{ label: 'online', key: 1 }, { label: 'offline', key: 0 }],
      textMap: {
        update: 'edit',
        create: 'add'
      },
      temp: {
        title: '',
        primaryIndustry: '',
        deputyIndustry: '',
        content: '',
        example: '',
        status: 1
      },
      rules: {
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      }
    }
  },
  methods: {
    dialogInit(dialogStatus, row) {
      this.dialogStatus = dialogStatus;
      this.dialogStatus === 'create' ? this.resetTemp() : this.temp = Object.assign({}, row)
      this.dialogVisible = true;
      if(this.dialogStatus === 'create') {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      }
    },
    resetTemp() {
      this.temp = {
        title: '',
        primaryIndustry: '',
        deputyIndustry: '',
        content: '',
        example: '',
        status: 1
      }
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createTemplate(this.temp).then((response) => {
            this.dialogVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.$parent.getList()
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateTemplate(this.temp.id, tempData).then((response) => {
            this.dialogVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.$parent.getList()
          })
        }
      })
    }
  }
}
</script>

<style>

</style>