<template>
  <div class="sub_title">任务区</div>
  <div class="task-area-div" id="task-area-div">
    <template v-for="(item,index) in taskList" :key="index">
      <div class="task-big-item interval" v-if="index < 3">
        <div class="left-item">
          <div class="item-title">数据中心</div>
          <div class="item-sub-title">{{ item.TaskName }}</div>
          <div class="progress">
            <a-progress 
              style="margin: 0;" 
              :percent="item.CompleteCount / item.ShpCount * 100" 
              :show-info="false"
              :size="10"/>
            <div class="progress-mark"></div>
            <span class="progress-span">{{ `${item.CompleteCount} / ${item.ShpCount}` }}</span>
          </div>
          <div class="item-button" @click="showSelectModal(item)">
            <span class="button-span">领取</span>
            <span class="button-span-shadow">领取</span>
          </div>
        </div>
        <div class="right-item">
          <div class="item-mark"></div>
        </div>
      </div>
      <div class="task-small-item interval" v-else>
        <div class="mark-out-div">
          <div class="item-mark"></div>
          <div class="span-div">
            <div class="title">{{ item.TaskName }}</div>
            <div class="progress">
              <a-progress 
                style="margin: 0;" 
                :percent="item.CompleteCount / item.ShpCount * 100" 
                :show-info="false"
                :size="10"/>
              <div class="progress-mark"></div>
              <span  class="progress-span">{{ `${item.CompleteCount} / ${item.ShpCount}` }}</span>
            </div>
          </div>
        </div>
        <div class="button-div">
          <div class="button-item" @click="showSelectModal(item)">
            <span class="button-span">领取</span>
            <span class="button-span-shadow">领取</span>
          </div>
        </div>
      </div>
    </template>
  </div>
  <a-modal
    v-model:open="openSelectTaskModal"
    style="top: 311px"
    :footer="null"
    :getContainer="getContainer"
    :maskClosable="false"
  >
    <div class="close-button" @click="closeModal"></div>
    <div class="modal-title">{{selectModalItemName}}</div>
    <div class="modal-content">
      <div 
        :class="`select-item ${submitSelectListId.includes(item.id)? 'select-item-check': ''}`"
        v-for="(item,index) in selectModalList"
        :key="index"
        @click="selectItem(item.id)">
        {{ `${item.beginNum}~${item.endNum}` }}
      </div>
    </div>
    <div style="display: flex; justify-content: flex-end;height: 43px;padding-right: 27px;">
      <div class="submit-button" @click="submitTask">
        <span class="button-span">提交</span>
        <span class="button-span button-span-shadow">提交</span>
      </div>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, createVNode } from "vue"
import { GetTaskList, GetgrouptaskList, ReciveTask } from '@/api/demo/taskhall'
import { Item, SelectModalItem } from './util'
import { message, Modal } from "ant-design-vue"
import { taskHallSignal as signal } from '@/utils/signalR';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue'

onMounted(() => {
  getValue()
  signal.start().then(() => {
    console.log('SignalR Connected');
    signal.on("RevMsg", (user, message, time, id, issystem) => {
      let messageData = JSON.parse(message);
      if (user == 'InsTask') {
        getValue()
        if(selectModalItemId.value == messageData.queryid){
          GetgrouptaskList({id: selectModalItemId.value}).then(res => {
            selectModalList.value = res
            let newIdlist = res.map(item => item.id)
            submitSelectListId.value = submitSelectListId.value.filter(item => newIdlist.includes(item))
          })
        }
      }
    });
  })
  signalConnection.value = signal
  
})
onBeforeUnmount(() => {
  if(signalConnection.value){
    signalConnection.value.stop().then(() => console.log('SignalR Disconnected'))
      .catch(err => console.error('Error stopping connection:', err));
  }
})

const getValue = () => {
  GetTaskList().then(res => {
    taskList.value = res
  })
}

const taskList: any = ref<Item[]>([])
const openSelectTaskModal = ref(false)
const selectModalList = ref<SelectModalItem[]>([])
const submitSelectListId = ref<string[]>([])
const signalConnection = ref()
const selectModalItemName = ref('')
const selectModalItemId = ref('')

