<template>
  <BasicModal
    v-bind="$attrs"
    @cancel="resetFields"
    @register="register"
    title="编辑场景"
    @ok="handleOk"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }">
          {{ model[field] }}
        </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { UpdateApi } from '/@/api/scene/list';
  import { uploadLiveApi } from '/@/api/scene/live';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { t } = useI18n();

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['reload', 'register'],
    setup(_, context) {
      const modelRef = ref({
        toUserId: 0,
        userId: 0,
      });
      const options = ref([]);
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'sceneNum',
          component: 'Input',
          label: 'sceneNum',
          show: false,
        },
        {
          field: 'sceneName',
          component: 'Input',
          label: '场景名称',
          slot: 'text',
          colProps: {
            span: 22,
          },
        },
        {
          field: 'picList',
          label: '场景封面',
          component: 'Upload',
          required: true,
          itemProps: {
            validateTrigger: 'blur',
          },
          componentProps: {
            api: uploadLiveApi,
            maxSize: 5,
            emptyHidePreview: true,
            maxNumber: 1,
            accept: ['jpg', 'jpeg', 'gif', 'png'],
            afterFetch: function (data) {
              Reflect.set(data, 'url', data.message.url);
              return data;
            },
          },

          colProps: {
            span: 20,
          },
        },
        {
          field: 'isShow',
          component: 'RadioGroup',
          label: '是否显示在小程序',
          required: true,
          defaultValue: 0,
          componentProps: {
            options: [
              {
                label: '是',
                value: 1,
                key: 1,
              },
              {
                label: '否',
                value: 0,
                key: 0,
              },
            ],
          },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 150,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 20,
        },
      });

      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function onDataReceive(data) {
        data = unref(data);
        console.log('onDataReceive', data);
        setFieldsValue({
          ...data,
          sceneNum: data.num,
          picList: [data.appListPicUrl],
        });
        // 方式1;
      }

      async function handleOk() {
        let data = await validate();
        console.log('data', data);
        let res = await UpdateApi({
          ...data,
          appListPicUrl: data.picList[0],
        });
        context && context.emit('reload', res);
        createMessage.success(t('common.optSuccess'));
        closeModal();
        resetFields();
      }

      return { options, register, registerForm, model: modelRef, handleOk, resetFields };
    },
  });
</script>
