<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      class="dialog-info"
      inline
      label-width="120px">
      <div class="block-title">
        会员信息
      </div>
      <el-form-item label="会员类型">
        <span>
          {{ currentItem.memberType.name }}
        </span>
      </el-form-item>
      <el-form-item label="姓名">
        <span>
          {{ currentItem.name }}
        </span>
      </el-form-item>
      <el-form-item label="联系方式">
        <span>
          {{ currentItem.phone }}
        </span>
      </el-form-item>
      <el-form-item label="证件类型">
        <span>
          {{ currentItem.papers_type | paperType }}
        </span>
      </el-form-item>
      <el-form-item label="证件号码">
        <span>
          {{ currentItem.papers_id }}
        </span>
      </el-form-item>
      <el-form-item label="生效时间">
        <span>
          {{ currentItem.starttime | formatTime }}
        </span>
      </el-form-item>
      <el-form-item label="截止时间">
        <span>
          {{ currentItem.endtime | formatTime }}
        </span>
      </el-form-item>
      <el-form-item label="创建时间">
        <span>
          {{ currentItem.createTime | formatTime }}
        </span>
      </el-form-item>
      <div class="block-title">
        结算信息
      </div>
      <el-form-item
        verify
        prop="total_money"
        label="退款金额"
        class="is-required">
        <el-input-number
          :min="0"
          :precision="0"
          controls-position="right"
          v-model="form.total_money">
          {{ form.total_money }}
        </el-input-number>
      </el-form-item>
      <el-form-item
        verify
        prop="payChannel"
        label="付款方式"
        class="is-required">
        <el-select v-model="form.payChannel">
          <el-option
            v-for="item in payChannelOptions"
            :key="item"
            :value="item"
            :label="item"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="dialog-btn-wrap">
      <el-button
        @click="submit"
        type="primary">
        确定
      </el-button>
    </div>

    <SalesList
      ref="salesList"
      @change="handleGetSaleId"></SalesList>
  </div>
</template>

<script>
import { apiMember } from '@/api/member'
import { shutMemberMoney } from '@/api/valueCard'
import SalesList from '@/components/SalesList'
import { debounce } from 'throttle-debounce'
export default {
  components: {
    SalesList
  },
  props: {
    currentItem: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    payChannelOptions () {
      return this.$store.state.app.payChannelList.filter(i => i.name.indexOf('旅行社') < 0).filter(i => i.type === 'NOPAY').map(i => i.name)
    }
  },
  data () {
    return {
      visible: false,
      cancel_manager: '',
      form: {
        member_id: '',
        total_money: 0,
        payChannel: '现金'
      }
    }
  },
  watch: {
    visible (val) {
      if (val) {
        this.reset()
        if (this.currentItem) {
          this.form.member_id = this.currentItem.id
        }
      }
    }
  },
  methods: {
    handleClose () {
      this.cancel_manager = ''
      this.$parent.handleClose()
    },
    handleGetSaleId (m) {
      this.cancel_manager = m

      this.submit()
    },
    submit () {
      if (!this.cancel_manager) {
        this.$refs.salesList.show('')
        return
      }

      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.creator = this.cancel_manager.id

          if (this.form.payChannel.indexOf('储值') > 0) {
            /*
            shutMemberMoney({
              cardNo: '',
              cardVendor: 'CHANGYI',
              memberTrade: this.form
            }).then(res => {
              this.$message.success('结算成功')
              this.$emit('updateList')
              this.handleClose()
            })
            */
            this.shutMemberMoneyCHANGYI()
          } else {
            const params = JSON.parse(JSON.stringify(this.form))
            if (this.tradeLog && this.tradeLog.id) {
              if (!this.payChannelOptions.includes(params.payChannel)) { // 不是nopay类型 补充trades结构 nopay类型不能传trades结构
                params.trades = {
                  [this.tradeLog.id]: {
                    cancel_pay_channel: this.form.payChannel,
                    total_cancelPrice: this.form.total_money
                  }
                }
              }
            }
            /*
            apiMember('shutMemberMoney', params).then(res => {
              this.$message.success('结算成功')
              this.$emit('updateList')
              this.handleClose()
            })
            */
            this.shutMemberMoneyDirect(params)
          }
        } else {
          return false
        }
      })
    },
    shutMemberMoneyDirect: debounce(1000, true, function (params) {
      apiMember('shutMemberMoney', params).then(res => {
        this.$message.success('结算成功')
        this.$emit('updateList')
        this.handleClose()
      })
    }),
    shutMemberMoneyCHANGYI: debounce(1000, true, function () {
      shutMemberMoney({
        cardNo: '',
        cardVendor: 'CHANGYI',
        memberTrade: this.form
      }).then(res => {
        this.$message.success('结算成功')
        this.$emit('updateList')
        this.handleClose()
      })
    }),
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>