const getContainer = () => {
  return document.getElementById('task-area-div')
}
const showSelectModal = (item) => {
  selectModalItemName.value = item.TaskName
  GetgrouptaskList({id: item.Id}).then(res => {
    selectModalList.value = res
    openSelectTaskModal.value = true
    selectModalItemId.value = item.Id
  })
}
const selectItem = (id) => {
  if(submitSelectListId.value.includes(id)){
    submitSelectListId.value = submitSelectListId.value.filter(item => item !== id)
  }else{
    submitSelectListId.value.push(id)
  }
}
const submitTask = () => {
  if(submitSelectListId.value.length < 1){
    message.warning('请选择后提交')
    return
  }
  Modal.confirm({
    title: '确定进行提交吗?',
    icon: createVNode(ExclamationCircleOutlined),
    onOk() {
      ReciveTask(submitSelectListId.value).then(res => {
        if(res){
          message.success('领取成功')
          closeModal()
        }
      })
    },
    onCancel() {},
  });
}
const closeModal = () => {
  openSelectTaskModal.value = false
  submitSelectListId.value = []
  selectModalItemName.value = ''
  selectModalItemId.value = ''
}
</script>

<style lang="scss" scoped>
.select-task-modal{
  background-color: #036DFF;
}
.task-area-div{
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 10px;
  height: 580px;
  align-content: flex-start;
  overflow: auto;
  scrollbar-width: none; 
  ::-webkit-scrollbar {
    width: 0;
    height: 0;
  }
  :deep(.ant-modal-content){
    padding: 0px;
    width: 481px;
    height: 355px;
    background-image: url('/public/taskhall/task_area_select_modal.png');
    background-repeat: no-repeat;
    background-color: transparent;
  }
  :deep(.ant-modal-close){
    display: none;
  }
}
:deep(.ant-progress-inner){
  background-color: rgba(0, 0, 0, 0.2);
}
.task-big-item{
  width: 418px;
  height: 239px;
  background-image: url('/public/taskhall/item_big_background.png');
  background-size: contain;
  background-repeat: no-repeat;
  margin-bottom: 14px;
  padding-top: 26px;
  padding-left: 27px;
  display: flex;
  .left-item{
    width: 228px;
    margin-right: 36px;
    .item-title{
      font-family: AlibabaPuHuiTiR;
      font-weight: 400;
      font-size: 30px;
      color: #FFFFFF;
      margin-bottom: 12PX;
    }
    .item-sub-title{
      font-family: AlibabaPuHuiTiR;
      font-weight: 400;
      font-size: 22px;
      color: #FFFFFF;
      margin-bottom: 25px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .progress{
      width: 228px;
      height: 10px;
      display: flex;
      align-items: center;
      position: relative;
      margin-bottom: 33px;
      :deep(.ant-progress-bg){
        background-image: linear-gradient(0deg, #00FF95 0%, #7FFFCA 100%) !important;
      }
      .progress-span{
        position: absolute;
        top: -18px;
        left: 50%;
        transform: translate(-50%,0px);
        font-family: DINPro;
        font-weight: 500;
        font-size: 18px;
        color: #FFFFFF;
        user-select: none;
      }
    }
    .item-button{
      width: 145px;
      height: 53px;
      background-image: url('/public/taskhall/big_item_button.png');
      box-shadow: 0px 5px 13px 0px rgba(7,112,255,0.38);
      border-radius: 26px;
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;
      cursor: pointer;
      .button-span{
        font-family: AlibabaPuHuiTiM;
        font-weight: 500;
        font-size: 24px;
        color: #FAFDFF;
        background: linear-gradient(#E5E5E5, #4fadff);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        position: relative;
        z-index: 2;
        user-select: none;
      }
      .button-span-shadow{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-family: AlibabaPuHuiTiM;
        font-weight: 500;
        font-size: 24px;
        color: rgb(250, 253, 255, 0);
        text-shadow: 0px 2px 4px rgba(0,40,45,1);
        z-index: 1;
        user-select: none;
      }
    }
  }
  .right-item{
    padding-top: 23px;
    .item-mark{
      width: 69px;
      height: 78px;
      background-image: url('/public/taskhall/big_item_mark.png');
    }
  }
}
.task-small-item{
  width: 418px;
  height: 99px;
  background-image: url('/public/taskhall/item_small_background.png');
  background-size: contain;
  background-repeat: no-repeat;
  margin-bottom: 10px;
  padding-top: 11px;
  padding-left: 16px;
  display: flex;
  .mark-out-div{
    width: 270px;
    display: flex;
    .item-mark{
      width: 69px;
      height: 78px;
      background-image: url('/public/taskhall/small_item_mark.png');
      margin-right: 12px;
    }
    .span-div{
      padding-top: 12px;
      width: calc(100% - 81px);
      .title{
        font-family: AlibabaPuHuiTiR;
        font-weight: 400;
        font-size: 20px;
        color: #FFFFFF;
        margin-bottom: 16px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .progress{
        width: 124px;
        height: 10px;
        // border-radius: 5px;
        // background: linear-gradient(268deg, #036DFF 0%, #7AD3FF 100%);
        position: relative;
        display: flex;
        align-items: center;
        // justify-content: center;
        // line-height: 13px;
        // font-family: DINPro;
        // font-weight: 500;
        // font-size: 18px;
        // color: #FFFFFF;
        :deep(.ant-progress-bg){
          background-image: linear-gradient(268deg, #036DFF 0%, #7AD3FF 100%) !important;
        }
        .progress-span{
          position: absolute;
          top: -14px;
          left: 55%;
          transform: translate(-50%, 0px);
          font-family: DINPro;
          font-weight: 500;
          font-size: 15px;
          color: #FFFFFF;
          white-space: nowrap;
          user-select: none;
        }
      }
    }
  }
  .button-div{
    padding-top: 15px;
    .button-item{
      width: 109px;
      height: 49px;
      background-image: url('/public/taskhall/small_item_button.png');
      box-shadow: 0px 5px 13px 0px rgba(7,112,255,0.38);
      border-radius: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;
      cursor: pointer;
      .button-span{
        font-family: AlibabaPuHuiTiM;
        font-weight: 500;
        font-size: 24px;
        color: #FAFDFF;
        // text-shadow: 0px 2px 4px rgba(0,40,45,0.7);
        background: linear-gradient( #E5E5E5, rgba(79,173,255,1));
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        position: relative;
        z-index: 2;
        user-select: none
      }
      .button-span-shadow{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-family: AlibabaPuHuiTiM;
        font-weight: 500;
        font-size: 24px;
        color: rgb(250, 253, 255, 0);
        text-shadow: 0px 2px 4px rgba(0,40,45,1);
        z-index: 1;
        user-select: none;
      }
    }
  }
  
}
.interval:nth-child(3n-1){
  margin-right: 20px;
}
.interval:nth-child(3n-2){
  margin-right: 20px;
}
.progress-mark{
  position: absolute;
  width: 22px;
  height: 26px;
  background-image: url('/public/taskhall/progress_mark.png');
  top: -9px;
  left: -2px;
}
.close-button{
  position: absolute;
  top: 3px;
  right: 2px;
  width: 49px;
  height: 46px;
  background-image: url('/public/taskhall/modal_close.png');
  cursor: pointer;
}
.modal-title{
  height: 72px;
  margin-left: 7px;
  margin-right: 7px;
  padding-top: 30px;
  padding-left: 21px;
  border-bottom: 1px solid #052A6B;
  font-family: AlibabaPuHuiTiR;
  font-weight: 400;
  font-size: 24px;
  color: #42D9FF;
  line-height: 23px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 43px;
}
.modal-content{
  padding-top: 13px;
  padding-left: 29px;
  padding-right: 18px;
  height: 183px;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  overflow: auto;
  margin-bottom: 20px;
  .select-item{
    width: 208px;
    height: 52px;
    background-image: url('/public/taskhall/select_item.png');
    background-size: contain;
    background-repeat: no-repeat;
    margin-right: 9px;
    margin-bottom: 7px;
    padding-left: 64px;
    padding-top: 19px;
    font-family: DINPro;
    font-weight: 500;
    font-size: 22px;
    color: #FFFFFF;
    line-height: 17px;
    cursor: pointer;
    user-select: none;
  }
  .select-item-check{
    background-image: url('/public/taskhall/select_item_check.png');
  }
}
.submit-button{
  width: 120px;
  height: 43px;
  background-image: url('/public/taskhall/modal_submit_button.png');
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
  .button-span{
    font-family: AlibabaPuHuiTiM;
    font-weight: 500;
    font-size: 24px;
    color: #FAFDFF;
    background: linear-gradient(#E5E5E5, #4fadff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    position: relative;
    z-index: 2;
    user-select: none;
  }
  .button-span-shadow{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-family: AlibabaPuHuiTiM;
    font-weight: 500;
    font-size: 24px;
    color: rgb(250, 253, 255, 0);
    text-shadow: 0px 2px 4px rgba(0,40,45,1);
    z-index: 1;
    user-select: none;
  }
}
</style>
