<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="编辑"
    :confirmLoading="loading"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    :width="550"
    :min-height="680"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px" style="width:600px">
      <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 { uploadApi, tipUpdata } from '/@/api/product/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import dayjs from 'dayjs';
  import { isEmojiCharacter } from '/@/utils';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref({});
      const loading = ref(false)
      const fileFlow = reactive({
        type:null
      })
      const { createMessage } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          label: 'id',
          show: false,
        },{
          field: 'typeText',
          component: 'Input',
          label: '设备类型',
          required: true,
          slot:'text',
          componentProps: {
            maxLength: 15,
          },
          colProps: {
            span: 20,
          },
        },
        {
          field: 'version',
          component: 'Input',
          label: '版本号',
          required: true,
          componentProps: {
            maxLength: 15,
          },
          colProps: {
            span: 20,
          },
        },{
          field: 'timeList',
          label: '提示时段',
          component: 'RangePicker',
          required: true,
          componentProps: {
            format: 'YYYY-MM-DD',
            valueFormat:'YYYY-MM-DD',
            width:100,
          },
          colProps: {            
            span: 20,
          },
        },
        {
          field: 'title',
          component: 'Input',
          label: '中文标题',
          required: true,
          colProps: {
            span: 20,
          },
          componentProps: {
            maxLength: 50,
          },
        },
        {
          field: 'description',
          component: 'InputTextArea',
          required: true,
          label: t('routes.product.description_zh'),
          componentProps: {
            rows:4,
            maxLength:500,
          },
          colProps: {
            span: 20,
          },
        },
        {
          field: 'imageUrl',
          component: 'Upload',
          label: '中文图片/视频',
          // helpMessage: t('routes.corporation.uploadHelp'),
          itemProps: {
            validateTrigger: 'onBlur',
          },
          componentProps: {
            api: uploadApi,
            maxNumber: 1,
            maxSize: 1000,
            accept: ['jpeg','jpg','png','mp4'],
          },

          colProps: {
            span: 20,
          },
        },
        {
          field: 'infoUrl',
          component: 'Input',
          required: true,
          label: '中文详情链接',
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                var reg=/(http|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#\u4e00-\u9fa5]*[\w\-\@?^=%&/~\+#])/;
                if (!reg.test(value)) {
                  return Promise.reject(t('common.inputText')+'正确的地址');
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
          colProps: {
            span: 20,
          },
        },,{
          field: 'titleEn',
          component: 'Input',
          label: '英文标题',
          required: true,
          colProps: {
            span: 20,
          },
          componentProps: {
            maxLength: 50,
          },
        },
        {
          field: 'descriptionEn',
          component: 'InputTextArea',
          required: true,
          label: t('routes.product.description_en'),
          componentProps: {
            maxLength:500,
            rows:4,
          },
          colProps: {
            span: 20,
          },
        },
        {
          field: 'imageUrlEn',
          component: 'Upload',
          label: '英文图片/视频',
          // helpMessage: t('routes.corporation.uploadHelp'),
          itemProps: {
            validateTrigger: 'onBlur',
          },
          componentProps: {
            api: uploadApi,
            maxNumber: 1,
            maxSize: 1000,
            accept: ['jpeg','jpg','png','mp4'],
          },

          colProps: {
            span: 20,
          },
        },
        {
          field: 'infoUrlEn',
          component: 'Input',
          required: true,
          label: '英文详情链接',
          rules: [
            {
              required: true,
              // @ts-ignore
              validator: async (rule, value) => {
                var reg=/(http|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#\u4e00-\u9fa5]*[\w\-\@?^=%&/~\+#])/;
                if (!reg.test(value)) {
                  return Promise.reject(t('common.inputText')+'正确的地址');
                }
                return Promise.resolve();
              },
              trigger: 'change',
            },
          ],
          colProps: {
            span: 20,
          },
        },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      function renderOwnTypeLabel(type: number): string {
        switch (type) {
          case 0:
            return '四维看看';
          case 1:
            return '四维深时'
          case 2:
            return '四维全景';
          case 3:
            return '四维带看';
          case 4:
            return '四维深光';
          default:
            return '';
        }
      }
      function rendercameraTypeLabel(cameraType: number): string {
        switch (cameraType) {
          case 4:
            return t('routes.devices.cameraName.4');
          case 1:
            return t('routes.devices.cameraName.1');
          case 9:
            return t('routes.devices.cameraName.9');
          case 10:
            return t('routes.devices.cameraName.10');
          case 6:
            return t('routes.devices.cameraName.6');
          default:
            return '';
        }
      }
      function onDataReceive(data) {
        fileFlow.type = data.type
        console.log('onDataReceive',data)
        resetFields();
        setFieldsValue({
          ...data,
          typeText:renderOwnTypeLabel(Number(data.type)),
          imageUrl:data.imageUrl?[data.imageUrl]:[],
          imageUrlEn:data.imageUrlEn?[data.imageUrlEn]:[],
        });
      }
      const handleSubmit = async () => {
        loading.value = true
        try {
          const params = await validate();
          const apiData = {
            ...params,
            type:fileFlow.type,
            timeList:params.timeList.map(ele => dayjs(ele).format('YYYY-MM-DD')),
            imageUrl:params.imageUrl?.[0],
            imageUrlEn:params.imageUrlEn?.[0],
          }
          console.log('res', apiData);
          const res = await tipUpdata(apiData);
          console.log('res', res);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
          loading.value = false
        } catch (error) {
          loading.value = false
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        rendercameraTypeLabel,
        renderOwnTypeLabel,
        schemas,
        registerForm,
        model: modelRef,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        loading,
        addListFunc,
        resetFields,
        t,
      };
    },
  });
</script>
<style lang="less">
.ant-calendar-picker-input.ant-input{
  width: 300px;
}
</style>
