<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">
<!--    <refund ref="realForm" @ok="submitCallback" :disabled="disableSubmit"></refund>-->
    <a-table
        :columns="columns"
        :data-source="feeList"
        :pagination="false"
        :scroll="{ y: 160 }"
        rowKey="id"
        style="text-align: center"
    >
      <template slot="payType" slot-scope="text, record, index">
        {{ getPayTypeText(text) }}
      </template>
      <template slot="subjectType" slot-scope="text, record, index">
        {{ getSubjectTypeText(text,record) }}
      </template>
      <template slot="roomId" slot-scope="text, record, index">
        {{ getRoomName(text) }}
      </template>
      <template slot="refundType" slot-scope="text, record, index">
        <a-select style="width: 100px" v-model="record.refundType">
          <a-select-option v-for="(item, key) in record.payTypeList" :key="key" :value="item.id" :disabled="item.disable">{{ item.name }}</a-select-option>
        </a-select>
      </template>
      <template slot="prerefund" slot-scope="text, record, index">
        <a-input-number v-model="record.prerefund"></a-input-number>
      </template>
    </a-table>
    <a-form-model>
      <a-row  style="display: flex; ">
        <a-col :span="16" style="flex: 5">
          <a-form-model-item style="margin-bottom: 0px;" label="合计"></a-form-model-item>
        </a-col>
        <a-col :span="4" style="flex: 5">
        <a-form-model-item label="支付金额:" style="display: flex; margin-bottom: 0px;">
            {{ allFeeMoney }}
          </a-form-model-item>
        </a-col>
        <a-col :span="4" style="flex: 1.5">
        <a-form-model-item label="退款金额:"  style="display: flex; margin-bottom: 0px;">
            {{ allReturnFee }}
          </a-form-model-item>
        </a-col>
      </a-row>

    </a-form-model>
  </j-modal>
</template>

<script>
import { postAction } from '@api/manage'
import Refund from './Refund'

const columns = [
  {
    title: '房间号',
    dataIndex: 'roomName',
    width: 80,
    align: 'center',
    // scopedSlots: { customRender: 'roomId' }
  },
  {
    title: '支付类型',
    dataIndex: 'payType',
    align: 'center',
    width: 80,
    scopedSlots: { customRender: 'payType' }
  },
  {
    title: '费项',
    dataIndex: 'subjectType',
    align: 'center',
    width: 100,
    scopedSlots: { customRender: 'subjectType' }
  },
  {
    title: '入账日期',
    dataIndex: 'dayTime',
    align: 'center',
    width: 110,
    customRender: function (text, record) {
      return record.createTime.substring(0, 10)
    }
  },
  {
    title: '支付金额',
    dataIndex: 'id',
    align: 'center',
    width: 90,
    customRender: function (text, record) {
      return record.money
    }
  },
  {
    title: '退款方式',
    // dataIndex: 'id',
    align: 'center',
    width: 130,
    scopedSlots: { customRender: 'refundType' }
  },
  {
  title: '本次退款',
  dataIndex: 'prerefund',
    align: 'center',
    scopedSlots: { customRender: 'prerefund' }
  },
  {
    title: '已退款',
    align: 'center',
    dataIndex: 'returnMoney',
  }
]

