<template>
    <div class="flex-row">
      <div class="span" style="overflow-y: hidden">
        <form novalidate class="form-horizontal">
          <p class="bg-info text-center" style="padding: 8px;">上次购气信息</p>
          <div class="row" style="margin-top:6px;">
            <label class=" col-sm-2 control-label">预购气量</label>
            <div class="col-sm-2" >
              <input class="form-control"  type="number" v-model="model.f_pregas" placeholder="预购气量" disabled>
            </div>
            <label class=" col-sm-2 control-label">预购金额</label>
            <div class="col-sm-2">
              <input class="form-control"  type="number" v-model="model.f_preamount" placeholder="预购金额" disabled>
            </div>
            <label class=" col-sm-2 control-label">撤销类型</label>
            <div class="col-sm-2">
              <input class="form-control"  type="text" v-model="model.f_canceltype" placeholder="撤销类型" disabled>
            </div>
          </div>
          <div class="row" style="margin-top:6px;">
            <label class=" col-sm-2 control-label">上期结余</label>
            <div class="col-sm-2">
              <input class="form-control"  type="number" v-model="model.f_balance" placeholder="上期结余" disabled>
            </div>
            <label class="col-sm-2 control-label">本期结余</label>
            <div class="col-sm-2">
              <input class="form-control"  type="number" v-model="model.f_curbalance" placeholder="本期结余" disabled>
            </div>
            <label class="col-sm-2 control-label">收款</label>
            <div class="col-sm-2">
              <input class="form-control"  type="number" v-model="model.f_collection" placeholder="收款" disabled>
            </div>
          </div>
          <div class="row" style="margin-top:6px;">
            <label class=" col-sm-2 control-label">售气时间</label>
            <div class="col-sm-2" >
              <input class="form-control"  type="text" v-model="model.f_date" disabled>
            </div>
            <label class="col-sm-2 control-label">操作员</label>
            <div class="col-sm-2">
              <input class="form-control" type="text" v-model="model.f_operator" disabled>
            </div>
            <!--<label for="f_outlets" class="col-sm-2 control-label" style="width:15%">网点:</label>-->
            <!--<div class="col-sm-2">-->
            <!--<input class="form-control"  type="text" v-model="model.f_outlets" disabled>-->
            <!--</div>-->
          </div>

          <div v-if="model.f_fillgas_id">
            <p class="bg-info text-center" style="padding: 8px;" >相关补气信息</p>
            <div class="row" style="margin-top:6px;">
              <label class=" col-sm-2 control-label">预购气量</label>
              <div class="col-sm-2" >
                <input class="form-control"  type="number" v-model="xmodel.f_pregas" placeholder="预购气量" disabled>
              </div>
              <label class=" col-sm-2 control-label">预购金额</label>
              <div class="col-sm-2">
                <input class="form-control"  type="number" v-model="xmodel.f_preamount" placeholder="预购金额" disabled>
              </div>
              <label class=" col-sm-2 control-label">撤销类型</label>
              <div class="col-sm-2">
                <input class="form-control"  type="text" v-model="xmodel.f_canceltype" placeholder="撤销类型" disabled>
              </div>
            </div>
            <div class="row" style="margin-top:6px;">
              <label class=" col-sm-2 control-label">上期结余</label>
              <div class="col-sm-2">
                <input class="form-control"  type="number" v-model="xmodel.f_balance" placeholder="上期结余" disabled>
              </div>
              <label class="col-sm-2 control-label">本期结余</label>
              <div class="col-sm-2">
                <input class="form-control"  type="number" v-model="xmodel.f_curbalance" placeholder="本期结余" disabled>
              </div>
              <label class="col-sm-2 control-label">收款</label>
              <div class="col-sm-2">
                <input class="form-control"  type="number" v-model="xmodel.f_collection" placeholder="收款" disabled>
              </div>
            </div>
            <div class="row" style="margin-top:6px;">
              <label class=" col-sm-2 control-label">售气时间</label>
              <div class="col-sm-2" >
                <input class="form-control"  type="text" v-model="xmodel.f_date" disabled>
              </div>
              <label class="col-sm-2 control-label">操作员</label>
              <div class="col-sm-2">
                <input class="form-control" type="text" v-model="xmodel.f_operator" disabled>
              </div>
              <!--<label for="f_outlets" class="col-sm-2 control-label" style="width:15%">网点:</label>-->
              <!--<div class="col-sm-2">-->
              <!--<input class="form-control"  type="text" v-model="model.f_outlets" disabled>-->
              <!--</div>-->
            </div>
          </div>
        </form>
        <div style="text-align:right;margin-right:50px;height: auto">
          <button class="btn btn-default width-60" @click="cancel()" :disabled="isToday" v-if="jurisdiction.includes('购气撤销')">购气撤销</button>
          <button class="button_clear" @click="clean()">取消</button>
        </div>

        <modal v-if="show" :show.sync="show" v-ref:modal backdrop="false">
          <header slot="modal-header" class="modal-header">
            <h4 class="modal-title">撤销原因</h4>
          </header>
          <article slot="modal-body" class="modal-body">
            <validator name='v'>
              <form class="form-horizontal select-overspread">
                <div class="form-group" v-if="config.hasBillManage && model.f_bill_style">
                  <label class=" col-sm-3 control-label">发票撤销类型:</label>
                  <div class="col-sm-4">
                    <v-select v-model="f_cancel_type"
                              placeholder='请选择'
                              :value.sync="correctModel.f_cancel_type"
                              :options='cancelTypes'
                              @change="cancelTypeChange"
                              :value-single="true"
                              close-on-select clear-button>
                    </v-select>
                  </div>
                </div>
                <div class="form-group" :class="[$v.f_cancel_reason.required ? 'has-error' : 'has-success']">
                  <label class=" col-sm-3 control-label">撤销原因:</label>
                  <div class="col-sm-8">
                    <textarea class="form-control" v-model="correctModel.f_cancel_reason" rows="3"
                              placeholder="请填写原因" v-validate:f_cancel_reason='{required: true}'></textarea>
                  </div>
                </div>
              </form>
            </validator>
            <validate-bill v-if="hasValidateBill" :data="model" @validate-bill="validateBill"></validate-bill>
          </article>
          <footer slot="modal-footer" class="modal-footer">
            <button v-show="show" type="button" class="button_search" @click='confirm()' :disabled="!$v.valid && validateOk">确认</button>
            <button v-show="show" type="button" class="button_clear" @click='close'>取消</button>
          </footer>
        </modal>
        <correct-bill :show="print" :bill-config='config' :bill-data='billData'
                      @printok="printok" @toggle="printCancel" :data='printModel' v-ref:printbill ></correct-bill>
      </div>
    </div>
