<template>
  <view class="content">
    <view class="header_a">
      <view class="header">
        <img @click="isTop=true"
             src="@/assets/images/search.png"
             alt="">
        <!-- <img @click="isFont=true"
           src="@/assets/images/change.png"
           alt=""> -->
      </view>
    </view>
    <view class="zhan"></view>
    <!-- table -->
    <view class="kuang"
          v-for="(item,index) in tableData"
          :key="index">
      <table class="tftable"
             border="1">
        <tr v-if="allTxt.settleDate">
          <td align="right">{{ '结算时间' }}</td>
          <td align="left">{{ item.settleDate }}</td>
        </tr>
        <tr v-if="allTxt.registerAmount">
          <td align="right">{{ '注册金额' }}(￥)</td>
          <td align="left">{{ item.registerAmount | toThousandthAndKeepDecimal }}</td>
        </tr>
        

        <tr v-if="allTxt.registerPv">
          <td align="right">{{ '注册业绩' }}(￥)</td>
          <td align="left">{{ item.registerPv | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.upgradeAmount">
          <td align="right">{{ '升级金额' }}(￥)</td>
          <td align="left">{{ item.upgradeAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        
        <tr v-if="allTxt.upgradePv">
          <td align="right">{{ '升级业绩' }}(￥)</td>
          <td align="left">{{ item.upgradePv | toThousandthAndKeepDecimal}}</td>
        </tr>
        

        <tr v-if="allTxt.firstPurchaseAmount">
          <td align="right">{{ '首购金额' }}(￥)</td>
          <td align="left">{{ item.firstPurchaseAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        

        <tr v-if="allTxt.firstPurchasePv">
          <td align="right">{{ '首购业绩' }}(￥)</td>
          <td align="left">{{ item.firstPurchasePv | toThousandthAndKeepDecimal }}</td>
        </tr>
        <tr v-if="allTxt.firstPurchasePvDollar">
          <td align="right">{{ '首购业绩' }}($)</td>
          <td align="left">{{ item.firstPurchasePvDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.firstPurchasePvLocality">
          <td align="right">{{ '首购业绩' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.firstPurchasePvLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.repurchaseAmount">
          <td align="right">{{ '复购金额' }}(￥)</td>
          <td align="left">{{ item.repurchaseAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.repurchasePv">
          <td align="right">{{ '复购业绩' }}(￥)</td>
          <td align="left">{{ item.repurchasePv | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.amountTotal">
          <td align="right">{{ '金额总计' }}(￥)</td>
          <td align="left">{{ item.amountTotal | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.pvTotal">
          <td align="right">{{ '业绩汇总' }}(￥)</td>
          <td align="left">{{ item.pvTotal | toThousandthAndKeepDecimal}}</td>
        </tr>
      </table>
    </view>
    <view class="kuang">小计</view>
     <view class="kuang"
          v-for="(item,index) in totalPrice"
          :key="index*100 + 1">
      <table class="tftable"
             border="1">
        <tr v-if="allTxt.registerAmount">
          <td align="right">{{ '注册金额' }}(￥)</td>
          <td align="left">{{ item.registerAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.registerAmountDollar">
          <td align="right">{{ '注册金额' }}($)</td>
          <td align="left">{{ item.registerAmountDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.registerAmountLocality">
          <td align="right">{{ '注册金额' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.registerAmountLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.registerPv">
          <td align="right">{{ '注册业绩' }}(￥)</td>
          <td align="left">{{ item.registerPv | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.registerPvDollar">
          <td align="right">{{ '注册业绩' }}($)</td>
          <td align="left">{{ item.registerPvDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.registerPvLocality">
          <td align="right">{{ '注册业绩' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.registerPvLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.upgradeAmount">
          <td align="right">{{ '升级金额' }}(￥)</td>
          <td align="left">{{ item.upgradeAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.upgradeAmountDollar">
          <td align="right">{{ '升级金额' }}($)</td>
          <td align="left">{{ item.upgradeAmountDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.upgradeAmountLocality">
          <td align="right">{{ '升级金额' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.upgradeAmountLocality | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.upgradePv">
          <td align="right">{{ '升级业绩' }}(￥)</td>
          <td align="left">{{ item.upgradePv | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.upgradePvDollar">
          <td align="right">{{ '升级业绩' }}($)</td>
          <td align="left">{{ item.upgradePvDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.upgradePvLocality">
          <td align="right">{{ '升级业绩' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.upgradePvLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.firstPurchaseAmount">
          <td align="right">{{ '首购金额' }}(￥)</td>
          <td align="left">{{ item.firstPurchaseAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.firstPurchaseAmountDollar">
          <td align="right">{{ '首购金额' }}($)</td>
          <td align="left">{{ item.firstPurchaseAmountDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.firstPurchaseAmountLocality">
          <td align="right">{{ '首购金额' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.firstPurchaseAmountLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.firstPurchasePv">
          <td align="right">{{ '首购业绩' }}(￥)</td>
          <td align="left">{{ item.firstPurchasePv | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.firstPurchasePvDollar">
          <td align="right">{{ '首购业绩' }}($)</td>
          <td align="left">{{ item.firstPurchasePvDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.firstPurchasePvLocality">
          <td align="right">{{ '首购业绩' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.firstPurchasePvLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.tripartiteAmount">
          <td align="right">{{ '三方'+'金额' }}(￥)</td>
          <td align="left">{{ item.tripartiteAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.tripartiteAmountDollar">
          <td align="right">{{ '三方'+'金额' }}($)</td>
          <td align="left">{{ item.tripartiteAmountDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.tripartiteAmountLocality">
          <td align="right">{{ '三方'+'金额' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.tripartiteAmountLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.tripartitePv">
          <td align="right">{{ '三方'+'业绩' }}(￥)</td>
          <td align="left">{{ item.tripartitePv | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.tripartitePvDollar">
          <td align="right">{{ '三方'+'业绩' }}($)</td>
          <td align="left">{{ item.tripartitePvDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.tripartitePvLocality">
          <td align="right">{{ '三方'+'业绩' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.tripartitePvLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.fansAmount">
          <td align="right">{{ '海粉金额' }}(￥)</td>
          <td align="left">{{ item.fansAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.fansAmountDollar">
          <td align="right">{{ '海粉金额' }}($)</td>
          <td align="left">{{ item.fansAmountDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.fansAmountLocality">
          <td align="right">{{ '海粉金额' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.fansAmountLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.hiFunPv">
          <td align="right">{{ '海粉业绩' }}(￥)</td>
          <td align="left">{{ item.hiFunPv | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.hiFunPvDollar">
          <td align="right">{{ '海粉业绩' }}($)</td>
          <td align="left">{{ item.hiFunPvDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.hiFunPvLocality">
          <td align="right">{{ '海粉业绩' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.hiFunPvLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.repurchaseAmount">
          <td align="right">{{ '复购金额' }}(￥)</td>
          <td align="left">{{ item.repurchaseAmount | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.repurchaseAmountDollar">
          <td align="right">{{ '复购金额' }}($)</td>
          <td align="left">{{ item.repurchaseAmountDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.repurchaseAmountLocality">
          <td align="right">{{ '复购金额' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.repurchaseAmountLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.repurchasePv">
          <td align="right">{{ '复购业绩' }}(￥)</td>
          <td align="left">{{ item.repurchasePv | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.repurchasePvDollar">
          <td align="right">{{ '复购业绩' }}($)</td>
          <td align="left">{{ item.repurchasePvDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.repurchasePvLocality">
          <td align="right">{{ '复购业绩' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.repurchasePvLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.amountTotal">
          <td align="right">{{ '金额总计' }}(￥)</td>
          <td align="left">{{ item.amountTotal | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.amountTotalDollar">
          <td align="right">{{ '金额总计' }}($)</td>
          <td align="left">{{ item.amountTotalDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.amountTotalLocality">
          <td align="right">{{ '金额总计' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.amountTotalLocality | toThousandthAndKeepDecimal}}</td>
        </tr>

        <tr v-if="allTxt.pvTotal">
          <td align="right">{{ '业绩汇总' }}(￥)</td>
          <td align="left">{{ item.pvTotal | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.pvTotalDollar">
          <td align="right">{{ '业绩汇总' }}($)</td>
          <td align="left">{{ item.pvTotalDollar | toThousandthAndKeepDecimal}}</td>
        </tr>
        <tr v-if="allTxt.pvTotalLocality">
          <td align="right">{{ '业绩汇总' }}({{ isLocalSymbol() }})</td>
          <td align="left">{{ item.pvTotalLocality | toThousandthAndKeepDecimal}}</td>
        </tr>
      </table>
    </view> 
    <!-- 顶部搜索 -->
    <u-popup :show="isTop"
             mode="top"
             @close="isTop=false">
      <view class="tt">
        <u--form labelPosition="left"
                 labelWidth="auto"
                 :model="select"
                 ref="uForm">
          <u-row gutter="10">
            <u-col span="6">
              <u-form-item :label="'结算期数'">
                <u--input fontSize="12"
                          v-model="select.memberSettlePeriodId"></u--input>
              </u-form-item>
            </u-col>
            <u-col span="6">
              <u-form-item :label="'币种展示'">
                <picker :range="acList"
                        range-key="label"
                        @change="bindPickerChange1">
                  <u--input disabled
                            fontSize="12"
                            disabledColor="#ffffff"
                            v-model="select.pkCurrencyVal"
                            suffixIcon="arrow-down"
                            suffixIconStyle="font-size: 12px;"></u--input>
                </picker>
              </u-form-item>
            </u-col>
          </u-row>
          <u-row gutter="10">
            <u-col span="12">
              <u-form-item :label="'结算日期'"
                           @click="isData=true">
                <u--input disabled
                          fontSize="12"
                          disabledColor="#ffffff"
                          v-model="select.operationTimeVal"
                          suffixIcon="arrow-down"
                          suffixIconStyle="font-size: 12px;"></u--input>
              </u-form-item>
            </u-col>
          </u-row>
          <u-row gutter="10">
            <u-col span="12">
              <u-form-item :label="'隶属体系'">
                <u-checkbox-group v-model="select.pkVertex">
                  <u-checkbox v-for="item in verList"
                              :key="item.pkId"
                              :name="item.pkId"
                              :label="item.vertexName">
                  </u-checkbox>
                </u-checkbox-group>
              </u-form-item>
            </u-col>
          </u-row>
          <u-row gutter="10">
            <u-col span="12">
              <u-form-item :label="'隶属团队'">
                <u-checkbox-group v-model="select.pkMemberTeam">
                  <u-checkbox v-for="item in memberteamList"
                              :key="item.pkId"
                              :name="item.pkId"
                              :label="item.teamName">
                  </u-checkbox>
                </u-checkbox-group>
              </u-form-item>
            </u-col>
          </u-row>
        </u--form>
        <view class="footer">
          <u-button @click="getList1"
                    color="#C8161D"
                    text="搜索"
                    class="btnn"></u-button>
          <u-button @click="reset"
                    text="重置"></u-button>
        </view>
      </view>
      <u-calendar @close="isData = false"
                  :closeOnClickOverlay="true"
                  :show="isData"
                  :allowSameDay="true"
                  mode="range"
                  :maxDate="calendar.maxDate"
                  :minDate="calendar.minDate"
                  :monthNum="calendar.monthNum"
                  @confirm="confirm"></u-calendar>
      </u-popup>
    <!-- 悬浮球 -->
    <drag-button />
    <export-ball ref="exportBall"

    v-hasButtons="['performanceTotalExport']"
                 @handleExport="handleExport" />
  </view>
</template>

<script>
import * as api from '@/api/settle'
import * as infor from '@/api/information'

import { isLocals, isLocalSymbol } from '@/util/numberToCurrency'
export default {
  data() {
    return {
      select: {},
      isData: false,
      isTop: false,
      setTime: 0,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      tableData: [],
      acList: [],
      verList: [],
      memberteamList: [],
      calendar: {
        minDate: '',
        maxDate: '',
        monthNum: 25,
      },
      noticeContent: '',
      allTxt: {},
      totalPrice:[]
    }
  },
  onLoad(options) {
    this.getData()
    this.chooseTimed()
    this.getUserRole()
    var s1 = this.getStartTime()
    var s2 = this.getEndTime()
    this.$set(this.select, 'startDate', s1)
    this.$set(this.select, 'endDate', s2)
    this.$set(this.select, 'operationTimeVal', s1 + ' 至 ' + s2)
  },
  onShow() {
    this.getSearch()
  },
  onPullDownRefresh() {
    this.getList1()
  },
  methods: {
    isLocalSymbol,
    isLocals,
    getStartTime() {
      let date = new Date()
      let year = date.getFullYear() // 得到当前年份
      let month = date.getDate() === 1 ? date.getMonth() : date.getMonth() + 1 // 得到当前月份（0-11月份，+1是当前月份）
      date.setDate(1) // 将当前时间的日期设置成第一天
      month = month >= 10 ? month : '0' + month // 补零
      let day = date.getDate() // 得到当前天数，实际是本月第一天，因为前面setDate(1) 设置过了
      day = day >= 10 ? day : '0' + day // 补零
      return year + '-' + month + '-' + day
    },
    getEndTime() {
      var date = new Date()
      var year = date.getFullYear() // 年
      let month = date.getDate() === 1 ? date.getMonth() : date.getMonth() + 1 // 得到当前月份（0-11月份，+1是当前月份）
      var day = date.getDate() > 1 ? date.getDate() - 1 : date.getDate() // 日
      let day1 = date.getDate()
      // 给一位数的数据前面加 “0”
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      if (day >= 0 && day <= 9) {
        day = '0' + day
      }
      if (day1 == 1) {
        var date2 = new Date()
        var year2 = date2.getFullYear()
        var month2 = date2.getMonth()
        var dates = new Date(year2, month2, 0).getDate()
        day = dates
      }
      return year + '-' + month + '-' + day
    },
    // 设置默认日期，最大值为今天，最小值为去年今天
    chooseTimed() {
      let date = new Date()
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month <= 9) {
        month = '0' + month
      }
      if (day <= 9) {
        day = '0' + day
      }
      let minyear = year - 1
      let maxyear = year + 1
      this.calendar.minDate = minyear + '-' + month + '-' + day
      this.calendar.maxDate = maxyear + '-' + month + '-' + day
    },
    getUserRole() {
      infor.getRoleMenu('performanceTotal').then((res) => {
        let obj = {}
        res.data.forEach((item) => {
          obj[item] = 1
        })
        this.allTxt = obj
      })
    },
    getData() {
      api.accountList().then((res) => {
        this.acList = res.data
      })
      // 隶属体系下拉选
      infor.getvertexValue().then((res) => {
        this.verList = res.data
      })
      //隶属团队下拉选
      infor.getmemberTeamList().then((res) => {
        this.memberteamList = res.rows
      })
    },
    bindPickerChange1(e) {
      this.$set(this.select, 'pkCurrencyVal', this.acList[e.detail.value].label)
      this.$set(this.select, 'pkCurrency', this.acList[e.detail.value].value)
    },
    // onReachBottom() {
    //   this.queryParams.pageNum++
    //   this.getSearch()
    // },
    getSearch() {
      api
        .memberStructure(Object.assign({}, this.queryParams, this.select))
        .then((res) => {
          uni.stopPullDownRefresh()
          this.tableData = res.data
          let obj = {};
          this.tableData.forEach((item) => {
            for (let key in item) {
              if(key!='settleDate'){
                let value = Number(item[key])
                key in obj ? (obj[key] += value) : (obj[key] = value)
              }
             
            }
          })
          console.log('obj',obj)
          this.totalPrice = [obj]
        })
    },
    getList1() {
      this.tableData = []
      this.queryParams.pageNum = 1
      this.isTop = false
      this.tableData = []
      this.getSearch()
    },
    reset() {
      this.select = {}
    },
    handleExport() {
      let url = 'member/manage/member-structure/export'
      let obj = Object.assign({}, this.select, this.queryParams)
      this.$refs.exportBall.handleExport(url, obj, `业绩汇总`)
    },
    confirm(e) {
      this.$set(this.select, 'startDate', e[0])
      this.$set(this.select, 'endDate', e[e.length - 1])
      this.$set(
        this.select,
        'operationTimeVal',
        e[0] + ' 至 ' + e[e.length - 1]
      )
      this.isData = false
    },
  },
}
</script>
<style lang="scss" scoped>
.content {
  background: #f9f9f9;
  // padding: 20rpx;
}
.header_a {
  position: fixed;
  width: 100%;
  z-index: 1;
}
.header {
  margin: 20rpx;
  background: #ffffff;
  box-shadow: 0px 2px 10px 0px rgba(204, 204, 204, 0.4);
  border-radius: 10px 10px 10px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 30rpx 40rpx;
  img {
    width: 24px;
    height: 24px;
  }
}
.zhan {
  height: 130rpx;
}
.kuang {
  margin: 20rpx;
  background: #f9f9f9;
  border-radius: 10px 10px 10px 10px;
  box-shadow: 0px 2px 10px 6px rgba(204, 204, 204, .8);
}
.table_f {
  display: flex;
  uni-button {
    margin: 20rpx 10rpx;
  }
}
.tftable {
  table-layout: fixed;
  font-size: 12px;
  border-collapse: collapse;
  border-radius: 10px;
  width: 100%;
  background: #fff;
  border: 1px solid #f9f9f9;
  margin-top: 20rpx;
  td {
    font-size: 11px;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight: 500;
    color: #999;
    padding: 20rpx 40rpx;
    width: 50%;
    // height: 60px;
    border-left: 1px solid #333;
    border-bottom: 1px solid #333;
    white-space: pre-wrap;
    word-wrap: break-word;
  }
  td:nth-child(odd) {
    border-left: 1px solid #f9f9f9;
  }
  tr :nth-child(2) {
    color: #333333;
  }
}
.tt {
  padding: 20rpx;
  margin-top: 40px;
}
.tt1 {
  padding: 20rpx;
  margin-top: 40px;
  // width: 60%;
}
::v-deep .u-transition {
  z-index: 998 !important;
}
.footer {
  display: flex;
  align-items: center;
  padding: 20rpx 60rpx;
  .btnn {
    margin-right: 40rpx;
  }
}
::v-deep .u-input {
  padding: 0 9px !important;
}
::v-deep .u-form-item__body__left__content__label {
  font-size: 12px;
}
::v-deep .u-form-item__body {
  padding: 5px 0;
}
::v-deep .u-checkbox-group {
  flex-wrap: wrap;
  flex-shrink: inherit;
  .u-checkbox {
    margin-bottom: 10rpx;
  }
  uni-text {
    font-size: 12px !important;
    margin-right: 10rpx;
  }
}
</style>