export default {
  name: 'RefundModal',
  components: {
    Refund
  },
  data () {
    return {
      title:'退款',
      width:900,
      visible: false,
      disableSubmit: false,
      payTypeList: [],
      columns,
      model: [],
      feeList: [],
      allFeeMoney: 0,
      allReturnFee: 0
    }
  },
  methods: {
    add(record) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.realForm.add(record);
      })
    },
    edit(record) {
      this.visible = true
      this.model = record;
      this.getFee()
    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      let refundFeeList = []
      if (this.feeList.some(e => e.money - e.returnMoney < e.prerefund)) {
        this.$message.warning('可退款余额不足')
        return
      }
      this.feeList.forEach(e => {
        console.log(e.prerefund)
        
        if (e.prerefund === 0 || e.prerefund === null) { return }
        let refundFee = {
          id: e.id,
          livingOrderId: e.livingOrderId,
          roomId: e.roomId,
          money: e.prerefund,
          returnFeeId: e.id,
          returnItem: true,
          feeType: e.feeType,
          subjectType: 13,
          payType: e.refundType,
          hotelId: e.hotelId,
          agreementUnitId: e.agreementUnitId
        }
        refundFeeList.push(refundFee)
      })
      if (refundFeeList.length === 0) {
        this.$message.warning('未检测出需要退款的账单')
        return
      }
      postAction('/business/busOrderFee/refundBatch', refundFeeList)
          .then(resp => {
            if (resp.result === true) {
              this.$message.success('退款成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.warning('退款失败')
            }
          })
    },
    handleCancel() {
      this.close()
    },
    getSubjectTypeText(text, record) {
      var msg = ''
      if (text == 1) {
        msg = '押金'
      } else if (text == 2) {
        msg = '预收房费'
      } else if (text == 3) {
        msg = '每日房费'
      } else if (text == 4) {
        msg = '优惠金额'
      } else if (text == 5) {
        msg = '结账收款'
      } else if (text == 6) {
        msg = '商品-' + record.feeGoodVo.name
      } else if (text == 7) {
        msg = '点餐'
      } else if (text == 8) {
        msg = '夜审房费'
      } else if (text == 9) {
        msg = '会议室'
      } else if (text == 10) {
        msg = '手工房费'
      } else if (text == 11) {
        msg = '水电煤抄表'
      } else if (text == 12) {
        msg = '赔偿费'
      } else if (text == 13) {
        msg = '退单结账'
      }
      return msg
    },
    getRoomName(id) {
      var index = this.model.livingRoomIds.findLastIndex((t) => t.roomId == id)
      var find = this.model.livingRoomIds[index]
      return find ? find.roomName : ''
    },
    getPayTypeText(text) {
      var find = this.payTypeList.find((t) => t.id == text)
      return find ? find.name : ''
    },
    getFee() {
      let ids = []
      this.model.forEach(e => {
        ids.push(e.livingOrder.id)
      })
      postAction('/business/busOrderFee/refundList', ids).then((res) => {
        let feeList = []
        let returnFeeList = []

        var vipIndex = this.payTypeList.findIndex((t) => t.name.includes('会员'))
        var danweiIndex = this.payTypeList.findIndex((t) => t.name.includes('单位'))
        res.result.forEach(e => {
          // 如果是退款
          if (e.returnItem === true) {
            e.money = -e.money;
            returnFeeList.push(e)
          } else {
            // 如果是收款
            e.returnMoney = 0
            e.prerefund = 0
            console.log(this.model);
            var room = this.model.filter(room => room.roomId === e.roomId)
            e.roomName = room[0].roomName
            this.$set(e, 'refundType', e.payType)
            let payTypeList = JSON.parse(JSON.stringify(this.payTypeList));
            console.log(room[0].livingOrder.vipCustomerId)
            console.log(room[0].livingOrder.contractTeamId)
            console.log(vipIndex)
            console.log(danweiIndex)
            this.$set(payTypeList[vipIndex], 'disable', room[0].livingOrder.vipCustomerId == null || room[0].livingOrder.vipCustomerId === '')
            this.$set(payTypeList[danweiIndex], 'disable', room[0].livingOrder.contractTeamId == null)
            this.$set(e, 'payTypeList', payTypeList)
            feeList.push(e)
          }
        })
        this.feeList = feeList
        console.log(this.feeList)
        console.log(feeList)
        if (returnFeeList.length === 0) {
          return
        }
        this.allFeeMoney = 0
        this.allReturnFee = 0
        this.feeList.forEach(e => {
          // 当前收款对象下所有的退款记录
          let returnMoney = returnFeeList.filter(ele => e.id === ele.returnFeeId).reduce((accumulator, ele) => accumulator + ele.money, 0)
          e.returnMoney = returnMoney;
          this.allFeeMoney += e.money;
          this.allReturnFee += returnMoney;
        })
        console.log(this.feeList);
      })
    }
  }
}
</script>
