<template>
  <div class="auto">
    <validator name='v'>
      <p class="bg-info text-center" style="padding: 8px;">{{data.type}}撤销</p>
      <div class="row" style="margin-top:10px;">
        <div class="form-group">
          <label for="f_cancel_reason" class=" col-sm-2 control-label">撤销原因:</label>
          <div class="col-sm-8">
            <textarea class="form-control" v-model="model.f_cancel_reason" rows="3" placeholder="请填写原因"></textarea>
          </div>
        </div>
      </div>
      <div style="text-align:right;height:auto;margin-top:30px;margin-right:50px;">
        <button class="btn btn-success" @click="confirm()" title="演示系统暂不支持" :disabled="true" >确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认</button>
        <button class="btn btn-default" @click="clean()" style="width:80px;margin-left:20px;">取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
      </div>
    </validator>
  </div>
</template>

<script>

  let postGen = async function (self, name) {
    let url = 'rs/logic/' + name
    // 获取冲正后的记录
    let getRecord = await self.$CancelService.cancel(url, self.model, self.data)
    console.log('冲正后的记录。。', getRecord)
    // 判断需不需要打印电子票
    if (self.isNeedEticket(getRecord.data, self.data.type)) {
      console.log('冲正打票，', getRecord.data.id, self.data.type)
      let getEticket = await
      self.$resetpost('rs/sql/singleTable', {
        data: {
          tablename: 't_eticket',
          condition: `f_sell_id = '${getRecord.data.id}' and f_charge_type = '${self.data.type}'`
        }
      }, {resolveMsg: null, rejectMsg: '获取电子票明细失败!!'})
      console.log('获取电子票信息', getEticket)
      if (getEticket.data.length > 0) {
        await self.$CommonService.correctEticket(getRecord.data.id, self.data.type)
      }
    }
    self.$dispatch('success')
  }

export default {
  props: ['data'],
  title: '操作撤销',
  data () {
    return {
      model: {
        f_cancel_reason: '',
        f_cancel_name: this.$login.f.name,
        f_filiale:this.$login.f.f_fengongsi,
        f_outlets:this.$login.f.f_parentname,
        f_orgstr:this.$login.f.orgpathstr,
        f_filialeids:this.$login.f.f_orgids,
        record_id: ''
      }
    }
  },
  methods: {
    confirm () {
      // 整理数据
      let name = this.sortdata()
      console.log('撤销请求业务逻辑名',  name)
      // 撤销操作请求
        console.log(name)
      this.post(name)
    },
    clean () {
      this.$dispatch('clean', this.model)
    },
    // 判断type类型  整理数据
    sortdata () {
      this.model.record_id = this.data.id
      let type = this.data.type
      let name
      // 根据type数据 选择调用的Logic
      if (type === '过户') {
        name = 'transferCancel'
      } else if (type === '换表') {
        name = 'changemeterCancel'
      } else if (type === '机表收费') {
        name = 'machineChargeCancelPay'
      } else if (type === '物联网收费') {
        name = 'iotMeterChargeCancel'
      } else if (type === '卡表超用') {
        name = 'cardOverCancel'
      } else if (type === '超用收费') {
        name = 'overuseCancel'
      } else if (type === '其他收费') {
        name = 'otherFeeRollback'
      } else if (type === '补卡') {
        name = 'replaceCardCancel'
      } else {
        return
      }
      return name
    },
    // 发送请求  及 请求后处理页面
    post (name) {
      postGen(this, name)
    },
    isNeedEticket (record, type) {
      // TODO 需要其他类型再继续加
      if (type === '补卡') {
        // 判断卡费是否大于零
        if (record.f_cardfees > 0) {
          return true
        }
      } else if (type === '其他收费') {
        if (record.f_collection > 0) {
          return true
        }
      } else if (type === '超用收费') {
        if (record.f_collection > 0) {
          return true
        }
      } else if (type === '物联网表收费') {
        if (record.f_collection > 0) {
          return true
        }
      }
      return false
    }
  }
}
</script>
