<!-- 获取验证字段 -->
<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="code" required>
        <el-input
          v-model="form.code"
          placeholder="请输入配置编码"
          v-trim
        />
      </el-form-item>
      <el-form-item label="配置名称" prop="name" required>
        <el-input
          v-model="form.name"
          placeholder="请输入配置名称"
          v-trim
        />
      </el-form-item>
      <el-form-item label="值类型" prop="valueType" required>
        <DictSelect
          v-model="form.valueType"
          code="SYSTEM_CONFIG_VALUE_TYPES"
          placeholder="请选择值类型"
          :clearable="true"
        />
      </el-form-item>
      <el-form-item label="配置值" prop="value" required>
        <ConfigInput :type="form.valueType" v-model:value="form.value" placeholder="请输入配置值"/>
      </el-form-item>
      <el-form-item label="应用" prop="scopes" required>
        <DictSelect
          code="APP_IDS"
          v-model="form.scopes"
          placeholder="请选择配置应用"
          :multiple="true"
        />
        <FormItemTip>系统配置会在应用启动或访问时一次性加载到服务端和客户端，以方便代码读取！</FormItemTip>
      </el-form-item>
      <el-form-item label="权限标识码" prop="permission">
        <el-input
          v-model="form.permission"
          placeholder="请输入权限标识码"
          v-trim
          maxlength="100"
        />
        <FormItemTip>如配置需要权限控制，可输入权限标识符，并在角色管理中进行授权！</FormItemTip>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          type="textarea"
          :rows="5"
          v-model="form.remark"
          placeholder="请输入备注"
          v-trim
          show-word-limit
          maxlength="200"
        />
      </el-form-item>
    </el-form>
  </GlobalWindow>
</template>

<script>
import BaseOpera from '@/components/base/BaseOpera.vue'
import ConfigInput from '@/components/system/config/ConfigInput'

export default {
  name: 'OperaSystemConfigWindow',
  components: { ConfigInput },
  extends: BaseOpera,
  data () {
    const _this = this
    return {
      // 表单数据
      form: {
        id: null,
        code: '',
        name: '',
        value: '',
        valueType: 'TEXT',
        scopes: [_this.$c('DEFAULT_CONFIG_SCOPE')],
        permission: '',
        remark: ''
      },
      // 验证规则
      rules: {
        code: [
          { required: true, message: '请输入配置编码' }
        ],
        name: [
          { required: true, message: '请输入配置名称' }
        ],
        value: [
          { required: true, message: '请输入配置值' }
        ],
        valueType: [
          { required: true, message: '请输入值类型' }
        ],
        scopes: [
          { 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.form.scopes = this.form.scopes == null || this.form.scopes === '' ?
          [] : this.form.scopes.split(',').filter(scope => scope.trim() !== '')
      })
    },
    /**
     * 获取新增编辑表单对象
     * @returns {*}
     * @private
     */
    __getForm () {
      const form = JSON.parse(JSON.stringify(this.form))
      // 前后补充逗号，方便模糊查询
      form.scopes = `,${form.scopes.join(',')},`
      return form
    }
  },
  async created () {
    this.config({
      api: await import('@/api/system/config'),
      'field.id': 'id'
    })
  }
}
</script>
