actionsDialog.vue 3.89 KB
<template>
  <el-dialog :title="$t('resource.' + textMap[dialogStatus])" :visible.sync="dialogVisible">
    <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('resource.name')" prop="name">
        <el-input v-model="temp.name" />
      </el-form-item>
      <el-form-item :label="$t('resource.resourceType')">
        <el-select v-model="temp.typeId" class="filter-item" placeholder="Please select" @change="resourceChangeHandle">
          <el-option
            v-for="item in resourceTypeOptions"
            :key="item.unid"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('resource.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('resource.' + item.label)" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('resource.description')" prop="description">
        <el-input v-model="temp.description" :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 { createResource, updateResource } from '@/api/resource'
export default {
  props: {
    resourceTypeOptions: {
      type: Array,
      required: true,
      default: () => []
    }
  },
  data() {
    return {
      dialogStatus: '',
      dialogVisible: false,
      statusOptions: [{ label: 'online', key: 1 }, { label: 'offline', key: 0 }],
      textMap: {
        update: 'edit',
        create: 'add'
      },
      temp: {
        name: '',
        typeUnid: '',
        typeId: null,
        status: 1,
        description: ''

      },
      rules: {
        name: [{ required: true, message: 'name 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 = {
        name: '',
        typeUnid: '',
        typeId: null,
        status: 1,
        description: ''
      }
    },
    resourceChangeHandle(val) {
      this.resourceTypeOptions.forEach(item => {
        if(item.id == val) {
          this.temp['typeUnid'] = item.unid;
        }
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createResource(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)
          updateResource(this.temp.id, tempData).then((response) => {
            this.dialogVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.$parent.getList()
          })
        }
      })
    }
  }
}
</script>

<style>

</style>