</template>
<script>
/**
*综合业务
*卡表收费组件
*/

let cardChargeCancelGen = async function (self){
  // 冲正记录
  self.row.cardInfo = self.cardData
  self.model = Object.assign({}, self.model, self.correctModel)
  let res = null
  if (self.xmodel.id) {
    res = await self.$CancelService.cardAndFillGasCancel(self.model,self.xmodel, self.row)
  } else {
    res = await self.$CancelService.cardChargeCancel(self.model, self.row)
  }

  console.log('冲正返回。。', res, self.model, self.config, self.correctModel)

  if (self.model.f_bill_style.includes('普通收据') && self.config.hasBillManage) {
    // 冲正发票管理数据
    if (self.correctModel.f_cancel_type === 'newBill') {

      // 显示发票信息
      self.printModel.id = res.data.record.id
      self.printModel.f_type = res.data.type
      self.printModel.f_bill_style = res.data.record.f_bill_style
      self.billData.url = self.getBillUrl(res.data.type)
      console.log('普通收据打印。。。', self.printModel, self.billUrl)
      self.print = true
    } else if (self.correctModel.f_cancel_type === 'abort') {
      await self.$CommonService.correctPaperBill(res.data.record.id, res.data.type, null, null, 'abort')
      self.$dispatch('refresh')
    }
  } else if (self.model.f_bill_style.includes('电子发票')) {
    // 冲正电子票
    await self.$CommonService.correctEticket(self.model.id, self.model.f_canceltype)
    self.$dispatch('success')
  } else {
    self.$dispatch('success')
  }
}

