<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-radio-group v-model="tebdata" style="margin-bottom: 16px">
      <a-radio-button value="small">金额冲账</a-radio-button>
      <a-radio-button value="default">商品冲账</a-radio-button>
    </a-radio-group>
    <a-table
    v-if="tebdata == 'small'"
      :columns="columns"
      :data-source="feesList"
      :pagination="false"
      :scroll="{ y: 160 }"
      rowKey="id">
      <template slot="subjectType" slot-scope="text, record">
        {{ getSubjectTypeText(text, record) }}
      </template>
      <template slot="money" slot-scope="text, record">
        <a-input-number v-model=record.money @blur="handleBlur()" :min="0"></a-input-number>
      </template>
      <template slot="remark" slot-scope="text, record">
        <a-textarea v-model=record.remark></a-textarea>
      </template>
    </a-table>
    <a-table
    v-if="tebdata == 'default'"
      :columns="columnstwo"
      :data-source="productList"
      :pagination="false"
      :scroll="{ y: 160 }"
      rowKey="id">
      <template slot="subjectType" slot-scope="text, record">
        {{ getSubjectTypeText(text, record) }}
      </template>
      <template slot="money" slot-scope="text, record">
        <!-- <a-input-number v-model=record.money @blur="handleBlur()" :min="0"></a-input-number> -->
        {{ record.money }}
      </template>
      <template slot="num" slot-scope="text, record">
        <a-input-number v-model='record.num' :min="1" :max="record.feeGoodVo.num"></a-input-number>
        <!-- {{ record.money }} -->
      </template>
      
      <template slot="remark" slot-scope="text, record">
        <a-textarea v-model=record.remark></a-textarea>
      </template>
    </a-table>
  </j-modal>
</template>

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

const columns = [
  {
    title: '房间',
    dataIndex: 'roomName',
    width: 30,
    align: 'center'
  },
  {
    title: '费项',
    dataIndex: 'subjectType',
    width: 60,
    align: 'center',
    scopedSlots: { customRender: 'subjectType' }
  },
  {
    title: '日期',
    dataIndex: 'dayTime',
    width: 100,
    align: 'center'
  },
  {
    title: '金额',
    dataIndex: 'oldPrice',
    width: 60,
    align: 'center'
  },
  {
    title: '冲账金额',
    dataIndex: 'money',
    width: 60,
    align: 'center',
    scopedSlots: { customRender: 'money'}
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 60,
    align: 'center',
    scopedSlots: { customRender: 'remark'}
  },
  ]
  const columnstwo = [
  {
    title: '房间',
    dataIndex: 'roomName',
    width: 30,
    align: 'center'
  },
  {
    title: '费项',
    dataIndex: 'subjectType',
    width: 60,
    align: 'center',
    scopedSlots: { customRender: 'subjectType' }
  },
  {
    title: '日期',
    dataIndex: 'dayTime',
    width: 100,
    align: 'center'
  },
  {
    title: '金额',
    dataIndex: 'oldPrice',
    width: 60,
    align: 'center'
  },
  {
    title: '数量',
    // dataIndex: 'oldPrice',
    width: 60,
    align: 'center',
    customRender: function (text, record) {
        return record.feeGoodVo.num
    }
  },
  {
    title: '冲账数量',
    dataIndex: 'num',
    width: 60,
    align: 'center',
    scopedSlots: { customRender: 'num'},
    customRender: function (text, record) {
        return record.feeGoodVo.num
    }
  },
  {
    title: '冲账金额',
    dataIndex: 'money',
    width: 60,
    align: 'center',
    scopedSlots: { customRender: 'money'}
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 60,
    align: 'center',
    scopedSlots: { customRender: 'remark'}
  },
  ]
export default {
  name: 'StrikeBalanceModel',
  components: {
  },
  data() {
    return {
      title: '',
      width: 800,
      visible: false,
      disableSubmit: false,
      showYinshou: true,
      columns: columns,
      columnstwo: columnstwo,
      feesList: [],
      tebdata: 'small',
      // 商品
      productList:[]
    }
  },
  methods: {

    edit(record, map) {
      this.visible = true
      console.log(record)
      record.forEach(index => {
        if (index.feeGoodVo == null) {
          this.feesList.push(index)
        } else if (index.feeGoodVo !== null) [
          this.productList.push(index)
        ]
      })
      console.log(this.feesList);
      console.log(this.productList);
      // this.feesList = record
      this.feesList.forEach(e => {
        e.roomName = map.get(e.roomId)
        e.oldPrice = e.money
      })
      this.productList.forEach(e => {
        e.roomName = map.get(e.roomId)
        e.oldPrice = e.money
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      // console.log(111);
      let fees = []
      if (this.feesList.some(e => e.money > e.oldPrice || e.money < 0)) {
        this.$message.warning('冲账金额不能超过原金额,且不能小于0')
        return
      } else {
        this.feesList.forEach(e => {
          // 一条消费可能有多次冲账，所以传入后端的money是冲账的负数差值
          if (e.oldPrice - e.money > 0) {
            let param = Object.assign({}, e)
            param.money = -param.money
            // param.money = param.money - param.oldPrice
            fees.push(param)
          }
        })
      }
      if (fees.length === 0) {
        return
      }
      console.log(fees);
      postAction('/business/busOrderFee/strike-balance', fees).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
    },
    handleBlur() {
      this.$forceUpdate()
    }

  }
}
</script>
