<template>
  <div class="Organize">
    <div class="Organize-top">
      <div>
        <div class="left">
          <h2>{{ DD }}</h2>
          <div class="left-wrapper__right">
            <h3>{{ yyyyMM }}</h3>
            <p>日·数据统计</p>
          </div>
        </div>
        <div class="right" @click="isShow = true">
          <image src="../images/qiehuan.png" />
          <span>切换日期</span>
        </div>
      </div>
    </div>
    <div class="info">
      <div class="info-item info-total">
        <div class="info-item__title">
          <h2>拍照统计</h2>
        </div>
        <div class="info-total__wrapper">
          <div class="info-total__item">
            <span>未拍照</span>
            <div class="info-total__item--bottom">
              <h3>{{ photoTotal.noPhtoto }}</h3>
              <i>人</i>
            </div>
          </div>
          <div class="info-total__item">
            <span>已拍照</span>
            <div class="info-total__item--bottom">
              <h3>{{ photoTotal.userPhoto }}</h3>
              <i>人</i>
            </div>
          </div>
          <div class="info-total__item">
            <span>拍照数</span>
            <div class="info-total__item--bottom">
              <h3>{{ photoTotal.allPhoto }}</h3>
              <i>张</i>
            </div>
          </div>
        </div>
      </div>
      <div class="info-item info-work">
        <div class="info-item__title">
          <h2>考勤统计</h2>
          <image src="../images/right.png" @click="linkTo('./Attendance?date=' + date.replace(/年|月/g, '-'))" />
        </div>
        <div class="info-work__wrapper">
          <div class="top">
            <div class="left">
              <h2>{{ attendanceCount.hasIn || 0 }}</h2>
              <span>人已打卡</span>
              <i>共{{ attendanceCount.all || 0 }}人</i>
            </div>
            <span>{{ rate }}%</span>
          </div>
          <div class="progress">
            <div :style="{width: rate + '%'}"></div>
          </div>
        </div>
      </div>
      <div class="info-item info-rank">
        <div class="info-item__title">
          <h2>成员拍照排名</h2>
          <image src="../images/right.png" @click="linkTo('./PhotoRank?date=' + date.replace(/年|月/g, '-'))" />
        </div>
        <div class="info-rank__wrapper">
          <div class="rank-item" v-for="(item, index) in list" :key="index">
            <div class="rank-item__left">
              <image src="../images/rank1.png" v-if="index === 0" />
              <image src="../images/rank2.png" v-else-if="index === 1" />
              <image src="../images/rank3.png" v-else-if="index === 2" />
              <span v-else>{{ index + 1 > 9 ? index + 1 : '0' + (index + 1) }}</span>
              <h2><AiOpenData v-if="item.name" type="userName" :openid="item.name"/></h2>
            </div>
            <div class="rank-item__right">
              <span>已上传</span>
              <i>{{ item.num }}</i>
              <span>张</span>
            </div>
          </div>
          <AiEmpty v-if="!list.length && isMore"></AiEmpty>
        </div>
      </div>
    </div>
    <u-calendar v-model="isShow" mode="date" @change="onDateChange"></u-calendar>
  </div>
</template>

