<template>
  <div id="unit" class="flex-row three" v-show="listpage">
    <div class="binary-left" style="overflow: scroll;width:15%" >
      <mulitinfo style="height:auto" @deal-msg="dealMsg" :data="row" :card-data="cardInfo" v-ref:info></mulitinfo>
    </div>
    <div class="binary-right" style="width:68%;overflow-y: scroll" >
      <div :style="{height:(row?'auto':'100%')}">
        <charge-list :row="row" @select-oper="showWork" @dblclick="toBusiness" @deal-msg="dealMsg"
                     v-ref:list></charge-list>

        <div class="auto" style="margin-top: 1%;" v-if="row">
          <charge-oper :data.sync="row" :card-info="cardInfo"></charge-oper>
        </div>
      </div>
    </div>
    <div class="binary-right" style="width:22%" >
      <tabset v-ref:tabs :active="0" :close="false">
        <tab header="其他信息" id="clickMe" v-ref:tab>
          <otherinfo style="height:auto" @deal-msg="dealMsg" :data="row" :card-data="cardInfo" v-ref:info></otherinfo>
        </tab>
        <tab header="历史操作">
          <card-list :btns="operatebtn" :f_orgid="f_orgid" :userinfoid="row ? row.f_userinfo_code : ''" @list-select-oper="recordCancel"
                     v-ref:card></card-list>
        </tab>
        <tab header="历史抄表" v-if="row ?row.f_meter_type=='机表' : false">
          <hand-list  :f_userfiles_id="row ? row.f_userfiles_id : ''" v-ref:hand></hand-list>
        </tab>
      </tabset>

    </div>

  </div>
  <div v-if="!listpage">
    <user-info-detail-manage-new :f_userinfo_id="rowData.f_userinfo_id" @cancel-main="cancel"></user-info-detail-manage-new>
  </div>
  <div v-if="showModal">
    <charge-modal :data2="modaldata2" :data="modaldata" :row="modalrow" :modal-show.sync="showModal" @close="close" @modalsuccess="modalsuccess"></charge-modal>
  </div>
</template>
<script>
import ChargeList from './ChargeList'
import ChargeOper from '../../components/charge/ChargeOper'
import {HttpResetClass} from 'vue-client'

