<template>
  <div>
    <van-tabs
      color="#EB4E3F"
      title-active-color="#EB4E3F"
      title-inactive-color="#666666"
    >
      <van-tab title="缴存">
        <div class="content">
          <div class="year-table" v-for="(data, index) in payData" :key="index">
            <div class="year">{{ data.year }}</div>
            <div class="list" v-for="(item, index) in data.arr" :key="index">
              <div class="cell" @click="item.show = !item.show">
                <div class="title-left">
                  <img src="@/assets/insurance/pay.png" class="icon" />
                  <div style="margin-left: 15px">
                    <p class="text">汇缴</p>
                    <p class="date">{{ item.date }}</p>
                  </div>
                </div>
                <div class="title-right">
                  <div class="moneny">{{ item.totalMoneny }}</div>
                  <van-icon
                    class="icon"
                    :name="item.show ? 'arrow-up' : 'arrow-down'"
                  />
                </div>
              </div>
              <div v-show="item.show" class="extra">
                <div class="extra-item">
                  <p class="text">缴费基数</p>
                  <p class="moneny">{{ item.aae180 }}</p>
                </div>
                <div class="extra-item">
                  <p class="text">个人缴费</p>
                  <p class="moneny">{{ item.aae022 }}</p>
                </div>
                <div class="extra-item">
                  <p class="text">单位缴费</p>
                  <p class="moneny">{{ item.aae020 }}</p>
                </div>
              </div>
            </div>
          </div>
          <div v-show="payNoData" class="nodata">
            <img src="@/assets/common/nodata.png" />
            <p class="nodataText">您当前还没有缴存记录</p>
          </div>
        </div>
      </van-tab>
      <van-tab title="待遇发放">
        <div class="content">
          <div class="nodata">
            <img src="@/assets/common/nodata.png" />
            <p class="nodataText">您当前还没有发放记录</p>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import Vue from "vue";
import { Tab, Tabs, Icon } from "vant";
import { social,doServerMedical } from "@/api/insurance";

Vue.use(Tab)
  .use(Tabs)
  .use(Icon);

