<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>
                <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">
            <u-subsection
              :list="subsection"
              mode="button"
              activeColor="#3c9cff"
              :current="subIndex"
              @change="changeSub"
            ></u-subsection>
          </div> -->

          <div class="tab" v-show="update0">
            <u-subsection
              :list="subsection0"
              mode="button"
              activeColor="#3c9cff"
              :current="subIndex0"
              @change="changeSub0"
            ></u-subsection>
          </div>

          <div class="tab" v-show="update1">
            <u-subsection
              :list="subsection1"
              mode="button"
              activeColor="#3c9cff"
              :current="subIndex1"
              @change="changeSub1"
            ></u-subsection>
          </div>

          <AiTable
            :data="tableData"
            :colConfigs="tabIndex == 0 ? colConfigs0 : colConfigs1"
            v-if="tableData.length"
          >
            <u-td slot="groupOwnerId" slot-scope="{ row }" style="border: none;">
              <AiOpenData
                v-if="row.groupOwnerId"
                type="userName"
                :openid="row.groupOwnerId"
              />
            </u-td>
          </AiTable>
          <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,
      subIndex0: 0,
      subIndex1: 0,
      tableData: [],
      createTime: "",
      id: "",
      info: {},
      current: 1,
      firstClickTime: "",
      currentClickTime: "",
      flag: true,
      detail: {},
      // subsection: [
      //   { name: "未执行" },
      //   { name: "已执行" },
      //   { name: "无法执行" },
      // ],

      subsection0: [
        { name: "未执行" },
        { name: "已执行" },
        { name: "无法执行" },
      ],
      subsection1: [
        { name: "未送达" },
        { name: "已送达" },
        { name: "无法送达" },
      ],
      update0: true,
      update1: false,
    };
  },
  computed: {
    colConfigs0() {
      return [
        { slot: "groupOwnerId", label: "成员" },
        { label: "预计送达居民群", prop: "groupCount" },
      ];
    },
    colConfigs1() {
      return [
        { label: "居民群", prop: "groupName" },
        { label: "群人数", prop: "memberCount" },
        { slot: "groupOwnerId", label: "群主" },
      ];
    },
  },
  onLoad(o) {
    this.id = o.id;
    this.createTime = o.time;
  },
  methods: {
    tabClick(index) {
      this.tabIndex = index;
      if(this.tabIndex == 0) {
          this.subsection = [
          { name: "未执行" },
          { name: "已执行" },
          { name: "无法执行" },
        ]
        this.update0 = true
        this.update1 = false
        this.$forceUpdate()
      } else if(this.tabIndex == 1) {
          this.subsection = [
          { name: "未送达" },
          { name: "已送达" },
          { name: "无法送达" },
        ]
        this.update0 = false
        this.update1 = true
        this.$forceUpdate()
      }
      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() {
      this.firstClickTime = new Date(this.detail.remindTime).getTime() || "";
      this.currentClickTime = +new Date();
      let time = this.currentClickTime - this.firstClickTime;
      if (time >= 60 * 60 * 1000 && this.flag) {
        console.log(time, 'time');
        console.log(60 * 60 * 1000, '60 * 60 * 1000');
        console.log(this.flag, 'flag');
        this.$http.post("/app/appmasssendingtask/remindSend", null, {
            params: {
              id: this.id,
            },
          })
          .then((res) => {
            if (res?.code == 0) {
              this.$u.toast("已提醒成员发送");
              this.flag = false;
              this.getDetail()
            }
          })
          .catch(() => {});
      } else {
        time = 60 * 60 * 1000 - time;
        const min = Math.floor(time / 1000 / 60); // 分钟
        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}秒后可以再次点击`;
        }
        setTimeout(()=> {
          this.$u.toast(msg);
        }, 600)
      }
    },
    // 切换分段器
    // changeSub(index) {
    //   this.subIndex = index;
    //   this.getStatistics();
    // },
    changeSub0(index) {
      this.subIndex0 = index;
      this.getStatistics();
    },
    changeSub1(index) {
      this.subIndex1 = index;
      this.getStatistics();
    },
    // 获取数据
    getStatistics() {
      this.$http
        .post(`/app/appmasssendingtask/detailStatistics`, null, {
          params: {
            type: this.tabIndex,
            sendStatus: this.tabIndex==0? this.subIndex0: this.subIndex1,
            taskId: this.id,
            current: this.current,
          },
        })
        .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: {
          trigger: "item",
        },
        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 }) => {
                      if (this.tabIndex == 0) {
                        return `{name|任务达成率\n\n}{value|${value / this.info.planCount }%}`;
                      } else {
                        return `{name|群发送达率\n\n}{value|${value / this.info.planCount }%}`;
                      }
                    },
                    textStyle: {
                      fontSize: 16,

                    },
                    rich: {
                      name: {
                        color: "#999",
                        
                      },
                      value: {
                        color: "#000000",
                        fontSize: 26,
                      },
                    },
                  },
                },
              },
              { value: this.info.unExecutedCount, name: "未送达数量" },
            ],
          },
        ],
      });
    },
  },
  onShow() {
    document.title = "群发居民群统计";
    this.$dict.load("mstStatus")
    this.getStatistics();
    this.getDetail();
  },
  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;
        }
      }

      .sub-select {

      }
    }
  }
}
</style>