<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    @ok="handSubmit"
    :title="t('routes.config.setconfig')"
    :min-height="100"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { ref, defineEmits } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { encodeStr } from '/@/utils/encodeUtil';
  import { defineComponent, nextTick, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { updateApi } from '/@/api/staff/list';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['reload'],
    setup(props, { emit }) {
      const { t } = useI18n();
      const preventAutoFill = ref(true);
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },
        {
          field: 'serial',
          component: 'Input',
          label: t('routes.config.serial'),
          required: true,
          componentProps: {
            maxLength: 100,
          },
          colProps: { span: 18 },
        },
        {
          field: 'account',
          component: 'Input',
          label: t('routes.config.account'),
          required: true,
          componentProps: {
            maxLength: 100,
          },
          colProps: { span: 18 },
        },
        {
          field: 'password',
          component: 'Input',
          label: t('routes.config.password'),
          required: true,
          componentProps: {
            maxLength: 100,
          },
          colProps: { span: 18 },
        },
      ];

      const [
        registerForm,
        {
          setFieldsValue,
          validate,
          resetFields,
          // setProps
        },
      ] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        setFieldsValue({
          ...data
        });
      }
      async function handSubmit() {
        const params = await validate();
        await updateApi(params);
        createMessage.success(t('common.optSuccess'));
        resetFields();
        closeModal();
        emit('reload');
      }
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

      return {
        handSubmit,
        register,
        t,
        schemas,
        registerForm,
        handleVisibleChange,
        resetFields,
      };
    },
  });
</script>
