<template>
  <div class="index-content">
    <div class="header">
      <Navbar :title="'文件去重'" @leftclick="goback" :hideBackNav="hideBackNav"></Navbar>
    </div>
    <div class="content">
      <p class="desc">
        文件去重可以将你存储在时光机上的重复文件清理掉，有利于节约存储空间。
      </p>
      <div class="descImg">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <div class="btnWrap">
        <van-button type="primary" class="add-btn hoverP" @click="addFold"
          >选择去重目录</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, onMounted, onBeforeUnmount,toRefs } from "vue";
import Navbar from "@/components/Navbar";
import Cookies from "js-cookie";
import { useRouter } from "vue-router";
import { toastmes } from "@/utils/index";
import { Dialog } from "vant";
import { useStore } from "vuex";
import { getStatus, getUserbyUserId } from "@/api/fileDeduplication";
import useFileSelect from "@/views/Scan/useFileSelect";
export default {
  name: "index",
  components: {
    Navbar,
  },

  setup(props, context) {
    const state = reactive({
      hideBackNav:false
    })
    const router = useRouter();
    if('isDeskApp' in router.currentRoute.value.query|| Cookies.get('isDeskApp')){
      let status = router.currentRoute.value.query.isDeskApp||Cookies.get('isDeskApp')
      Cookies.set('isDeskApp',status)
      state.hideBackNav = !!status
    }
    const store = useStore();
    // 手势返回处理
    onMounted(() => {
      if (window.history && window.history.pushState) {
        history.pushState(null, null, document.URL);
        window.addEventListener("popstate", goback, false); //false阻止默认事件
      }
    });
    onBeforeUnmount(() => {
      window.removeEventListener("popstate", goback, false); //false阻止默认事件
    });
    const goback = () => {
      if(!Cookies.get('isDeskApp')){
         if (Cookies.get("pluginListUrl")) {
          window.location.href = Cookies.get("pluginListUrl");
        } else {
          router.go(-1);
        }
      }
    };
    const addFold = () => {
      router.push({ path: "selectFile" });
    };

    const getStatusFn = () => {
      getStatus({ kid: Cookies.get("kid") }).then((res) => {
        const { data, error_code } = res;
        if (error_code !== 0) {
          toastmes(res.error_msg, "fail");
        }
        // 0：空闲状态；1：处理状态；2：获取结果状态；3：错误状态；4：被占用状态
        switch (data.status) {
          case 1:
            store.commit("selectFile/SET_DUPLICATE_DATA", {
              fileSetList: [],
              totalCount: 0,
              totalSize: 0,
            });
            router.push({ path: "scan" }); //扫描页
            break;
          case 2:
            if (!data.fileSetList.length) {
               router.push({ path: "scan" , query: { reGetStatus: 1 }}); //扫描页
            } else {
              const { getDownLoadUrls } = useFileSelect(store);
              getDownLoadUrls(data, () => {
                router.push({ path: "clearFiles" }); //清除文件页
              });
            }

            break;
          case 3:
            store.commit("selectFile/SET_DUPLICATE_DATA", {
              fileSetList: [],
              totalCount: 0,
              totalSize: 0,
            });
            router.push({ path: "scan", query: { isError: 1 } }); //扫描页
            break;
          case 4:
            getUserbyUserId({ serverUserId: data.userId }).then((res1) => {
              Dialog.alert({
                title: "提示",
                message: `“${res1.data.nickName}”正在进行文件去重操作，同一时间，仅支持一个去重任务。`,
                confirmButtonText: "我知道了",
              }).then(() => {
                // on close
              });
            });
            break;
          default:
            console.log(data.status);
        }
      });
    };
    if (router.currentRoute.value.query.reGetStatus !== "1") {
      getStatusFn();
    }
    return {
      goback,
      addFold,
      getStatusFn,
      ...toRefs(state)
    };
  },
};
</script>

<style lang="less" scoped>
.index-content {
  .content {
    .desc {
      margin-top: 89px;
      text-align: center;
      font-size: 15px;
      color: #909399;
      line-height: 22px;
      padding: 0 48px;
    }
    .descImg {
      width: 202px;
      height: 202px;
      margin: 55px auto 103px;
    }
    .btnWrap {
      display: flex;
      justify-content: center;
      .add-btn {
        width: 130px;
        height: 50px;
        background: linear-gradient(135deg, #ef8881 0%, #da291c 100%);
        border-radius: 12px;
        :deep(.van-button__text) {
          font-size: 13px;
          color: #ffffff;
          line-height: 20px;
        }
      }
    }
  }
}
</style>
