<template>
  <div class="mainPage flex-v">
    <div class="top">
      <topCaseInfo :data="topCaseInfoData" :userName="userInfo.customer.RealName ? userInfo.customer.RealName : userInfo.customer.CustomerName" @selectCase="showVanPicker"></topCaseInfo>
    </div>
    <div class="flex-item body">
      <div>
        <ul>
          <li v-for="(item,index) in list" :key="index">
            <salesRecordItem :data="item" :fibdata="fibdata(item)" @triggerCardAndCoupon="triggerCardAndCoupon" @triggerDrink="triggerDrink" @toDetail='toDetail'></salesRecordItem>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import topCaseInfo from '../../../components/topCaseInfo/index'
import salesRecordItem from '../../../components/salesRecordItem/index'
import { mapState, createNamespacedHelpers } from 'vuex'
const { mapState: mapForbidState, mapActions: mapForbidActions } = createNamespacedHelpers('forbid')
const { mapActions: actions } = createNamespacedHelpers('app')
export default {
  name: '',
  data () {
    return {
      topCaseInfoData: {
        caseName: '',
        caseId: '',
        showSelect: false
      },
      list: []
    }
  },
  components: {
    topCaseInfo,
    salesRecordItem
  },
  computed: {
    ...mapState({
      userInfo: x => x.userCenter.userInfo,
      CaseList: x => x.app.CaseList,
    }),
    ...mapForbidState({
      salesList: x => x.salesList,
      forbidList: x => x.forbidList,
    }),
  },
  created () {
    this.getCaseList().then((res) => {
      this.topCaseInfoData.caseId = this.userInfo.customer.BelongCaseId
      for (let i=0;i<this.CaseList.length;i++){
        if (this.CaseList[i].CaseId === this.topCaseInfoData.caseId){
          this.topCaseInfoData.caseName = this.CaseList[i].CaseName
        }
      }
    })
    this.init()
  },
  methods: {
    ...actions([
      'getCaseList',
    ]),
    fibdata (item) {
      const couponfib = this.forbidList.filter(x => x.UserId === item.UserId && x.ForbidType === 'coupon').length > 0 ? true : false
      const orderfib = this.forbidList.filter(x => x.UserId === item.UserId && x.ForbidType === 'goods').length > 0 ? true : false
      return {
        'coupon': couponfib,
        'order': orderfib
      }
    },
    showVanPicker () {
      this.showSelect = true
    },
    selectCase (val) { // 选择案场
      this.topCaseInfoData.caseName = val.CaseName
      this.topCaseInfoData.caseId = val.CaseId
      this.getCaseTableList({
        caseid: this.topCaseInfoData.caseId
      })
      this.showSelect = false
    },
    ...mapForbidActions(['getSales', 'getForbidList', 'putForbid', 'putForbidOpen']),
    triggerCardAndCoupon (val) { // 切换卡券状态 type='close'：关闭；type='open'：开启；
      console.log(val)
      if (val.type === 'open') {
        this.putForbid({ userid: val.data.UserId, type: 'coupon' })
      } else {
        this.putForbidOpen({ userid: val.data.UserId, type: 'coupon' })
      }
      this.init()
    },
    triggerDrink (val) { // 切换饮品状态 type='close'：关闭；type='open'：开启；
      console.log(val)
      if (val.type === 'open') {
        this.putForbid({ userid: val.data.UserId, type: 'goods' })
      } else {
        this.putForbidOpen({ userid: val.data.UserId, type: 'goods' })
      }
      this.init()
    },
    toDetail (item) {
      console.log(item)
      this.$router.push({ name: 'salesGiveOutDetail', query: { id: item.UserId } })
    },
    init () {
      this.getSales({ type: 'sales' }).then(() => {
        this.getForbidList().then(() => {
          this.list = []
          for (let i = 0; i < this.salesList.length; i++) {
            let CustomerCard = this.salesList[i].CustomerCard || []
            let CustomerCoupon = this.salesList[i].CustomerCoupon || []
            let OrdersDetail = this.salesList[i].OrdersDetail || []
            let item = {
              name: this.salesList[i].RealName,
              id: i,
              cardAndCoupon: {
                totalNum: '100',
                usedNum: CustomerCard.length + CustomerCoupon.length
              },
              drink: {
                totalNum: '100',
                usedNum: OrdersDetail.length
              },
              UserId: this.salesList[i].UserId
            }
            this.list.push(item)
          }
        })
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
@import 'page.scss';
</style>
