<template>
<div class="summary w100 h100">
  <search-result :result-num="sum"></search-result>
  <div class="list">
    <van-collapse v-model="activeNames">
      <van-collapse-item :name="item.orgName" v-for="(item, index) in lists" :key="index" class="common">
        <template #title>
          <h4 class="remove-margin">{{item.orgName}}</h4>
          <p class="" v-if="!referType">小计: {{item.taxcount | separator}}元</p>
          <p class="" v-if="referType">小计: {{item.taxfreecount | separator}}元</p>
        </template>
        <van-cell :title="val.empName" v-if="!referType" v-for="(val, idx) in item.children" :key="idx" is-link :value="val.taxAmountTotal| separator('含税总金额：',' 元')" @click="onClickUser(val)" />
        <van-cell :title="val.empName" v-if="referType" v-for="(val, idx) in item.children" :key="idx" is-link :value="val.taxFreeTotal| separator('无税总金额：',' 元')" @click="onClickUser(val)" />
      </van-collapse-item>
    </van-collapse>
    <p class="text-center font15 saleTitle">销售开单时间: {{time.start}} 至 {{time.end}}</p>
  </div>
  <div class="footer">
    <van-button square type="primary" v-if="!referType" @click="referType = !referType">含税</van-button>
    <p class="text-center remove-margin" v-if="!referType">合计: <span>{{totalMoney.taxcount | separator}}元</span></p>
    <van-button square type="primary" v-if="referType" @click="referType = !referType">无税</van-button>
    <p class="text-center remove-margin" v-if="referType">合计: <span>{{totalMoney.taxfreecount | separator}}元</span></p>
  </div>
</div>
</template>

<script>
// 引入接口
import {
  getSummary
} from "@/api/saleSituation";

// 引入组件
import SearchResult from "@/components/SearchResult";

export default {
  name: "summaryComponents",
  components: {
    SearchResult
  },
  props: {
    chooseTime: {
      type: Object,
      default: () => {}
    },
    salerInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      activeNames: ["1"],
      lists: [],
      totalMoney: {},
      time: {},
      sum: "0",
      referType: false
    };
  },
  mounted() {
    this.time = JSON.parse(JSON.stringify(this.chooseTime));
    this.getSummary();
  },
  watch: {
    chooseTime: {
      handler(val) {
        this.time = JSON.parse(JSON.stringify(this.chooseTime));
        this.getSummary();
      },
      deep: true
    }
  },
  destroyed() {},
  methods: {
    onClickUser(val) {
      this.$emit("click", val);
    },
    getSummary() {
      getSummary({
        dbName: this.salerInfo.sysDbSource, // 'erp_ogg_shaphar',
        attr: {
          saler: this.salerInfo.sysUserId,
          orgId: this.salerInfo.sysOrgId,
          startDate: this.time.start,
          endDate: this.time.end,
          start: "",
          inAttr1: this.salerInfo.sysDbSource, // 'erp_ogg_shaphar',
          inAttr2: this.salerInfo.appRealDb, // 'app_real_shaphar',
          inAttr3: "",
          inAttr4: "",
          inAttr5: "",
          inAttr6: "",
          inAttr7: "",
          inAttr8: "",
          inAttr9: "",
          inAttr10: ""
        }
      }).then(res => {
        if (res.code !== "1") return false;
        // 后面加的，防止数据重复添加的问题
        this.lists = [];
        this.sum = res.data.length + "";
        if (res.data !== "" && res.data.length > 0) {
          res.data.forEach(item => {
            let count = 0;
            let index = 0;
            this.lists.forEach((val, idx) => {
              if (item.orgName === val.orgName) {
                count++;
                index = idx;
                return false;
              }
            });
            if (!count) {
              this.lists.push({
                orgName: item.orgName,
                children: [item]
              });
            } else {
              this.lists[index].children.push(item);
            }
          });
        }
        this.activeNames = this.lists.map(item => {
          return item.orgName;
        });
        this.lists = this.lists.map(item => {
          let taxcount = 0;
          let taxfreecount = 0;
          item.children.forEach(val => {
            taxcount += parseFloat(val.taxAmountTotal);
            taxfreecount += parseFloat(val.taxFreeTotal);
          });
          return Object.assign({}, item, {
            taxcount: taxcount.toFixed(2),
            taxfreecount: taxfreecount.toFixed(2)
          });
        });
        let obj = {
          taxcount: 0,
          taxfreecount: 0
        };
        this.lists.forEach(item => {
          obj.taxcount = parseFloat(item.taxcount) + parseFloat(obj.taxcount);
          obj.taxfreecount =
            parseFloat(item.taxfreecount) + parseFloat(obj.taxfreecount);
        });
        this.totalMoney = {};
        this.totalMoney = Object.assign({}, this.totalMoney, obj);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.summary {
  display: flex;
  flex-direction: column;

  .list {
    width: 100%;
    flex: 1;
    overflow: auto;

    .common {
      p {
        font-size: 15px;
        color: #26a7e1;
        margin: 5px 0 0;
      }

      /deep/ .van-collapse-item__content {
        padding: 0 0 0 16px;

        .van-cell__title {
          width: 80px;
          flex: none;
          color: #677282;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

        .van-cell__value {
          color: #677282;
          text-align: left;
        }
      }
    }
  }

  .saleTitle {
    color: #23262a;
    font-weight: 600;
    font-size: 15px;
  }

  .footer {
    background-color: $bc1;
    padding-left: 60px;
    height: 44px;
    line-height: 44px;
    position: relative;

    .van-button {
      width: 60px;
      position: absolute;
      left: 0;
      top: 0;
      z-index: 1;
    }

    p {
      font-size: 16px;
    }

    p span {
      font-size: 16px;
      font-weight: bold;
      color: $tc5;
    }
  }
}
</style>
