// 添加/修改场景
<template>
  <el-dialog
    :title="isEdit ? '编辑场景' : '添加场景'"
    width="500px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form ref="form" :rules="rules" :model="form" label-position="left" label-width="120px">
      <el-form-item prop="id" style="margin-bottom: 0;">&nbsp;</el-form-item>
      <el-form-item label="场景名称" prop="sceneName" required>
        <el-input v-model="form.sceneName" />
      </el-form-item>
      <el-form-item label="场景参数" prop="trade" required>
        <el-select v-model="form.trade" class="filter-item" placeholder="请选择场景参数">
          <el-option v-for="(item, index) in tradeLists" :key="index" :label="item.value" :value="item.key" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add } from '@/api/scene'

export default {
  data() {
    return {
      form: {
        id: null,
        sceneName: null,
        trade: null
      },
      rules: {
        sceneName: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        trade: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ]
      },
      visible: false,
      submitLoading: false,
      isEdit: false,
      tradeLists: [
        { key: 'COMPANY', value: '企业 / COMPANY' },
        { key: 'K12', value: 'K12' },
        { key: 'MEDICAL_T', value: '医疗 / MEDICAL_T' },
        { key: 'COMMUNITY', value: '社区 / COMMUNITY' },
        { key: 'GOVERNMENT', value: '政务 / GOVERNMENT' },
        { key: 'TRAFFIC', value: '出行 / TRAFFIC' },
        { key: 'SCENIC', value: '景区 / SCENIC' }
      ]
    }
  },
  methods: {
    open(obj) {
      this.visible = true
      this.submitLoading = false
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (obj && Object.keys(obj).length > 0) {
          this.form = obj
          this.isEdit = true
        } else {
          this.isEdit = false
        }
      })
    },
    submit(e) {
      e.preventDefault()
      this.$refs.form.validate(async(valid) => {
        if (!valid) {
          return false
        }
        if (this.submitLoading) {
          return
        }
        this.submitLoading = true
        try {
          await add(this.form)
          this.visible = false
          this.$emit('success', this.isEdit)
        } catch (e) {
          console.info('add', e)
        } finally {
          this.submitLoading = false
        }
      })
    }
  }
}
</script>

<style scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
