<!-- 获取验证字段 -->
<template>
  <GlobalWindow
    :title="title"
    v-model:visible="visible"
    :confirm-working="isWorking"
    width="750px"
    @confirm="confirm"
  >
    <el-form :model="form" ref="form" :rules="rules">
      <el-form-item label="唯一标识" prop="uid" required>
        <el-input
          v-model="form.uid"
          placeholder="请输入唯一标识"
          v-trim
          maxlength="50"
        />
      </el-form-item>
      <el-form-item label="组名" prop="name" required>
        <el-input
          v-model="form.name"
          placeholder="请输入组名"
          v-trim
          maxlength="50"
        />
      </el-form-item>
      <el-form-item label="支持的资源类型" prop="supportedTypes">
        <DictSelect
          v-model="form.supportedTypes"
          code="RESOURCE_TYPES"
          placeholder="请选择支持的资源类型"
          :multiple="true"
        />
        <FormItemTip>选择支持的资源类型，可避免创建资源时指定不支持的类型！</FormItemTip>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="3"
          placeholder="请输入备注"
          v-trim
          maxlength="200"
          show-word-limit
        />
      </el-form-item>
    </el-form>
  </GlobalWindow>
</template>

<script>
import BaseOpera from '@/components/base/BaseOpera'
import GlobalWindow from '@/components/common/GlobalWindow'

export default {
  name: 'OperaResourceGroupWindow',
  extends: BaseOpera,
  components: {
    GlobalWindow
  },
  data () {
    return {
      // 原始唯一标识
      originUid: null,
      // 表单数据
      form: {
        id: null,
        uid: '',
        name: '',
        supportedTypes: [],
        remark: ''
      },
      // 验证规则
      rules: {
        uid: [
          { required: true, message: '请输入唯一标识' }
        ],
        name: [
          { required: true, message: '请输入组名' }
        ]
      }
    }
  },
  methods: {
    /**
     * 打开窗口
     *
     * @param title 窗口标题
     * @param target 行对象（仅编辑需该参数）
     */
    open (title, target) {
      this.title = title
      this.visible = true
      // 新建
      if (target == null) {
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.form[this.configData['field.id']] = null
        })
        return
      }
      // 编辑
      this.$nextTick(() => {
        for (const key in this.form) {
          this.form[key] = target[key]
        }
        // 拷贝原始唯一标识
        this.originUid = this.form.uid
        // - 赋值支持的资源类型
        this.form.supportedTypes = target.supportedTypes == null ? [] : target.supportedTypes.split(',')
          .filter(item => item.trim() !== '')
      })
    },
    /**
     * 确认（点击确认按钮后触发）
     */
    confirm () {
      if (this.form[this.configData['field.id']] == null || this.form[this.configData['field.id']] === '') {
        this.__confirmCreate()
        return
      }
      if (this.form.uid === this.originUid) {
        this.__confirmEdit()
        return
      }
      // 修改了唯一标识
      this.$dialog.confirm('唯一标识可能会在源码中使用，修改后需及时修改源码，确认修改吗？如不确定，请咨询超级管理员！', '提示', {
        confirmButtonText: '确认修改',
        confirmButtonClass: 'el-button--danger',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.__confirmEdit()
      }).catch(() => {})
    },
    // 获取新增编辑表单对象
    __getForm () {
      const form = JSON.parse(JSON.stringify(this.form))
      form.supportedTypes = form.supportedTypes.join(',')
      return form
    }
  },
  async created () {
    this.config({
      api: await import('@/api/cms/resource.group'),
      'field.id': 'id'
    })
  }
}
</script>
