<!-- 获取验证字段 -->
<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="title" required>
        <el-input
          v-model="form.title"
          placeholder="请输入资源标题"
          v-trim
          maxlength="200"
        />
      </el-form-item>
      <el-form-item label="资源副标题" prop="subTitle">
        <el-input
          v-model="form.subTitle"
          placeholder="请输入资源副标题"
          v-trim
          maxlength="200"
        />
      </el-form-item>
      <el-form-item label="资源值类型" prop="valueType" required>
        <DictSelect
          v-model="form.valueType"
          code="RESOURCE_TYPES"
          placeholder="请选择资源值类型"
          :only-values="supportedTypes"
          @change="form.value = ''"
        />
      </el-form-item>
      <el-form-item v-if="form.valueType != null && form.valueType !== ''" label="资源值" prop="value" required>
        <ResourceInput
          ref="resourceInput"
          :type="form.valueType"
          v-model="form.value"
          :image-tip="imageTip"
        />
      </el-form-item>
      <el-form-item label="状态" prop="disabled" required class="form-item-status">
        <el-switch
          v-model="form.disabled"
          :active-value="false"
          active-text="启用"
          :inactive-value="true"
          inactive-text="禁用"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort" required>
        <el-input-number
          v-model="form.sort"
          placeholder="请输入排序"
          :controls="false"
          :min="1"
          v-trim
          maxlength="200"
        />
      </el-form-item>
      <el-form-item label="资源描述" prop="description">
        <el-input
          type="textarea"
          :rows="3"
          v-model="form.description"
          placeholder="请输入资源描述"
          v-trim
          show-word-limit
          maxlength="200"
        />
      </el-form-item>
      <el-form-item label="资源配置" prop="config">
        <el-input
          type="textarea"
          :rows="5"
          v-model="form.config"
          placeholder="请输入资源配置"
          v-trim
          show-word-limit
          maxlength="2000"
        />
        <FormItemTip>资源配置用于通过JSON或其它形式的内容进一步对资源做设定！</FormItemTip>
      </el-form-item>
    </el-form>
  </GlobalWindow>
</template>

<script>
import BaseOpera from '@/components/base/BaseOpera'
import GlobalWindow from '@/components/common/GlobalWindow'
import ResourceInput from '@/components/cms/resource/input/ResourceInput'

export default {
  name: 'OperaResourceWindow',
  extends: BaseOpera,
  components: {
    ResourceInput,
    GlobalWindow
  },
  data () {
    return {
      // 资源组
      group: null,
      // 表单数据
      form: {
        id: null,
        groupId: '',
        title: '',
        subTitle: '',
        sort: 1,
        description: '',
        value: '',
        valueType: null,
        config: '',
        disabled: false
      },
      // 验证规则
      rules: {
        title: [
          { required: true, message: '请输入资源标题' }
        ],
        value: [
          { required: true, message: '请补充资源值' }
        ]
      }
    }
  },
  computed: {
    supportedTypes () {
      if (this.group == null) {
        return []
      }
      if (this.group.supportedTypes == null || this.group.supportedTypes.length === 0) {
        return []
      }
      return this.group.supportedTypes.split(',').filter(type => type.trim() !== '')
    }
  },
  methods: {
    /**
     * 打开窗口
     *
     * @param title 窗口标题
     * @param group 所属资源组
     * @param target 行对象（仅编辑需该参数）
     */
    open (title, group, target) {
      this.group = group
      this.title = title
      this.visible = true
      // 新建
      if (target == null) {
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.form.id = null
          this.form.groupId = group.id
        })
        return
      }
      // 编辑
      this.$nextTick(() => {
        for (const key in this.form) {
          this.form[key] = target[key]
        }
      })
    },
    /**
     * 验证表单
     *
     * @param callback 验证回调
     * @private
     */
    __validateForm (callback) {
      Promise.all([this.$refs.form.validate(), this.$refs.resourceInput.validate()])
        .then(result => {
          callback(result[0], result[1])
        })
        .catch(() => {
          callback(false)
        })
    },
  },
  async created () {
    this.config({
      api: await import('@/api/cms/resource'),
      'field.id': 'id'
    })
  }
}
</script>
<style scoped lang="scss">
:deep(.el-input-number) {
  .el-input__inner {
    text-align: left;
  }
}
:deep(.form-item-status) {
  .el-form-item__content{
    display: flex;
    & > * {
      width: auto !important;
    }
  }
  .status-text {
    color: #999;
    margin-left: 6px;
    font-size: 13px;
    vertical-align: middle;
  }
}
</style>