export default {
  title: '收费(综合)',
  components: {
    ChargeList, ChargeOper
  },
  data () {
    return {
      // 页面开关
      listpage: true,
      f_orgid: '',
      row: null,
      cardInfo: null,
      modaldata: null,
      modaldata2: null,
      modalrow: null,
      showModal: false,
      rowData: {},
      dibao_remind: this.$appdata.getSingleValue('低保快到期提醒') ? this.$appdata.getSingleValue('低保快到期提醒') : 0,
      show: [true]
    }
  },
  methods: {
    async toBusiness (obj) {
      console.log('双击列表的时候传进来的数据：', obj)
      if (!(obj instanceof Event)) {
        // 对此数据进行验证
        if (await this.validateRow(obj)) {
          // 获取未写卡或者写卡失败记录
          let getUnWriteSell = await this.$SqlService.singleTable('t_sellinggas', `f_state = '有效' and f_write_card  in ('未写卡','写卡失败') and f_userfiles_id = '${obj.f_userfiles_id}'`)
          console.log('获取未写卡记录', getUnWriteSell)
          if (getUnWriteSell.data.length > 1) {
            this.$showAlert('此卡已缴费多次未写卡。请核实后处理！！', 'warning', 5000)
          } else {
            this.row = obj
            this.row.unWriteSell = getUnWriteSell.data
            this.cardInfo = this.$refs.list.cardInfo
            this.$refs.list.criteriaShow = false
            // 将列表只显示该条数据
            this.$refs.list.refreshRow(obj)
          }
        }
      }
    },
    close () {
      this.showModal = false
      this.modaldata = null
      this.modalrow = null
      this.clean()
      this.$refs.list.search()
      // this.$refs.card.search()
    },
    async validateRow (obj) {
      console.log('查看传进来的参数:', obj)

      // 验证卡上次数是否和系统次数一直
      // if (this.cardInfo) {
      //   if ((this.cardInfo.Times - 0) - (obj.f_times - 0) != 0) {
      //     this.$showAlert('此卡上的次数和')
      //   }
      // }

      // 验证是否有低保户/黑名单/档案完善等
      let self = this
      let expiration = await this.$resetpost('rs/sql/sale_getUserProperties', {data: {f_userinfo_id: `${obj.f_userinfo_id}`}}, {resolveMsg: '', rejectMsg: ''})
      let invo = await this.$resetpost('rs/sql/sale_getUserInvo', {data: {f_userinfo_code: `${obj.f_userinfo_code}`}}, {resolveMsg: '', rejectMsg: ''})
      // console.log("查看传进来的invo参数:",invo)
      let isbasicliving = false
      let isBlackList = false
      let isInvoList = false
      if (invo.length > 0) {
        isInvoList = true
      }
      expiration.data.forEach(function (value) {
        if (value.f_parameter_key === 'dibaohu_endDate' && value.days < parseInt(self.dibao_remind)) {
          isbasicliving = true
        }
        // else  if (value.f_parameter_key === 'blackList') {
        //   isBlackList = true
        // }
      })
      // console.log("查看传进来的expiration参数:",expiration)
      // 报建等第三方导入进来的档案可能会出现无气表品牌，气价等信息的情况,此时不能进行业务操作，需提示完善档案信息
      if ((!obj.f_price_id || !obj.f_gasmodel_id || !obj.f_gasbrand_id || isbasicliving || isBlackList || isInvoList) && obj.f_user_state !== '预备') {
        this.modaldata = expiration
        this.modaldata2 = invo
        this.modalrow = obj
        this.showModal = true
        return false
      } else {
        return true
      }
    },
    clean () {
      this.row = null
      this.cardInfo = null
    },
    clearCondition () {
      Object.keys(this.$refs.list.$refs.paged.$refs.cri.model).forEach((key) => {
        this.$refs.list.$refs.paged.$refs.cri.model[key] = ''
      })
      // console.log('清数据。。。', this.$refs.list.$refs.paged.$refs.grid.model.rows)
      // this.$set('$refs.list.$refs.paged.$refs.grid.model.rows', [])
    },
    dealMsg (obj) {
      console.log('==================obj', obj)
      this.listpage = false
      this.rowData = obj
    },
    cancel (obj) {
      this.listpage = true
    },
    modalsuccess (obj) {
      this.showModal = false
      this.$refs.list.criteriaShow = false
      this.row = obj
      this.cardInfo = this.$refs.list.cardInfo
      // 将列表只显示该条数据
      this.$refs.list.refreshRow(obj)
    },
    async serRow (obj) {
      let newdata = await this.$resetpost('rs/sql/sale_getUser', {
        data: {condition: `i.f_userinfo_code='${obj.f_userinfo_code}' and u.f_filialeid = '${obj.f_filialeid}'`, orderitem: `f_userinfo_id Desc`}
      }, {resolveMsg: null, rejectMsg: '获取用户信息失败'})
      this.toBusiness(newdata.data[0])
      // this.$refs.list.search()
    }
  },
  events: {
    'error' (name, row, res) {
      this.clean()
    },
    'row' (val) {

    },
    'success' (name, row, res) {
      // name === '发卡售气' ? this.$refs.list.toNext(this.index) : null
      if (name === '机表收费确认') {
        this.serRow(row)
        return
      }
      console.log('成功事件')
      this.clean()
      this.$refs.list.search()
      // this.clearCondition()
      // this.$refs.card.search()
      // this.$refs.info.$refs.recordstoryinfo.getdata()
    },
    'clean' (row) {
      // this.$refs.list.singleRefresh(row)
      this.clean()
    },
    'refresh' () {
      this.clean()
      this.clearCondition()
      this.$refs.list.searchNoData()
      this.$refs.card.search()
    },
    'refreshrow' (val) {
      console.log(val)
      this.row = val
      this.$refs.list.refreshRow(val)
      // this.$refs.card.search()
    },
    'cancelclean' (row) {
      this.clean()
      this.$refs.list.searchNoData()
      // this.$refs.card.search()
    }
  },
  watch: {
    'row' () {
      // this.$refs.tab.$el.click()
    }
  }
}
</script>
<style>
.basic-main {
  width: 79%;
}
</style>
