<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    title="更换SN码"
    @visible-change="handleVisibleChange"
    @cancel="resetFields"
    @ok="handleSubmit"
  >
    <div class="pt-2px pr-3px zdysrk">
      <BasicForm @register="registerForm">
        <template #text="{ model, field }"> {{ model[field] }} </template>
      </BasicForm>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive, computed, h } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { authorizeCameraupdateSnCode } from '/@/api/authorizeModeling';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { uploadApi } from '/@/api/product/index';
  import { copyTextToClipboard } from '/@/hooks/web/useCopyToClipboard';
  import { ResultEnum } from '/@/enums/httpEnum';
  import { authorizeModelingaddOrUpdate } from '/@/api/authorizeModeling';
  import dayjs from 'dayjs';
  import { otherInfoStore } from '/@/store/modules/other';

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

      const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = 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) {
        resetFields();
        fileFlow.id = data.id;
        setFieldsValue(data);
      }
      async function handleConfirm() {
        const { id } = await validate();
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: () => h('span', `确定要${id ? '编辑' : '新增'}授权吗？`),
          onOk: async () => {
            handleSubmit();
          },
        });
      }
      const handleSubmit = async () => {
        try {
          const params = await validate();
          let res = await authorizeCameraupdateSnCode({...params,snCode:params.snCode.replace(/\s*/g,"")});
          closeModal();
          resetFields();
          createSuccessModal({
            title: t('layout.setting.operatingTitle'),
            content: `授权Key：${res.updateAuthorizeKey}`,
            okText: '复制',
            onOk: async () => {
              // 双击复制
              copyTextToClipboard(res.updateAuthorizeKey)
              createMessage.success('复制成功');
            },
          });
          // 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,
        handleConfirm,
        t,
      };
    },
  });
</script>
<style lang="less">
  .zdysrk {
    .ant-calendar-picker {
      min-width: 285px;
    }
  }
</style>