<script>
  export default {
    name: 'Organize',

    data () {
      return {
        photoTotal: {},
        date: '',
        list: [],
        isMore: false,
        isShow: false,
        yyyyMM: '',
        DD: '',
        attendanceCount: {}
      }
    },

    computed: {
      rate () {
        if (!this.attendanceCount.all) {
          return '0'
        }

        return (this.attendanceCount.hasIn / this.attendanceCount.all * 100).toFixed(2)
      }
    },

    mounted () {
      this.date = this.$dayjs(new Date).format('YYYY年MM月DD')
      this.yyyyMM = this.$dayjs(new Date).format('YYYY年MM月')
      this.DD = this.$dayjs(new Date).format('DD')
      this.getPhotoTotal()
    },

    methods: {
      getPhotoTotal () {
        this.$http.post(`/api/appattendancerecord/punchclocksum?queryTime=${this.date.replace(/年|月/g, '-')}`).then(res => {
          if (res.code === 0) {
            this.photoTotal = res.data
          }
        })

        this.$http.post(`/api/appattendancerecord/attendanceCount?queryTime=${this.date.replace(/年|月/g, '-')}`).then(res => {
          if (res.code === 0) {
            this.attendanceCount = res.data
          }
        })

        this.$http.post(`/api/appattendancerecord/userphotosort?queryTime=${this.date.replace(/年|月/g, '-')}`).then(res => {
          if (res.code === 0) {
            this.list = Object.keys(res.data).map(v => {
              return {
                name: v,
                num: res.data[v]
              }
            })

            this.isMore = true
          }
        })
      },

      onDateChange (e) {
        this.date = `${e.year}年${e.month > 9 ? e.month : '0' + e.month}月${e.day > 9 ? e.day : '0' + e.day}`

        this.yyyyMM = `${e.year}年${e.month > 9 ? e.month : '0' + e.month}月`
        this.DD = e.day > 9 ? e.day : '0' + e.day

        this.$nextTick(() => {
          this.getPhotoTotal()
        })
      },

      linkTo (url) {
        uni.navigateTo({
          url
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .Organize {
    padding: 0 0 40px;

    * {
      box-sizing: border-box;
      line-height: 1;
    }

    i, em {
      font-style: normal;
    }

    .info {
      position: relative;
      top: -116px;
      padding: 0 32px;

      .info-item {
        margin-bottom: 32px;
        padding: 0 32px 32px;
        background: #FFFFFF;
        box-shadow: 0px 4px 8px 0px rgba(17, 67, 110, 0.02);
        border-radius: 16px;

        &:last-child {
          margin-bottom: 0;
        }

        .info-rank__wrapper {
          .rank-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            height: 112px;
            border-bottom: 1px solid #DDDDDD;

            &:last-child {
              border-bottom: none;
            }

            & > div {
              display: flex;
              align-items: center;
            }

            .rank-item__right {
              font-size: 28px;
              color: #999999;

              i {
                color: #2E88FF;
              }
            }

            .rank-item__left {
              image {
                width: 50px;
                height: 50px;
                margin-right: 32px;
              }

              span {
                width: 60px;
                margin-right: 22px;
                padding-left: 6px;
                color: #CCCCCC;
                font-size: 32px;
                font-weight: 600;
              }

              h2 {
                color: #333333;
                font-size: 32px;
              }
            }
          }
        }

        .info-work__wrapper {
          .top {
            display: flex;
            align-items: flex-end;
            justify-content: space-between;

            & > span {
              font-size: 32px;
              font-weight: 600;
              color: #333333;
            }

            .left {
              display: flex;
              align-items: baseline;

              h2 {
                position: relative;
                top: 6px;
                color: #333;
                font-size: 56px;
              }

              span {
                font-size: 32px;
                color: #333;
              }

              i {
                margin-left: 12px;
                font-size: 32px;
                color: #999999;
              }
            }
          }

          .progress {
            position: relative;
            height: 16px;
            margin-top: 32px;
            margin-bottom: 16px;
            background: #EEEEEE;
            border-radius: 4px;
            overflow: hidden;

            & > div {
              position: absolute;
              top: 0;
              left: 0;
              height: 16px;
              border-radius: 4px;
              background: #2E88FF;
            }
          }
        }

        .info-total__wrapper {
          display: flex;
          align-items: center;

          & > div {
            display: flex;
            justify-content: center;
            flex-direction: column;
            flex: 1;
            height: 156px;
            margin-right: 18px;
            padding: 0 24px;
            background: #FFFFFF;
            border-radius: 8px;
            border: 1px solid #E6E6E7;

            &:last-child {
              margin-right: 0;
            }

            .info-total__item--bottom {
              display: flex;
              align-items: center;
              line-height: 1;
            }

            span {
              margin-bottom: 20px;
              font-size: 28px;
              color: #333;
              font-weight: 600;
            }

            i {
              color: #333333;
              font-size: 26px;
            }
          }
        }

        .info-item__title {
          display: flex;
          align-items: center;
          justify-content: space-between;
          height: 108px;

          h2 {
            font-weight: 600;
            font-size: 32px;
            color: #333;
          }

          image {
            width: 32px;
            height: 32px;
          }
        }
      }
    }

    .Organize-top {
      height: 320px;
      width: 100%;
      padding: 58px 32px 0;
      background: #3975C6;

      & > div {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .right {
        display: flex;
        align-items: center;
        height: 56px;
        padding: 0 36px;
        background: #285DA4;
        border-radius: 28px;

        image {
          width: 36px;
          height: 26px;
          margin-right: 12px;
        }

        span {
          color: #FFFFFF;
          font-size: 28px;
        }
      }

      .left {
        display: flex;
        align-items: center;
        line-height: 1;

        h2 {
          margin-right: 16px;
          font-size: 100px;
          font-weight: 600;
          color: #FFFFFF;
        }

        h3 {
          margin-bottom: 8px;
          color: #a9c3e6;
          font-size: 28px;
        }

        p {
          font-size: 32px;
          color: #FFFFFF;
        }
      }
    }
  }
</style>
