<template>
<i-form
  :ref="formRef"
  :model="formModel"
  :rules="formRules"
  @submit.native.prevent="onSubmit">
  <form-item prop="non_field_errors" />
  <form-item
    label="Name"
    prop="name">
    <i-input v-model="formModel.name" />
  </form-item>
  <form-item>
    <i-button
      type="primary"
      @click="onSubmit">Save</i-button>
  </form-item>
</i-form>
</template>

<script>
import FormMixin from './form';

export default {
  mixins: [FormMixin],
  data() {
    const defaultFormModel = {
      name: '',
      roomId: '',
    };
    return {
      defaultFormModel,
      formModel: { ...defaultFormModel },
      formRef: 'roomForm',
      formRules: {
        name: [],
      },
    };
  },
  methods: {
    sendRequest() {
      if (this.formType === 'update') {
        this.$matrix.client.setRoomName(this.formModel.roomId, this.formModel.name);
      } else {
        this.$matrix.client.createRoom({ name: this.formModel.name });
      }
      this.$emit('success');
    },

    modelValueToFormModel() {
      this.formModel = { name: this.modelValue.name, roomId: this.modelValue.roomId };
    },
  },
};
</script>

<style>
</style>
