<template>
  <div>
    <van-nav-bar left-arrow left-text="返回" @click-left="goBack" title="查看全部网点" />
    <!-- <input type="date"> -->
    <div style="padding:6px 16px 0px;background-color:#fff;margin-top:17px">
      <div class="tit">电子支付总金额：{{allMoney | numToFixed}}元</div>

    </div>
    <van-cell title="选择日期" :value="date" @click="show = true" is-link />
    <van-calendar v-model="show" @confirm="onConfirm" :min-date="minDate" />
    <div style="margin-top:18px;background-color:#fff;padding-top:6px">
      <van-row style="padding:0 16px">
        <van-col span="2"><span class="listTitle">排名</span></van-col>
        <van-col span="12"><span class="listTitle">网点</span></van-col>
        <van-col span="5" @click="insFn"><span class="listTitle">安装支付
            <van-icon :name="isIns?'arrow-down':'arrow-up'" />
          </span></van-col>
        <van-col span="5" @click="repairFn"><span class="listTitle">维修支付
            <van-icon :name="isrepair?'arrow-down':'arrow-up'" />
          </span></van-col>
      </van-row>
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-cell v-for="(item,i) in dataList" :key="i" @click="websiteDetail(item.webNumber,item.name)">
          <van-row>
            <van-col span="2">{{i+1}}</van-col>
            <van-col span="12">
              <div class="listTitle">{{item.name}}</div>

            </van-col>
            <van-col span="5">
              <div style="text-align:right">
                <div class="fr">{{item.amountIns | numToFixed}}</div>
              </div>
            </van-col>
            <van-col span="5">
              <div style="text-align:right">
                <div class="fr">{{item.amountRepair | numToFixed}}</div>
              </div>
            </van-col>
          </van-row>
          <van-row>
            <van-col span="2">
            </van-col>
            <van-col span="12">
              <div class="listTitle">{{item.webNumber}}</div>
            </van-col>
            <van-col span="5">
              <div style="text-align:right">
                <div v-if="item.wmrIns>0" class="upFloat"><img style="width:12px;height:12px" src="../assets/icon/shangsheng.png" alt="">{{item.wmrIns}}%</div>
                <div v-if="item.wmrIns<0" class="downFloat"><img style="width:12px;height:12px" src="../assets/icon/down.png" alt="">{{-item.wmrIns}}%</div>
              </div>
            </van-col>
            <van-col span="5">
              <div style="text-align:right">
                <div v-if="item.wmrRepair>0" class="upFloat"><img style="width:12px;height:12px" src="../assets/icon/shangsheng.png" alt="">{{item.wmrRepair}}%</div>
                <div v-if="item.wmrRepair<0" class="downFloat"><img style="width:12px;height:12px" src="../assets/icon/down.png" alt="">{{-item.wmrRepair}}%</div>
              </div>
            </van-col>
          </van-row>
        </van-cell>
      </van-list>
    </div>
  </div>
</template>

<script>
import { getUrlKey } from "@/utils/common";
export default {
  data() {
    return {
      dataList: [],
      show: false,
      minDate: new Date(2020, 1, 1),
      date: "",
      allMoney: 0,
      loading: false,
      finished: false,
      refreshing: false,
      isIns: false,
      isrepair: false,
    };
  },
  created() {
    let time = this.$route.query.date;
    console.log(time);
    this.date = time.replace(/-/g, "/");
  },
  methods: {
    //网点详情-跳转
    websiteDetail(value, name) {
      this.$router.push({
        path: "/analysis/websiteDetail",
        query: {
          time: this.date,
          website: value,
          name: name,
          type: "day",
        },
      });
    },
    //维修筛选
    repairFn() {
      this.isrepair = !this.isrepair;
      if (this.isrepair) {
        this.dataList.sort((a, b) => {
          return b.amountRepair - a.amountRepair;
        }); /* 从大到小 */
      } else {
        this.dataList.sort((a, b) => {
          return a.amountRepair - b.amountRepair;
        }); /* 从小到大 */
      }
      // this.isrepair = false;
    },
    //安装筛选
    insFn() {
      this.isIns = !this.isIns;
      if (this.isIns) {
        this.dataList.sort((a, b) => {
          return b.amountIns - a.amountIns;
        }); /* 从大到小 */
      } else {
        this.dataList.sort((a, b) => {
          return a.amountIns - b.amountIns;
        }); /* 从小到大 */
      }
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
    onLoad() {
      this.getList();
    },
    //获取数据
    getList() {
      let params = {
        openid: getUrlKey("openid", window.location.href),
        moduleId: "111111",
        type: "家用",
        startDate: this.date,
        endDate: this.date,
        website: "",
      };
      this.postRequest("/wechat-analysis/fees-day", params).then((res) => {
        let money = 0;
        let websitArr = res.data.map((v) => {
          money = money + v.amount;
          return v.websit;
        });
        this.allMoney = money;
        websitArr = [...new Set(websitArr)];
        let list = [];
        for (let i = 0; i < websitArr.length; i++) {
          list[i] = {
            webNumber: websitArr[i],
            name: "",
            amountIns: 0,
            wmrIns: 0,
            amountRepair: 0,
            wmrRepair: 0,
          };
          for (let j = 0; j < res.data.length; j++) {
            if (websitArr[i] === res.data[j].websit) {
              list[i].name = res.data[j].webName;
            }
            if (
              websitArr[i] === res.data[j].websit &&
              res.data[j].btype === "材料"
            ) {
              list[i].amountIns = res.data[j].amount;
              list[i].wmrIns = res.data[j].wmr;
            }
            if (
              websitArr[i] === res.data[j].websit &&
              res.data[j].btype === "维修"
            ) {
              list[i].amountRepair = res.data[j].amount;
              list[i].wmrRepair = res.data[j].wmr;
            }
          }
        }
        if (this.refreshing) {
          this.dataList = [];
          this.refreshing = false;
        }
        if (res.data.length === 0) {
          this.dataList = [];
          this.finished = true;
        }
        this.dataList = list;
        this.insFn();
        this.loading = false;
        if (this.dataList.length >= websitArr.length) {
          this.finished = true;
        }
      });
    },
    onConfirm(date) {
      this.show = false;
      this.date = this.formatDate(date);
      this.getList();
    },
    //日期格式化
    formatDate(date) {
      return `${date.getFullYear()}/${
        date.getMonth() + 1 >= 10
          ? date.getMonth() + 1
          : "0" + (date.getMonth() + 1)
      }/${date.getDate() >= 10 ? date.getDate() : "0" + date.getDate()}`;
    },
    goBack() {
      this.$router.back();
    },
  },
};
</script>

<style lang="scss" scoped>
.listTitle {
  font-weight: 700;
  font-size: 12px;
}
.tit {
  font-weight: 700;
  font-size: 14px;
}
.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>