let readyGen = async function (self) {
  await self.$getConfig(self,'RecordCancel')
  console.log('冲正业务config', self.config)
  self.correctModel.f_cancel_type = self.config.cancelType

  self.model.f_price_id = self.row.f_price_id
  self.model.f_user_id = self.row.f_user_id
  self.model.f_user_name = self.row.f_user_name
  self.model.f_userinfo_id = self.row.f_userinfo_id
  let res = await self.$resetpost('rs/sql/gasCancel', {data: {f_user_id: self.model.f_user_id}}, {resolveMsg: null, rejectMsg: '获取记录失败！！'})
  console.log('GASCANCEL:', res.data)
  self.model.id = res.data[0].id
  self.model.f_pregas = res.data[0].f_pregas
  self.model.f_preamount = res.data[0].f_preamount
  self.model.f_curbalance = res.data[0].f_curbalance
  self.model.f_collection = res.data[0].f_collection
  self.model.f_balance = res.data[0].f_balance
  self.model.f_user_balance = res.data[0].f_user_balance
  self.model.f_canceltype = res.data[0].f_canceltype
  self.model.f_bill_style = res.data[0].f_bill_style
  self.model.f_operator = res.data[0].f_operator
  self.model.f_date = res.data[0].f_date
  if(res.data[0].f_fillgas_id&&res.data[0].f_fillgas_id.length>0){

    self.model.f_fillgas_id = res.data[0].f_fillgas_id
    //开始给关联购气信息赋值
    for(var i=0;i<res.data.length;i++){
      console.log('换表关联信息。。', res.data[0].f_fillgas_id, res.data[i].id)
      if(res.data[0].f_fillgas_id==res.data[i].id){
        self.$showAlert('此卡存在关联购气信息，请谨慎核对！！！', 'warning', 2000)
        self.xmodel.id = res.data[i].id
        self.xmodel.f_pregas = res.data[i].f_pregas
        self.xmodel.f_preamount = res.data[i].f_preamount
        self.xmodel.f_curbalance = res.data[i].f_curbalance
        self.xmodel.f_collection = res.data[i].f_collection
        self.xmodel.f_balance = res.data[i].f_balance
        self.xmodel.f_user_balance = res.data[i].f_user_balance
        self.xmodel.f_canceltype = res.data[i].f_canceltype
        self.xmodel.f_bill_style = res.data[i].f_bill_style
        self.xmodel.f_operator = res.data[i].f_operator
        self.xmodel.f_date = res.data[i].f_date
        //跳出循环
        break
      }
      // this.radio[index].$set(i,true)
    }

  }
  if(self.config.todayLimit) {
    self.isToday = new Date().toDateString() !== new Date(res.data[0].f_date).toDateString()
  }

  if (self.config.hasBillManage && self.config.cancelType === 'newBill' && self.model.f_bill_style) {
    // 获取原来收费记录使用的发票类型
    let param = {
      id: res.data[0].id,
      type: res.data[0].f_canceltype
    }
    let oldUseType = await self.$resetpost('rs/logic/sale_getOldUseType', {data: param}, {resolveMsg: null, rejectMsg: '获取原始票据信息出错！！'})
    console.log('获取原始票据信息。。。', oldUseType)

    self.model.f_use_type = oldUseType.data.f_use_type
    self.model.f_bill_type = [res.data[0].f_bill_style]
    self.hasValidateBill = self.config.hasBillManage
  }




}

export default {
  title: '卡表冲正',
  data () {
    return {
      config: {
        hasBillManage : false,  //默认不启用发票管理
        cancelType: 'newBill',
        todayLimit: false // 是否限制当天不能冲正
      },
      model: {
        f_payment: '',
        f_pregas: '',
        f_preamount: '',
        f_totalcost: '',
        f_curbalance: '',
        f_collection: '',
        f_balance: '',
        f_user_balance: '',
        f_canceltype: '',
        f_operator: '',
        f_date: '',
        f_fillgas_id: null,
      },
      //关联购气信息
      xmodel: {
        f_payment: '',
        f_pregas: '',
        f_preamount: '',
        f_totalcost: '',
        f_curbalance: '',
        f_collection: '',
        f_balance: '',
        f_user_balance: '',
        f_canceltype: '',
        f_operator: '',
        f_date: ''
      },
      show: false,
      isToday: false,

      correctModel: {
        f_cancel_type: '',
        f_cancel_reason: ''
      },
      printModel: {},
      print: false,
      billUrl: '',
      billData: {
        url: '',
        bill: ''
      },

      // 启用发票管理需要对票号进行验证
      hasValidateBill: false,
      validateOk: false,

      jurisdiction: this.$login.r,

    }
  },
  props: ['row', 'cardData'],
  ready () {
    readyGen(this)
  },
  watch: {
    'row' (val) {
      this.model.f_price_id = val.f_price_id
      this.model.f_user_id = val.f_user_id
      this.model.f_userinfo_id = val.f_userinfo_id
    }
  },
  methods: {
    cancel () {
      this.show = true
    },
    close () {
      this.show = false
    },
    confirm () {
      // 关闭 modal框
      this.close()
      console.log('看看有没有撤销原因', this.model)
      cardChargeCancelGen(this)
    },
    clean () {
      this.$info('取消操作')
      this.$dispatch('cancelclean', this.row)
    },
    getBillUrl (type) {
      let name = ''
      // 根据type数据 选择调用的Logic
      if (type === '卡表收费') {
        name = 'rs/report/card_bill'
      } else if (type === '发卡售气') {
        name = 'rs/report/sendCard_bill'
      }
      return name
    },
    printok () {
      this.show = false
      this.print = false
      this.$dispatch('success')
    },
    printCancel () {
      this.$dispatch('refresh')
    },
    validateBill (val) {
      this.validateOk = !val.isOk
      this.billData.bill = val.bill
    },
    cancelTypeChange () {
      this.hasValidateBill = this.correctModel.f_cancel_type === 'newBill'
    }
  },
  computed: {
    cancelTypes () {
      return [{label: '使用新票号', value: 'newBill'}, {label: '旧发票作废', value: 'abort'}]
    }
  }
}
</script>

<style>
</style>
