<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="fileFlow.title"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
    :min-height="250"
  >
    <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 { fileaddOrUpdate, uploadApi, getByKey } from '/@/api/media';
  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,
        title: '编辑',
      });
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          show: false,
          label: 'id',
          required: false,
        },
        {
          field: 'uploadId',
          component: 'Input',
          show: false,
          label: 'id',
          required: false,
        },
        {
          field: 'name',
          component: 'Input',
          // defaultValue: '场景标题',
          label: '名称',
          colProps: {
            span: 18,
          },
          componentProps: {
            placeholder: '请输入名称',
            showCount: true,
            maxLength: 100,
            showWordLimit: true,
          },
          required: true,
        },
        {
          field: 'dictId',
          label: '分组',
          component: 'ApiSelect',
          required: true,
          componentProps: {
            api: getByKey,
            style: { maxWidth: '250px' },
            labelField: 'dictName',
            listHeight: 150,
            valueField: 'id',
            params: {
              type: 1,
            },
          },
          itemProps: {
            autoLink: false,
          },
          colProps: {
            xl: 18,
            xxl: 18,
          },
        },
      ];
      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(data);
      }
      const handleSubmit = async () => {
        try {
          const params = await validate();
          const res = await fileaddOrUpdate(params);
          console.log('res', res);
          closeModal();
          resetFields();
          createMessage.success('操作成功。');
          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>
