<!--
 * @Author: Devin
 * @Date: 2021-07-30 16:50:47
 * @LastEditTime: 2021-07-31 13:48:51
 * @LastEditors: Devin
 * @Description: 修改弹窗
 * email: das.devin@outlook.com
-->
<template>
  <x-modal
    :append-to-body="true"
    :destroy-on-close="true"
    :modalVisible.sync="visible"
    :wrapper-class="'edit-modal'"
    :title="modalTitle"
    :width="500"
    v-on="$listeners"
  >
    <el-form ref="editForm" :model="formData" :rules="formRules" label-width="100px">
      <el-form-item label="名称:" prop="name">
        <el-input v-model="formData.templateName" placeholder="请输入名称" maxlength="100" />
      </el-form-item>
      <el-form-item label="排序号:" prop="orderNum">
        <el-input-number v-model="formData.orderNum" placeholder="请输入排序号" min="0" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <div class="dialog-footer">
        <el-button @click="closeModal">
          取消
        </el-button>
        <el-button type="primary" @click="handleSubmit">
          确定
        </el-button>
      </div>
    </template>
  </x-modal>
</template>

<script>
import { getAuthModule, getUserId } from '@/utils'

export default {
  name: 'EditModal',
  data() {
    return {
      visible: false,
      currentRow: null,
      userData: getAuthModule(),
      mode: 'add', // 'add' 或 'edit'
      formData: {
        templateName: '',
        template_status: '',
        createdBy: '',
        orderNum: ''
      },
      formRules: {
        templateName: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        orderNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }]
      },
      statusOptions: [
        { label: '草稿', value: '草稿' },
        { label: '已发布', value: '已发布' },
        { label: '审核中', value: '审核中' }
      ]
    }
  },
  computed: {
    modalTitle() {
      return this.mode === 'add' ? '添加模板' : '修改'
    }
  },
  methods: {
    showModal(row, mode = 'edit') {
      this.mode = mode
      this.currentRow = row
      // 初始化表单数据
      if (mode === 'add') {
        this.formData = {
          templateName: '',
          template_status: '草稿'
        }
      } else {
        this.formData = {
          templateName: row.templateName || '',
          template_status: row.template_status
        }
      }
      this.visible = true
      // 清除表单验证
      this.$nextTick(() => {
        if (this.$refs.editForm) {
          this.$refs.editForm.clearValidate()
        }
      })
    },
    closeModal() {
      this.visible = false
      this.currentRow = null
      this.formData = {
        templateName: '',
        template_status: ''
      }
    },
    handleSubmit() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          const submitData = {
            mode: this.mode,
            formData: {
              ...this.formData,
              createBy: getUserId(),
              userId: getUserId()
            },
            row: this.currentRow
          }
          this.$emit('submit', submitData)
          this.closeModal()
        }
      })
    }
  }
}
</script>

<style lang="scss">
.edit-modal {
  .el-form {
    padding: 20px 0;
  }

  .dialog-footer {
    text-align: right;
    padding-top: 20px;
  }
}
</style>
