<template>
  <div class="container-wrapper">
    <div class="top">
      <div class="download_btn">
        <el-space>
          <el-form-item label="起始日期">
            <el-date-picker
                v-model="updt_time"
                type="date"
                placeholder="选择一个起始日期"
                size="default"
            />
          </el-form-item>
          <el-form-item label="">
            <el-button type="primary" v-if="canDownload && !isDownLoading" @click="start">更新目录</el-button>
            <el-button @click="stop_download" type="primary" v-else-if="!canDownload && isDownLoading">暂停下载
            </el-button>
            <el-button type="primary" v-else-if="canDownload && isDownLoading" disabled>正在暂停</el-button>
          </el-form-item>
          <el-form-item label="" class="tip">提示:因医保规定，有效期更新早6点至19点期间仅可更新30天内数据</el-form-item>
        </el-space>
      </div>
      <div>
        <el-button type="primary" >有效期更新</el-button>
        <el-button type="primary" >自付比例更新</el-button>
        <el-button type="primary" >医保限额更新</el-button>
      </div>
    </div>
    <div class="content">

      <el-card v-for="(item,index) in op_list" style="margin: 10px 0">
        <div class="text">
          <el-icon v-if="current_index==index+1" class="is-loading" color="#F60">
            <Loading/>
          </el-icon>
          <el-icon v-if="current_index>index" color="#090">
            <SuccessFilled/>
          </el-icon>
          {{ item.content }}
          <div class="tip_message" v-if="current_index==index+1">{{ tip_message }}</div>
        </div>
        <el-progress :percentage="100" v-if="current_index==index+1 &&current_index!=7" :format="format"
                     :indeterminate="true"/>
        <el-progress :percentage="up_percent" v-if="current_index==index+1 && current_index==7" :format="format"/>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">


import {ref, reactive, onMounted} from 'vue';
import {ElNotification, type TabsPaneContext} from 'element-plus'
import {post} from '@/utils/request.ts'
import { Loading, SuccessFilled } from '@element-plus/icons-vue';
const format = (percentage: number) => ('正在更新')
let updt_time = ref(getDate30DaysAgo())
let current_page = ref(1);
let total_page = ref(0);

function getDate30DaysAgo() {
  const currentDate = new Date();
  currentDate.setDate(currentDate.getDate() - 29);
  const year = currentDate.getFullYear();
  const month = String(currentDate.getMonth() + 1).padStart(2, '0');
  const day = String(currentDate.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

let up_percent = ref(0)
let op_list = reactive([
  // {
  //   content: "中西成药目录更新",
  //   op: function () {
  //     start_download(1301)
  //   }
  // },
  // {
  //   content: "中药饮片目录更新",
  //   op: function () {
  //     start_download(1302)
  //   }
  // },
  // {
  //   content: "医疗项目目录更新",
  //   op: function () {
  //     start_download(1305)
  //   }
  // },
  // {
  //   content: "医疗耗材目录更新",
  //   op: function () {
  //     start_download(1306)
  //   }
  // },
  // {
  //   content: "疾病诊断目录更新",
  //   op: function () {
  //     start_download(1307)
  //   }
  // },
  // {
  //   content: "曼特病目录更新",
  //   op: function () {
  //     start_download(1309)
  //   }
  // },
  {
    content: "有效期更新",
    op: function () {
      start_updatetime()
    }
  }
])
let current_index = ref(-1);
let start = () => {
  current_index.value = 0
  next()
}
let next = () => {
  tip_message.value = "";
  if (current_index.value < op_list.length) {

    op_list[current_index.value].op()
    current_index.value++

  } else {
    canDownload.value = true;
    current_index.value = 99
    up_percent.value = 0
    ElNotification({
      title: '提示',
      message: "更新全部完成",
      type: 'success',
    })
  }
}
let current_up_page = ref(1);
let start_updatetime = () => {
  current_up_page.value = 1;
  update_time()

}
let update_time = () => {
  post("social/directory_upinfo/get_page", {
    updt_time: updt_time.value,
    page: current_up_page.value
  }, {catch_error: true}).then((res: any) => {
    let pages = res.pages;
    let number = res.number;
    if (current_up_page.value < pages) {
      current_up_page.value++
      update_time();
      tip_message.value = "正在更新有效期，当前进度:" + current_up_page.value + "/" + pages + "本页有效数据:" + number + "条";
      up_percent.value = Math.floor((current_up_page.value / pages) * 100)
    } else {
      up_percent.value = 0
      next();
    }

  }).catch((err: any) => {
    isDownLoading.value = false;
    canDownload.value = true;
    current_index.value = -1
  })
}

let canDownload = ref(true);
let isDownLoading = ref(false);
let tip_message = ref('提示:如果更新目录，请不要强制关闭本程序，否则有可能数据不完整');
let tableData: any = ref([])
let content: any = ref(null);
onMounted(() => {
});


let start_download = (type: number) => {
  if (isDownLoading.value) {
    ElNotification({
      title: '存在未完成的下载',
      message: "存在未完成的下载任务，请不要关闭程序，并稍后重试",
      type: 'error',
    })
    return;
  }
  canDownload.value = false;
  tip_message.value = "正在获取版本信息" + ",请不要强制关闭程序，否则数据不完整";
  post("social/directory_version/get_current", {type: type}, {catch_error: true}).then((res: any) => {
    tip_message.value = "正在更新目录，上一个版本:" + res.currentVersionName + ",请不要强制关闭程序，否则数据不完整";
    download(res.currentVersionName, type);
  }).catch((err: any) => {
    isDownLoading.value = false;
    canDownload.value = true;
    current_index.value = -1
  })
}
let stop_download = () => {
  canDownload.value = true;
  ElNotification({
    title: '提示',
    message: "暂停下载成功，请不要强制关闭程序，否则数据不完整，请等待自动停止",
    type: 'success',
  })
}

function download(ver: String, type: any) {
  isDownLoading.value = true;
  post("social/directory/download", {ver: ver, type: type}, {catch_error: true})
      .then((res: any) => {
        isDownLoading.value = false;
        if (res == null) {
          next();
          return;
        }
        if (!canDownload.value) {
          start_download(type);
        } else {
          tip_message.value = "";
          ElNotification({
            title: '提示',
            message: "更新全部完成",
            type: 'success',
          })
        }
      })
      .catch((err: any) => {
        isDownLoading.value = false;
        canDownload.value = true;
        current_index.value = -1
      })
      .finally(() => {
        current_page.value = 1;
      })

}


</script>

<style scoped lang="scss">
.container {
  box-sizing: border-box;
  padding: 20px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  height: 100%;

  .top {
    .tip {
      color: #999;
    }

    width: 100%;
    height: 50px;

    :deep(.el-tabs__item) {
      font-size: 16px;
    }
  }

  .content {
    width: 100%;
    flex: 1;
    overflow: hidden;

    .tip_message {
      color: #999;
      font-size: 12px;
    }
  }

  .bottom {
    width: 100%;
    height: 60px;
    background-color: #FFF;
    box-sizing: border-box;
    padding: 10px;
    position: relative;

    .page_btn_list {
      position: absolute;
      left: 0;
      top: 10px;
    }

    .download_btn {
      position: absolute;
      right: 0;
      top: 10px;
    }
  }

  .tip_bottom {
    width: 100%;
    height: 40px;
    background-color: #FFF;
    line-height: 40px;
    color: #999;
  }
}

</style>