export default {
  data() {
    return {
      aae140: null,
      idCard: null,
      payData: [],
      payNoData: false,
      userInfo: {}
    };
  },
  methods: {
    JSBgetUserInfo() {
      // alert(111)
      if (window.android !== undefined) {
        const res = window.android.getUserInfo();
        sessionStorage.setItem("ZJCD-UserInfo", res);
        this.userInfo = JSON.parse(res);
        this.idCard = JSON.parse(res).idCardNo;
        sessionStorage.setItem('token', this.userInfo.token),
        this.init();
      } else if (window.webkit) {
        window.webkit &&
          window.webkit.messageHandlers.getUserInfo.postMessage("getUserInfo");
      } else {
        this.$toast("请先登录！");
      }
    },
    init() {
      // alert(this.userInfo.idCardNo)
      let params = {
        aae140: this.aae140,
        busCode: "WS-B0003",
        encryptIdCard: this.userInfo.idCardNo,
        cpage: 1,
        rows: 10000,
      };
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: "加载中...",
      });
      // alert(JSON.stringify(params))
      if (this.aae140 != "310") {
        social(params).then((res) => {
          // alert(JSON.stringify(res))
          // alert(res.data.tradeMsg.head.rst.busiCode)
          this.$toast.clear();
          if (res.data.tradeMsg.data.busiId !== "0000") {
            this.$toast("未查询到相关社保信息");
            this.payNoData = true;
            return;
          }
          const initData = res.data.tradeMsg.data;
          if (!initData) {
            this.payNoData = true;
            return;
          }
          // alert(JSON.stringify(initData))
          let listData;
          if (initData.dataSet.rowSet.row.__proto__.constructor !== Array) {
            let a = [];
            a.push(initData.dataSet.rowSet.row);
            listData = a;
          } else {
            listData = initData.dataSet.rowSet.row;
          }
          listData.forEach((item) => {
            item.year = item.aae003.slice(0, 5);
            item.show = false;
            item.date = item.aae003.slice(5);
            item.totalMoneny = (
              parseFloat(item.aae020) + parseFloat(item.aae022)
            ).toFixed(2);
          });
          let newData = [];
          listData.forEach((list) => {
            let flag = false;
            newData.forEach((item) => {
              if (item.year === list.year) flag = true;
              else flag = false;
            });
            if (flag) {
              newData.forEach((item) => {
                if (item.year === list.year) {
                  item.arr.push(list);
                }
              });
            } else {
              let newArr = [];
              newArr.push(list);
              const newObj = {
                year: list.year,
                arr: newArr,
              };
              newData.push(newObj);
            }
          });
          this.payData = newData;
        });  
      } else {
        doServerMedical(params).then((res) => {
          this.$toast.clear();
          if (res.data.tradeMsg.data.busiId !== "0000") {
            this.$toast("未查询到相关社保信息");
            this.payNoData = true;
            return;
          }
          const initData = res.data.tradeMsg.data;
          if (!initData) {
            this.payNoData = true;
            return;
          }
          // alert(JSON.stringify(initData))
          let listData;
          if (initData.dataSet.rowSet.row.__proto__.constructor !== Array) {
            let a = [];
            a.push(initData.dataSet.rowSet.row);
            listData = a;
          } else {
            listData = initData.dataSet.rowSet.row;
          }
          listData.forEach((item) => {
            item.year = item.aae003.slice(0, 5);
            item.show = false;
            item.date = item.aae003.slice(5);
            item.totalMoneny = (
              parseFloat(item.aae020) + parseFloat(item.aae022)
            ).toFixed(2);
          });
          let newData = [];
          listData.forEach((list) => {
            let flag = false;
            newData.forEach((item) => {
              if (item.year === list.year) flag = true;
              else flag = false;
            });
            if (flag) {
              newData.forEach((item) => {
                if (item.year === list.year) {
                  item.arr.push(list);
                }
              });
            } else {
              let newArr = [];
              newArr.push(list);
              const newObj = {
                year: list.year,
                arr: newArr,
              };
              newData.push(newObj);
            }
          });
          this.payData = newData;
        })
      }
      
    },
    getUserInfo(res) {
      sessionStorage.setItem("ZJCD-UserInfo", res);
      this.userInfo = JSON.parse(res);
      this.idCard = JSON.parse(res).idCardNo;
      sessionStorage.setItem('token', this.userInfo.token),
      this.init();
    },
  },
  mounted() {
    window.getUserInfo = this.getUserInfo;
  },
  created() {
    this.aae140 = this.$route.query.type;
    if (this.aae140 === "110") {
      document.title = "养老保险";
    } else if (this.aae140 === "310") {
      document.title = "医疗保险";
    } else if (this.aae140 === "410") {
      document.title = "工伤保险";
    } else if (this.aae140 === "210") {
      document.title = "失业保险";
    } else if (this.aae140 === "510") {
      document.title = "生育保险";
    }
      this.JSBgetUserInfo();
  },
};
</script>

<style lang="stylus" scoped>
.content {
  height calc(100vh - 88px)
  overflow-y auto
  background #fff

  .nodata {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-top: 150px;

    img {
      width 300px;
      height 300px;
    }

    .nodataText {
      font-size: 24px;
      color: #666666;
      margin-top: 80px;
    }
  }

  .year-table {
    .year {
      height: 80px;
      padding: 0 30px;
      background: #F5F5F5;
      font-size: 28px;
      color: #666666;
      line-height: 80px;
    }
    .list {
      padding: 0 30px;
      background: #FFFFFF;

      .cell {
        height: 110px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .title-left {
          display: flex;
          align-items: center;

          img {
            width 60px;
            height 60px;
          }
          .text {
            font-size: 32px;
            color: #333333;
          }
          .date {
            margin-top: 16px
            font-size: 22px;
            color: #999999;
          }
        }

        .title-right {
          display flex;
          align-items center

          .moneny {
            font-size: 32px;
            font-weight: 600;
            color: #EB4E3F;
          }

          .icon {
            font-size: 25px;
            margin-left: 30px;
          }
        }
      }
    }
    .extra {
      padding 30px 55px 30px 90px

      .extra-item {
        margin-top: 24px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .text {
          font-size: 28px;
          font-weight: 500;
          color: #333333;
        }
        .moneny {
          font-size: 28px;
          font-weight: 600;
          color: #EB4E3F;
        }
      }
      .extra-item:first-child {
        margin-top 0
      }
    }
  }
}
</style>
