<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()" :max="record.oldPrice" :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" @change="changeValue(record)"></a-input-number>
      </template>
      <template slot="money" slot-scope="text, record">
        <a-input-number v-model=record.money @blur="handleBlur()" :max="record.oldPrice" :min="0"></a-input-number>
      </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: 40,
    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',
    scopedSlots: { customRender: 'subjectType' },
    customRender: function (text, record) {
        return record.feeGoodVo.num
    }
  },
  {
    title: '冲账数量',
    dataIndex: 'num',
    width: 60,
    align: 'center',
    scopedSlots: { customRender: 'num'},
    // customRender: function (text, record) {
    //     return record.num = 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: 1000,
      visible: false,
      disableSubmit: false,
      showYinshou: true,
      columns: columns,
      columnstwo: columnstwo,
      feesList: [],
      tebdata: 'small',
      // 商品
      productList:[]
    }
  },
  methods: {
    // 冲账数量变化
    changeValue(val) {
      console.log(val);
      let tempvalue = val.oldPrice / val.feeGoodVo.num * val.num
      return val.money = tempvalue
    },
    edit(record, map) {
      this.visible = true
      console.log(record)
      this.productList = []
      this.feesList = []
      record.forEach(index => {
        if (index.feeGoodVo == null) {
          this.feesList.push(index)
        } else if (index.feeGoodVo !== null) [
          this.productList.push(index)
        ]
      })
      // this.feesList = record
      this.feesList.forEach(e => {
        e.roomName = map.get(e.roomId)
        e.oldPrice = e.money
        e.money = 0
      })
      this.productList.forEach(e => {
        e.roomName = map.get(e.roomId)
        e.oldPrice = e.money
        e.money = 0
        let tempnum = JSON.parse(JSON.stringify(e.feeGoodVo))
        this.$set(e,'num',tempnum.num)
      })
      console.log(this.feesList);
      console.log(this.productList);
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      if (this.tebdata == 'default') {
        let productfees = []
        if (this.productList.some(e => e.money > e.oldPrice || e.money < 0)) {
          this.$message.warning('冲账金额不能超过原金额,且不能小于0')
          return
        } else {
          this.productList.forEach(e => {
            // 一条消费可能有多次冲账，所以传入后端的money是冲账的负数差值
            if (e.oldPrice - e.money > 0) {
              let param = Object.assign({}, e)
              param.money = -param.money
              // param.money = param.money - param.oldPrice
              productfees.push(param)
            }
          })
        }
        if (productfees.length === 0) {
          return
        }
        postAction('/business/busOrderFee/strike-balance', productfees).then(resp => {
          if (resp.result === true) {
            this.$message.success('冲账成功')
            this.$emit('ok')
            this.visible = false
          } else {
            this.$message.warning('冲账失败')
          }
        });
      } else {
        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
        }
        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>
