<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="授权相机"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    minHeight="900px"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px zdysrk">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }"> {{ model[field] }}{{ fileFlow.type }} </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive, computed, h, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { remarkschemas, Addschemas } from './data';
  import { InvoiceRegister } from '/@/api/order';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi } from '/@/api/product/index';
  import { ResultEnum } from '/@/enums/httpEnum';
  import {
    DetailsApi,
    dincrementList,
    AddUserIncrementApi,
    UnbindCameraApi,
    getAddUserIncrementTime,
    currencyList,
  } from '/@/api/account';
  import dayjs from 'dayjs';
  import { otherInfoStore } from '/@/store/modules/other';
  import { useUserStore } from '/@/store/modules/user';

  const { t } = useI18n();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      userData: { type: Object },
    },
    emits: ['update', 'register'],
    setup(props, { emit }) {
      const otherInfo = otherInfoStore();
      const userStore = useUserStore();
      const overviewInfo = computed(() => otherInfo.getOverviewInfo);
      // const overviewInfo = getOverviewInfo() || {}
      const title = ref('新增权益');
      const fileFlow = reactive({
        file: null,
        id: '',
        type: 'down', //down-下载，equity-权益
      });
      const { createMessage, createConfirm } = useMessage();
      const schemas: FormSchema[] = [
        {
          field: 'id',
          component: 'Input',
          show: false,
          label: 'id',
        },
        {
          field: 'userId',
          component: 'Input',
          show: false,
          label: 'userId',
        },
        {
          field: 'snCode',
          component: 'Input',
          label: '相机SN码',
          required: true,
        },
      ];

      const [
        registerForm,
        { validate, getFieldsValue, resetFields, setFieldsValue, updateSchema, validateFields, scrollToField },
      ] = useForm({
        labelWidth: 120,
        schemas: schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      onMounted(() => {});
      let addListFunc = () => {};
      const [register, { closeModal }] = useModalInner((data) => {
        data && onDataReceive(data);
      });
      async function onDataReceive(data) {
        setFieldsValue({
          ...data,
          // incrementEndTime: dayjs(incrementEndTime).add(1, data.incrementTypeId == '3' ? 'month' : 'year').format('YYYY-MM-DD'),
        });
      }
      const handleSubmit = async () => {
        const submitUrl = UnbindCameraApi;
        try {
          const params = await validate();
          console.log('validate', params);
          const apiData = {
            ...params,
            incrementId: params.id,
          };
          const res = await submitUrl(apiData);
          closeModal();
          resetFields();
          createMessage.success(t('common.optSuccess'));
          emit('update');
        } catch (error) {
          console.log('not passing', error);

        }
      };
      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }
      return {
        register,
        registerForm,
        fileFlow,
        handleVisibleChange,
        handleSubmit,
        addListFunc,
        resetFields,
        t,
        title,
      };
    },
  });
</script>
<style lang="less">
  .zdysrk {
    .ant-calendar-picker {
      min-width: 285px;
    }
  }
</style>
