<!-- 获取验证字段 -->
<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="name" required>
        <el-input
          v-model="form.name"
          placeholder="请输入模板名称"
          v-trim
          maxlength="200"
        />
      </el-form-item>
      <el-form-item label="模板文件路径" prop="path" required>
        <el-input
          v-model="form.path"
          placeholder="请输入模板路径"
          v-trim
          maxlength="200"
        />
        <FormItemTip>
          模板文件路径也是模板的唯一标识，用于查询模板的相关配置。
          <a href="https://www.yuque.com/u21334242/eva-cms/wz7s8t6cmd9t4pg1" target="_blank">查看更详细的说明</a>
        </FormItemTip>
      </el-form-item>
      <el-form-item label="访问路径" prop="accessUri" required>
        <el-input
          v-model="form.accessUri"
          placeholder="如：/article/${articleUid}"
          v-trim
          maxlength="200"
        />
        <FormItemTip>
          访问路径将使用freemarker解析，作为挂载该模板数据的访问地址。
          <a href="https://www.yuque.com/u21334242/eva-cms/wz7s8t6cmd9t4pg1" target="_blank">查看更详细的说明</a>
        </FormItemTip>
      </el-form-item>
      <el-form-item v-if="visible" label="模板数据" prop="presetDataCodes">
        <DictSelect
          code="TEMPLATE_DATA_LIST"
          v-model="form.presetDataCodes"
          placeholder="请选择模板数据"
          :multiple="true"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          type="textarea"
          :rows="3"
          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'
import GlobalWindow from '@/components/common/GlobalWindow'

export default {
  name: 'OperaCmsTemplateWindow',
  extends: BaseOpera,
  components: {
    GlobalWindow
  },
  data () {
    return {
      // 表单数据
      form: {
        id: null,
        name: '',
        path: '',
        accessUri: '',
        presetDataCodes: [],
        remark: ''
      },
      // 验证规则
      rules: {
        name: [
          { required: true, message: '请输入模板名称' }
        ],
        path: [
          { required: true, message: '请输入模板文件路径' }
        ],
        accessUri: [
          { 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.presetDataCodes = target.presetDataCodes == null || target.presetDataCodes === '' ?
          [] : target.presetDataCodes.split(',')
            .filter(item => item.trim() !== '')
      })
    },
    /**
     * 获取新增编辑表单对象
     *
     * @returns {*}
     * @private
     */
    __getForm () {
      const form = JSON.parse(JSON.stringify(this.form))
      // 将预设数据的编码集转为字符串，并在前后增加“,”，便于模糊搜索
      form.presetDataCodes = ',' + form.presetDataCodes.join(',') + ','
      return form
    }
  },
  async created () {
    this.config({
      api: await import('@/api/cms/template'),
      'field.id': 'id'
    })
  }
}
</script>
