<script setup lang="ts">
import { onMounted } from 'vue';
import { message } from 'ant-design-vue';

import SvgIcon from '@/components/SvgIcon.vue';
import { useRequest } from '@/hooks/request';
import { t } from '@/i18n';
import { downloadUserProtocol } from '@/api';
import { downloadBlob } from '@/utils';

import type { SetupProtocolForm, UseGuideStepItemProps } from '@/types';

const props = defineProps<UseGuideStepItemProps<SetupProtocolForm>>();

let fileName = '';

onMounted(() => {
  const { protocolType, protocolName } = props.form.protocolInfo;
  fileName = `${protocolType} - ${protocolName}.xlsx`;
});

const { handleRequest } = useRequest();

const downloadProtocol = () => {
  handleRequest(async () => {
    const { id } = props.form.protocolInfo;

    if (id) {
      const file = await downloadUserProtocol(id);
      downloadBlob(file, fileName);
      message.success(t('setupProtocol.downloadProtocolSuccessful', { name: fileName }));
    }
  });
};
</script>

<template>
  <div class="finish-container">
    <div class="finish-bg">
      <div class="finish-circle">
        <div class="circle-first-line"></div>
        <div class="circle-second-line"></div>
      </div>
      <div class="finish-desktop">
        <div class="desktop-header">
          <span class="desktop-header-dot" v-for="i in 3" :key="i"></span>
        </div>
        <div class="desktop-content">
          <div class="desktop-content-item" v-for="i in 3" :key="i">
            <div class="desktop-content-item-check">
              <SvgIcon class="desktop-content-check-icon" name="check" />
            </div>
            <div class="desktop-content-item-right">
              <li class="desktop-content-item-line" v-for="i in 2" :key="i"></li>
            </div>
          </div>
        </div>
      </div>
      <div class="finish-mobile">
        <div class="mobile-header"></div>
        <div class="mobile-content">
          <div class="mobile-content-item">
            <li class="mobile-content-item-line" v-for="i in 3" :key="i"></li>
          </div>
          <SvgIcon class="mobile-content-check-icon" name="check-circle" />
        </div>
      </div>
    </div>
    <div class="finish-tip">{{ $t('setupProtocol.protocolConfigCompleted') }}</div>
    <AButton class="finish-button" type="primary" @click="downloadProtocol">
      {{ $t('setupProtocol.downloadProtocolToLocal') }}
    </AButton>
  </div>
</template>

<style lang="scss" scoped>
.modal-guidance-modal {
  .finish-container {
    margin-top: 50px;
  }
}

.finish-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 149px;
}

.finish-bg {
  position: relative;
  width: 302px;
  height: 232px;
}

.finish-circle {
  position: absolute;
  top: 0;
  right: 0;
  width: 201px;
  height: 201px;
  background-color: var(--antd-color-primary-bg);
  border: 1px solid var(--antd-color-primary-border);
  border-radius: 50%;
}

.circle-first-line {
  position: absolute;
  top: 38px;
  left: 135px;
  width: 13.04px;
  height: 1px;
  background-color: var(--antd-color-primary);
  transform: rotate(-58deg);
}

.circle-second-line {
  position: absolute;
  top: 45px;
  left: 142px;
  width: 15.65px;
  height: 1px;
  background-color: var(--antd-color-primary);
  transform: rotate(-26.57deg);
}

.finish-desktop {
  position: absolute;
  top: 48px;
  left: 0;
}

.desktop-header {
  display: flex;
  align-items: center;
  width: 240px;
  height: 22px;
  padding-left: 16px;
  background-color: var(--antd-color-primary);
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
}

.desktop-header-dot {
  width: 6px;
  height: 6px;
  background-color: white;
  border-radius: 50%;

  & + & {
    margin-left: 6px;
  }
}

.desktop-content {
  width: 240px;
  padding: 12px;
  background-color: #fafafa;
  border: 1px solid var(--antd-color-primary);
  border-bottom-right-radius: 8px;
  border-bottom-left-radius: 8px;
}

.desktop-content-item {
  display: flex;
  align-items: center;
  height: 36px;
  padding: 6px;
  padding-right: 12px;
  background: #fff;
  border: 1px solid #d9dbe2;
  border-radius: 4px;

  & + & {
    margin-top: 8px;
  }
}

.desktop-content-item-check {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  margin-right: 8px;
  background-color: var(--antd-color-primary-bg-hover);
  border: 1px solid var(--antd-color-primary);
  border-radius: 4px;
}

.desktop-content-check-icon {
  font-size: 11px;
  font-weight: bold;
  color: var(--antd-color-primary);
}

.desktop-content-item-right {
  flex: 1;
}

.desktop-content-item-line {
  height: 3px;
  background-color: var(--antd-color-primary-bg-hover);
  border-radius: 2px;

  & + & {
    margin-top: 6px;
  }

  &:last-child {
    width: 109.2px;
  }
}

.finish-mobile {
  position: absolute;
  right: 38px;
  bottom: 0;
  width: 99px;
  height: 117px;
  padding: 12px 8px;
  background-color: rgb(255 255 255 / 10%);
  backdrop-filter: blur(4px);
  border: 1px solid var(--antd-color-primary);
  border-radius: 8px;
}

.mobile-header {
  height: 24px;
  margin-bottom: 8px;
  background-color: var(--antd-color-primary-border-hover);
  border-radius: 4px;
}

.mobile-content {
  text-align: center;
}

.mobile-content-item-line {
  height: 3px;
  background-color: var(--antd-color-primary-bg-hover);
  border-radius: 1px;

  & + & {
    margin-top: 8px;
  }

  &:last-child {
    width: 56px;
  }
}

.mobile-content-check-icon {
  margin-top: 12px;
  font-size: 24px;
  color: var(--antd-color-primary);
}

.finish-tip {
  margin-top: 16px;
  margin-bottom: 40px;
  font-size: 16px;
  line-height: 32px;
  color: var(--antd-color-text);
}

.finish-button {
  width: 160px;
  height: 40px;
}
</style>
