<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="title"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
    :min-height="0"
  >
    <div class="pt-2px pr-3px">
      <BasicForm @register="registerForm" >
        <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 { companyAdd, companyUpdate } from '/@/api/customer';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi } from '/@/api/product/index';
  import { useModalInner } from '/@/components/Modal';
  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const modelRef = ref(false);
      const fileFlow = reactive({
        file: null,
      });
      const { createMessage } = useMessage();
      const title = ref('新增客户')
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          show:false,
          label: 'id',
          required: false,
        },{
          component: 'Divider',
          label: '基本信息',
          colProps: {
            span: 24,
          },
        },
        {
          field: 'companyName',
          component: 'Input',
          label: '客户名称',
          required: true,
          defaultValue: '',
          colProps: {
            span: 24,
          },
          rules: [
            // {
            //   required: true,
            //   // @ts-ignore
            //   validator: async (rule, value) => {
            //     if (!value) {
            //       return Promise.reject('请输入相机SN码');
            //     }
            //     if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
            //       /* eslint-disable-next-line */
            //       return Promise.reject('不支持中文字符');
            //     }
            //     return Promise.resolve();
            //   },
            //   trigger: 'change',
            // },
          ],
          componentProps: {
            placeholder: '请输入',
            maxLength: 15,
            onChange: () => {
              // console.log('data', data);
            },
          },
        },
        {
          field: 'companyDesc',
          component: 'InputTextArea',
          label: '客户描述',
          defaultValue: '',
          colProps: {
            span: 24,
          },
          componentProps: {
            placeholder: '请输入内容',
            rows:4,
            maxLength: 200,
            onChange: () => {
              // console.log('data', data);
            },
          },
        },
        {
          field: 'topLogo',
          component: 'Divider',
          label: 'Logo信息',
          colProps: {
            span: 24,
          },
        },
        {
          field: 'topLogo',
          component: 'Upload',
          label: '加载LOGO',
          helpMessage:'进入场景时显示的LOGO',
          defaultValue: null,
          componentProps: {
            api: uploadApi,
            maxNumber: 1,
            maxSize: 5,
            accept: ['jpg', 'png'],
            afterFetch: function (data) {
              // Reflect.set(data, 'url', data.data);
              return data;
            },
          },
          colProps: {
            span: 22,
          },
        },
        {
          field: 'floorLogo',
          component: 'Upload',
          helpMessage:'在场景内漫游时，显示在地面的LOGO',
          label: '地面LOGO',
          defaultValue: null,
          componentProps: {
            api: uploadApi,
            maxNumber: 1,
            maxSize: 5,
            accept: ['jpg', 'png'],
            afterFetch: function (data) {
              // Reflect.set(data, 'url', data.data);
              return data;
            },
          },
          colProps: {
            span: 22,
          },
        },
        {
          field: 'qrLogo',
          component: 'Upload',
          label: '二维码LOGO',
          defaultValue: null,
          helpMessage:'显示在场景二维码中间的LOGO',
          componentProps: {
            api: uploadApi,
            maxNumber: 1,
            maxSize: 5,
            accept: ['jpg', 'png'],
            afterFetch: function (data) {
              // Reflect.set(data, 'url', data.data);
              return data;
            },
          },
          colProps: {
            span: 22,
          },
        },
        // {
        //   field: 'showLogo',
        //   component: 'RadioGroup',
        //   label: '移动端品牌LOGO',
        //   helpMessage:'四维时代提供技术支持',
        //   defaultValue: 1,
        //   componentProps: {
        //     options: [
        //       {
        //         label: '显示',
        //         value: 1,
        //       },
        //       {
        //         label: '不显示',
        //         value: 2,
        //       },
        //     ],
        //   },
        //   colProps: {
        //     span: 22,
        //   },
        // },
      ];
      const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({
        labelWidth: 150,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });

      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        console.log('data',data)
        if (data) {
          title.value =data.id?'编辑客户':'新增客户'
          modelRef.value = data.id?true:false;
          setFieldsValue({
            ...data,
            floorLogo: data.floorLogo ? [data.floorLogo] : [],
            qrLogo: data.qrLogo ? [data.qrLogo] : [],
            topLogo: data.topLogo ? [data.topLogo] : [],
          });
        }else{
          title.value ='新增客户'
        }
      });

      // function onDataReceive(data) {
      //   modelRef.value = data;
      //   resetFields();
      //   setFieldsValue({
      //     type: data.sceneName,
      //   });
      // }
      const handleSubmit = async () => {
        try {
          const params = await validate();
          console.log('params',params)
          const apiData = {
            ...params,
            floorLogo: params.floorLogo && params.floorLogo[0] || '',
            qrLogo: params.qrLogo && params.qrLogo[0] || '',
            topLogo: params.topLogo && params.topLogo[0] || '',
          };
          console.log(modelRef.value);
          let tips: string;
          if (modelRef.value) {
            // apiData.id = modelRef.value.id;
            await companyUpdate(apiData);
            tips = '修改成功。';
          } else {
            tips = '添加成功。';
            await companyAdd(apiData);
          }

          closeModal();
          resetFields();
          emit('update');
          createMessage.success(tips);
        } catch (error) {
          console.log('not passing', error);
        }
      };
      function handleVisibleChange(_) {
        // console.log(v);
        // v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        schemas,
        registerForm,
        modelRef,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        title,
        t,
      };
    },
  });
</script>
