<template>
  <div id="unit" class="flex-row three" v-show="listpage">
    <div class="binary-left" style="overflow: scroll;width:20%" >
      <mulitinfo style="height:auto" @deal-msg="dealMsg" :data="row" :card-data="cardInfo" v-ref:info></mulitinfo>
    </div>
    <div class="binary-right" style="width:60%;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:25%" >
      <tabset v-ref:tabs :active="0" :close="false">
        <tab header="其他信息" id="clickMe" v-ref:tab>
          <otherinfo @setstate="setstate" style="height:auto" :state="state" @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 './../../../../components/charge/ChargeList'
  import ChargeOper from './ChargeOper'
  import {HttpResetClass} from 'vue-client'

export default {
    title: '用户操作',
    components: {
      ChargeList, ChargeOper
    },
    data () {
      return {
        // 页面开关
        listpage: true,
        f_orgid: '',
        row: null,
        state: '已领',
        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)) {
    // 对此数据进行验证
          let warningInfo = await this.validateRow(obj)
          this.state = warningInfo.data.state.invoice ? '已领' : '未领'
          if (warningInfo.data.canCharge) {
            if (warningInfo.data.warningNum > 0) {
              let msg = warningInfo.data.msgData + '确定对客户' + obj.f_user_name + '进行操作吗？'
              this.$showMessage(msg, ['confirm', 'cancel']).then((res) => {
                if (res === 'confirm') {
          // 获取未写卡或者写卡失败记录
                  this.$SqlService.singleTable('t_sellinggas', `f_state = '有效' and (f_write_card = '未写卡' or (f_write_card = '写卡失败' and f_operate_date >= DATEADD(HOUR, -3, GETDATE()))) and f_userfiles_id = '${obj.f_userfiles_id}'`).then((getUnWriteSell) => {
                    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)
                    }
                  })
                }
              })
            } else {
        // 获取未写卡或者写卡失败记录
              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)
              }
            }
          } else {
            this.$showAlert(warningInfo.data.msgData, 'warning', 5000)
          }
  }
      },
  close () {
    this.showModal = false
    this.modaldata = null
    this.modalrow = null
    this.clean()
    this.$refs.list.search()
    // this.$refs.card.search()
  },
  setstate (state) {
    console.log('-----------state----')
    this.state = state ? '已领' : '未领'
  },
  async validateRow (obj) {
    console.log('查看传进来的参数:', obj)
    let warningInfo = await this.$resetpost('rs/logic/getWarningMsg',
      {data: {
        f_userfiles_id: obj.f_userfiles_id,
        f_userinfo_code: obj.f_userinfo_code,
        f_userinfo_id: obj.f_userinfo_id
      }
      }
      , {resolveMsg: '', rejectMsg: '获取提示失败'})
    return warningInfo
  },
  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>
