<template>
  <Modal :maskClosable="false" :keyboard="false" v-model:open="data.isShowModal" :footer="null" 
         :title="$t('common.edit')" width="50%" @cancel="_close">
    <Form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 6 }" labelAlign="left" :labelWrap="true">
      <Row :gutter="24">
        <Col :span="12">
          <FormItem :label="$t('robot.messageTemplate.status')" name="status">
            <Select allow-clear showSearch :filterOption="selectFilterOption" v-model:value="form.status" 
                    allowClear :placeholder="$t('common.placeholderIn')">
              <SelectOption v-for="(item, index) in data.statusOptions" :key="index" :value="item.value" 
                            :label="item.label" :text="item.label">{{ item.label }}
              </SelectOption>
            </Select>
          </FormItem>
        </Col>
        <Col :span="24">
          <FormItem :label-col="{ span: 3 }" :label="$t('robot.messageTemplate.content')" name="content">
            <TextArea v-model:value="form.content" allowClear :placeholder="$t('common.placeholderIn')" :rows="5"/>
          </FormItem>
        </Col>
      </Row>
      <FormItem style="text-align: right">
        <Button @click="_close" :disabled="data.loading">{{ $t('common.cancel') }}</Button>
        <Button danger style="margin-left: 10px" @click="handleResetForm" :disabled="data.loading">
          {{ $t('common.reset') }}
        </Button>
        <Button type="primary" style="margin-left: 10px" @click="handleSubmit" :loading="data.loading"
                :disabled="data.loading">{{ $t('common.confirm') }}
        </Button>
      </FormItem>
    </Form>
  </Modal>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { $t } from '@vben/locales';
import { Modal, Form, Input, Select, Row, Col, Button, message } from 'ant-design-vue';
import { messageTemplate } from '#/api';

const FormItem = Form.Item;
const SelectOption = Select.Option;
const TextArea = Input.TextArea;

interface Emits {
  (e: 'close', submit: Boolean): void
}

const emits = defineEmits<Emits>()

const data = reactive({
  isShowModal: false,
  loading: false,
  statusOptions: [
    { label: $t('robot.messageTemplate.statusOptions.v0'), value: 0 },
    { label: $t('robot.messageTemplate.statusOptions.v1'), value: 1 }
  ],
  typeOptions: [
    { label: $t('robot.messageTemplate.typeOptions.notification'), value: 'notification' },
    { label: $t('robot.messageTemplate.typeOptions.verification'), value: 'verification' },
    { label: $t('robot.messageTemplate.typeOptions.announcement'), value: 'announcement' },
    { label: $t('robot.messageTemplate.typeOptions.other'), value: 'other' }
  ]
})

const form = reactive({
  uuid: null,
  type: undefined,
  code: '',
  content: '',
  status: undefined
})

// 选择框过滤方法
const selectFilterOption = (input: string, option: any) => {
  return option.text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
}

// 表单规则
const rules = reactive({
  // 编辑时部分字段不可编辑，不需要验证
})

const formRef = ref();

function handleSubmit() {
  data.loading = true;
  formRef.value.validate().then(() => {
    messageTemplate.updateByUid(form).then((res) => {
      if (res.error) {
        return;
      }
      message?.success($t('common.editSuccess'));
      _close(true);
    })
  }).finally(() => {
    data.loading = false;
  })
}

function handleResetForm() {
  formRef.value.resetFields();
}

function _close(submit = false) {
  data.isShowModal = false;
  formRef.value.resetFields();
  emits('close', submit);
}

function _open(item) {
  data.isShowModal = true;
  Object.assign(form, item);
}

defineExpose({
  _open
})
</script>