<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="t('routes.scene.transferTitle')"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
    :min-height="0"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" :model="model">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, onMounted, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { sceneMove } from '/@/api/operate';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const fileFlow = reactive({
        file: null,
      });
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'type',
          component: 'Input',
          // defaultValue: '场景标题',
          label: t('routes.scene.sceneName'),
          slot: 'text',
          colProps: {
            span: 24,
          },
          // required: true,
        },
        {
          field: 'snCode',
          component: 'Input',
          label: t('routes.scene.toSnCode'),

          required: true,
          colProps: {
            span: 16,
          },
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                if (!value) {
                  return Promise.reject(t('routes.scene.rules.snCode'));
                }
                if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
                  /* eslint-disable-next-line */
                  return Promise.reject(t('routes.scene.rules.snCode1'));
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
          componentProps: {
            placeholder: t('routes.scene.rules.snCode'),
            maxLength: 15,
            onChange: (data) => {
              console.log('data', data);
            },
          },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        console.log(data);
        data && onDataReceive(data);
      });

      function onDataReceive(data) {
        modelRef.value = data;
        resetFields();
        setFieldsValue({
          type: data.sceneName,
        });
      }
      const handleSubmit = async () => {
        try {
          const params = await validate();
          const apiData = {
            snCode: params.snCode,
            num: modelRef.value.num,
          };
          console.log('res', apiData, params);
          const res = await sceneMove(apiData);
          console.log('res', res);
          closeModal();
          resetFields();
          createMessage.success(t('routes.scene.transferSccuse'));
          emit('update');
        } catch (error) {
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        // console.log(v);
        // v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
