<template>
  <el-dialog
    :title="$t('reserveCatalogManage.edit.title')"
    :visible.sync="visible"
    width="50%"
    @close="handleClose"
  >
    <el-form
      ref="form"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item
        :label="$t('reserveCatalogManage.edit.name')"
        prop="name"
      >
        <el-input
          v-model="formData.name"
          :placeholder="$t('reserveCatalogManage.edit.namePlaceholder')"
        />
      </el-form-item>
      <el-form-item
        :label="$t('reserveCatalogManage.edit.sort')"
        prop="sort"
      >
        <el-input
          v-model="formData.sort"
          :placeholder="$t('reserveCatalogManage.edit.sortPlaceholder')"
        />
      </el-form-item>
      <el-form-item
        :label="$t('reserveCatalogManage.edit.type')"
        prop="type"
      >
        <el-select
          v-model="formData.type"
          :placeholder="$t('reserveCatalogManage.edit.typePlaceholder')"
          style="width:100%"
        >
          <el-option
            :label="$t('reserveCatalogManage.edit.typeOption1')"
            value="1001"
          />
          <el-option
            :label="$t('reserveCatalogManage.edit.typeOption2')"
            value="2002"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        :label="$t('reserveCatalogManage.edit.state')"
        prop="state"
      >
        <el-select
          v-model="formData.state"
          :placeholder="$t('reserveCatalogManage.edit.statePlaceholder')"
          style="width:100%"
        >
          <el-option
            :label="$t('reserveCatalogManage.edit.stateOption1')"
            value="1001"
          />
          <el-option
            :label="$t('reserveCatalogManage.edit.stateOption2')"
            value="2002"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        {{ $t('common.cancel') }}
      </el-button>
      <el-button type="primary" @click="handleSubmit">
        {{ $t('common.confirm') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateReserveCatalog } from '@/api/scm/reserveCatalogManageApi'
import { getCommunityId } from '@/api/community/communityApi'

export default {
  name: 'EditReserveCatalog',
  data() {
    return {
      visible: false,
      formData: {
        catalogId: '',
        name: '',
        sort: '',
        type: '',
        state: '',
        communityId: ''
      },
      rules: {
        name: [
          { required: true, message: this.$t('reserveCatalogManage.validate.nameRequired'), trigger: 'blur' },
          { max: 128, message: this.$t('reserveCatalogManage.validate.nameMaxLength'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('reserveCatalogManage.validate.sortRequired'), trigger: 'blur' },
          { max: 12, message: this.$t('reserveCatalogManage.validate.sortMaxLength'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('reserveCatalogManage.validate.typeRequired'), trigger: 'change' }
        ],
        state: [
          { required: true, message: this.$t('reserveCatalogManage.validate.stateRequired'), trigger: 'change' }
        ],
        catalogId: [
          { required: true, message: this.$t('reserveCatalogManage.validate.catalogIdRequired'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    open(row) {
      this.formData = {
        ...row,
        communityId: getCommunityId()
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    handleClose() {
      this.$refs.form.resetFields()
    },
    handleSubmit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            await updateReserveCatalog(this.formData)
            this.$message.success(this.$t('reserveCatalogManage.edit.success'))
            this.visible = false
            this.$emit('success')
          } catch (error) {
            this.$message.error(this.$t('reserveCatalogManage.edit.error'))
          }
        }
      })
    }
  }
}
</script>