<template>
  <div class="cooperationDetail">
    <AiTopFixed>
      <div class="tab-select">
        <div
          class="item"
          :class="tabIndex == index ? 'active' : ''"
          v-for="(item, index) in tabs"
          :key="index"
          @click="tabClick(index)"
        >
          {{ item }}<span></span>
        </div>
      </div>
    </AiTopFixed>

    <div class="content">
      <div class="header">
        <div class="header_left">
          <div>{{ detail.taskTitle }} <span :class="detail.status==0? 'status0': detail.status==1? 'status1': detail.status==2? 'status2':
           detail.status == 3? 'status3':detail.status==4? 'status4': 'status5'">{{ $dict.getLabel('mstStatus', detail.status) }}</span></div>
          <div>
            创建时间: <span>{{ createTime }}</span> 
          </div>
        </div>
        <div class="header_right" @click="toDetail">查看详情</div>
      </div>

      <div class="pieEcharts">
        <div class="pie_info">
          <div class="tips" v-if="detail.status">
            数据更新于<span>{{ info.remindTime }}</span>
          </div>
          <div class="pie_card">
            <div id="pieEcharts"></div>
            <div class="pie_right">
              <div>
                <span>{{
                  tabIndex == 0 ? "计划执行成员：" : "计划送达居民群："
                }}</span>
                <span>{{ info.planCount || 0 }}</span>
              </div>
              <div>
                <span>{{
                  tabIndex == 0 ? "未执行成员：" : "未送达居民群："
                }}</span>
                <span>{{ info.unExecutedCount || 0 }}</span>
              </div>
              <div>
                <span>{{
                  tabIndex == 0 ? "已执行成员：" : "已送达居民群："
                }}</span>
                <span>{{ info.executedCount || 0 }}</span>
              </div>
              <div v-if="detail.sendChannel !=1">
                <span>{{
                  tabIndex == 0 ? "无法执行成员：" : "无法送达居民群："
                }}</span>
                <span>{{ info.cannotExecuteCount || 0 }}</span>
              </div>
            </div>
          </div>
          <div v-if="detail.status == 4" class="btn" @click="remindSend">提醒成员发送</div>
        </div>
      </div>

      <div class="list_content">
        <div class="list_card">
          <div class="tab" >
            <div class="item" :class="subIndex == index? 'bgactive': ''" v-for="(item,index) in subsection" :key="index" @click="changeSub(index)">
              {{ item }}
            </div>
          </div>
          <AiTable
            :data="tableData"
            :colConfigs="tabIndex == 0 ? colConfigs0 : colConfigs1"
            v-if="tableData.length"/>
          <AiEmpty v-if="!tableData.length" description="暂无数据"></AiEmpty>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  data() {
    return {
      tabs: ["成员统计", "居民群统计"],
      tabIndex: 0,
      pieEcharts: null,
      subIndex: 0,
      tableData: [],
      createTime: "",
      id: "",
      info: {},
      current: 1,
      firstClickTime: "",
      currentClickTime: "",
      detail: {},
      subsection: [],
      sendChannel: ''
    };
  },
  computed: {
    colConfigs0() {
      return [
        { prop: "groupOwnerName", label: "成员" },
        { label: "预计送达居民群", prop: "groupCount" },
      ];
    },
    colConfigs1() {
      return [
        { label: "居民群", prop: "groupName" },
        { label: "群人数", prop: "memberCount" },
        { prop: "groupOwnerName", label: "群主" },
      ];
    },
  },
  onLoad(o) {
    this.id = o.id;
    this.createTime = o.time;
  },
  methods: {
    tabClick(index) {
      this.tabIndex = index;
      this.subIndex = 0
      if(this.sendChannel == 1) {
        if(this.tabIndex == 0) {
          this.subsection = ["未执行","已执行"]
        } else if(this.tabIndex == 1){
          this.subsection = ["未送达","已送达"]
        }
      } else if(this.sendChannel != 1) {
        if(this.tabIndex == 0) {
          this.subsection = ["未执行","已执行","无法执行"]
        } else if(this.tabIndex == 1) {
          this.subsection = ["未送达","已送达","无法送达"]
        }
      }
      this.getStatistics();
    },

    getDetail() {
      this.$http
        .post(`/app/appmasssendingtask/queryDetailById?id=${this.id}`)
        .then((res) => {
          if (res?.data) {
            this.detail = res.data;
          }
        });
    },

    toDetail() {
      uni.navigateTo({ url: `./detail?id=${this.id}` });
    },
    // 提醒发送
    remindSend() {
      uni.getSystemInfo({
        success: (res)=>{
          if(res.platform == "ios") {
            this.firstClickTime = new Date(this.detail.remindTime.replace(/-/g, "/")).getTime() || 0
          } else {
            this.firstClickTime = new Date(this.detail.remindTime).getTime() || 0
          }
        }
      });
      this.currentClickTime = +new Date();
      let time = this.currentClickTime - this.firstClickTime;
      if (time >= 3600000) {
        this.$http.post("/app/appmasssendingtask/remindSend", null, {
            params: {
              id: this.id,
            },
          })
          .then((res) => {
            if (res?.code == 0) {
              this.$u.toast("已提醒成员发送");
              this.getDetail()
            }
          })
          .catch(() => {});
      } else {
        time = 3600000 - time;
        const min = Math.floor(time / 60000); // 分钟
        let second = Math.floor(time / 1000); // 秒
        second %= 60;
        let msg = ``;
        if (min > 0 && second > 0) {
          msg = `${min}分钟${second}秒后可以再次点击`;
        } else if (min > 0 && second <= 0) {
          msg = `${min}分钟后可以再次点击`;
        } else if (min <= 0 && second > 0) {
          msg = `${second}秒后可以再次点击`;
        }
        this.$u.toast(msg);
      }
    },
    // 切换分段器
    changeSub(index) {
      this.subIndex = index;
      this.getStatistics();
    },
    // 获取数据
    getStatistics() {
      this.$http
        .post(`/app/appmasssendingtask/detailStatistics`, null, {
          params: {
            type: this.tabIndex,
            sendStatus: this.subIndex,
            taskId: this.id,
            current: this.current,
            size: 3000
          },
        })
        .then((res) => {
          if (res?.data) {
            this.info = res.data;
            this.tableData = res.data.executedList.records;
            setTimeout(()=>{
              this.getPieEcharts();
            }, 1000)
          }
        });
    },

    // 饼图
    getPieEcharts() {
      this.pieEcharts = echarts.init(document.getElementById("pieEcharts"));
      this.pieEcharts.setOption({
        tooltip: {
          show: false,
        },
        color: ["#1684fc", "#ccc"],
        series: [
          {
            name: this.tabIndex == 0 ? "任务完成率" : "群发送达率",
            type: "pie",
            radius: ["50%", "70%"],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                position: "center",
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              {
                value: this.info.executedCount,
                name: "已执行成员",
                label: {
                  normal: {
                    show: true,
                    formatter: ({ name, value }) => {
                      let num = value / this.info.planCount * 100
                      if (this.tabIndex == 0) {
                        return num % 1==0? `{name|任务达成率\n\n}{value|${num}%}`: `{name|任务达成率\n\n}{value|${ num.toFixed(1) }%}`;
                      } else {
                        return num % 1==0? `{name|群发送达率\n\n}{value|${num}%}`: `{name|群发送达率\n\n}{value|${ num.toFixed(1) }%}`;
                      }
                    },
                    textStyle: {
                      fontSize: 16,
                    },
                    rich: {
                      name: {
                        color: "#999",
                        
                      },
                      value: {
                        color: "#000000",
                        fontSize: 26,
                      },
                    },
                  },
                },
              },
              { 
                value: this.info.planCount - this.info.executedCount ,
                name: "未执行成员"
              },
            ],
          },
        ],
      });
    },
  },
  watch: {
    detail: {
      handler(v) {
        if(v) {
          this.sendChannel = v.sendChannel
          if(v.sendChannel == 1) {
            if(this.tabIndex == 0) {
              this.subsection = ["未执行","已执行"]
              this.$forceUpdate()
            } else if(this.tabIndex == 1){
              this.subsection = ["未送达","已送达"]
              this.$forceUpdate()
            }
          } else if(v.sendChannel != 1) {
            if(this.tabIndex == 0) {
              this.subsection = ["未执行","已执行","无法执行"]
              this.$forceUpdate()
            } else if(this.tabIndex == 1){
              this.subsection = ["未送达","已送达","无法送达"]
              this.$forceUpdate()
            }
          }
        }
      },
      deep: true
    }
  },
  onShow() {
    document.title = "群发居民群统计";
    this.$dict.load("mstStatus")
    this.getDetail();
    this.getStatistics();
  },
  mounted() {
    this.getPieEcharts();
  },
};
</script>

<style lang="scss" scoped>
.cooperationDetail {
  ::v-deep .AiTopFixed .content {
    padding: 0;
  }
  ::v-deep .emptyWrap {
    border: 2px solid #d0d4dc;
    border-radius: 8px;
    padding-bottom: 20px;
  }
  .tab-select {
    width: 100%;
    height: 96px;
    line-height: 96px;
    background: #3975c6;
    display: flex;

    .item {
      flex: 1;
      text-align: center;
      font-size: 28px;
      font-family: PingFangSC-Regular, PingFang SC;
      color: #cddcf0;
    }

    .active {
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      position: relative;
      color: #fff;

      span {
        width: 48px;
        height: 4px;
        background: #fff;
        position: absolute;
        bottom: 14px;
        left: 50%;
        margin-left: -24px;
      }
    }
  }

  .header {
    // height: 160px;
    padding: 32px;
    box-sizing: border-box;
    background: #fff;
    display: flex;
    align-items: center;
    .header_left {
      width: calc(100% - 120px);
      div:first-child {
        color: #000000;
        font-size: 32px;
        font-weight: 600;
        margin-bottom: 10px;

        span {
          display: inline-block;
          padding: 0 8px;
          border: 1px solid;
          border-radius: 6px;
          font-size: 26px;
          font-weight: normal;
          margin-left: 8px;
        }
        .status0 {
          color: #FFA938 !important;
        }
        .status1 {
          color: #FF6758 !important;
        }
        .status2 {
          color: #3399FF !important;
        }
        .status3 {
          color: #FF6758 !important;
        }
        .status4 {
          color: #3399FF !important;
        }
        .status5 {
          color: #1CCEB0 !important;
        }
        .status6 {
          color: #666666 !important;
        }
      }
      div:last-child {
        color: #666666;
      }
    }
    .header_right {
      width: 120px;
      color: #5297ff;
    }
  }

  .pieEcharts {
    width: 100%;
    padding: 32px;
    box-sizing: border-box;
    .pie_info {
      background: #fff;
      border-radius: 16px;
      padding-bottom: 20px;
      box-sizing: border-box;
      .tips {
        color: #666666;
        padding: 40px 32px 0 32px;
        box-sizing: border-box;
      }
      .pie_card {
        display: flex;
        width: 100%;
        align-items: center;
        #pieEcharts {
          width: 60%;
          height: 400px;
        }
        ::v-deep .emptyWrap {
          width: 60%;
        }
        .pie_right {
          width: 40%;
          padding-right: 30px;
          box-sizing: border-box;
          div {
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
          }
        }
      }
      .btn {
        margin: 0 auto;
        width: 90%;
        height: 80px;
        line-height: 80px;
        text-align: center;
        color: #fff;
        background: #3aa0ff;
        border-radius: 8px;
      }
    }
  }

  .list_content {
    padding: 0 32px 32px 32px;
    box-sizing: border-box;
    .list_card {
      background: #fff;
      border-radius: 16px;
      padding: 30px 30px;

      .tab {
        margin-bottom: 34px;

        ::v-deep .u-subsection uni-view{
          background: #eeef !important;
        }
      }

      ::v-deep .AiTable .u-table .u-tr .u-td p{
        width: 100%;
        word-break: keep-all;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      ::v-deep .AiTable .u-table .u-tr .u-td {
        width: 30%;
      }
    }
  }

  .tab {
    height: 70px;
    background-color: #eeeeef;
    padding: 3px 4px;
    border-radius: 10px;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
    .item {
      flex: 1;
      height: 58px;
      line-height: 58px;
      align-self: center;
      color: #303133;
      text-align: center;
      border-radius: 16px;
    }
    .bgactive {
      background: #fff;
    }
  }
}
</style>