<template>
  <div style="">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <p class="bg-info text-center" style="padding: 8px;">过户信息</p>
        <div class="row" style="margin-top:10px;">
          <div class="col-sm-4 form-group form-input-group" :class="[$v.f_newuser_name.required ? 'has-error' : 'has-success']">
            <label for="f_newuser_name" class="col-sm-2 control-label" >新姓名</label>
            <input class="form-control" type="text" title="新姓名" v-model="model.f_newuser_name"
             v-validate:f_newuser_name='{required: true }' placeholder="新姓名" v-next-el='phone'>
             <!-- <label v-if="$v.f_newuser_name.required && namespan" class = "" style = "color:red">不能为空</label> -->
          </div>
          <div class="col-sm-4 form-group form-input-group" :class="[$v.f_newuser_phone.minlength || $v.f_newuser_phone.maxlength ? 'has-error' : '']">
            <label for="f_newuser_phone" class="control-label">新电话</label>
            <input title="新电话"  class="form-control" type="text" v-model="model.f_newuser_phone"
                   v-validate:f_newuser_phone='{minlength: 11, maxlength: 11  }'
                   placeholder="新电话"  v-next-el='number' v-el:phone>
          </div>
          <div class="col-sm-4 form-group form-input-group">
            <label for="f_credentials" class="control-label">证件类型</label>
            <v-select :value.sync="model.f_newcredentials"
                      :options='credentials' placeholder='证件类型'
                      close-on-select></v-select>
          </div>
        </div>
        <div>
          <div class="col-sm-8 form-group form-input-group" :class="[$v.f_idnumber2.identityCardValid ? 'has-error' : '']" v-if="model.f_newcredentials[0] === '身份证'">
            <label for="f_idnumber" class="control-label">证件号码</label>
            <input type="text"  v-model="model.f_newidnumber" v-validate:f_idnumber2='{identityCardValid: true}' class="form-control" placeholder="新身份证号">
          </div>
          <div class="col-sm-8 form-group form-input-group" v-if="model.f_newcredentials[0] !== '身份证'"> <!---->
            <label for="f_idnumber" class="control-label">证件号码</label>
            <input type="text"  v-model="model.f_newidnumber" class="form-control" placeholder="新身份证号" >
          </div>
          <div class="col-sm-4 form-group form-input-group">
            <label for="f_print" class="control-label col-sm-2">打印格式</label>
            <div class="col-sm-2">
              <v-select id="print"
                        v-model="f_print"
                        placeholder='请选择'
                        :value.sync="model.f_print"
                        :options='printstyle'
                        close-on-select clear-button></v-select>
            </div>
          </div>
        </div>
        <div class="row" style="margin-top:10px;">

          <div class="col-sm-4 form-group form-input-group">
            <label for="f_paymenttype" class="control-label">付款方式</label>
            <v-select :value.sync="model.f_payment"
                      v-model="model.f_payment"
                      :options='payments' placeholder='付款方式'
                      close-on-select></v-select>
          </div>

          <div class="col-sm-4 form-group form-input-group">
            <label for="f_transfer_fees" class="control-label">过户费</label>
              <input class="form-control" type="number"  v-model="model.f_transfer_fees"  placeholder="过户费"
                 v-next-el='accopen' v-el:fees v-if="bankinfo">
              <input class="form-control" type="number"  v-model="model.f_transfer_fees"  placeholder="过户费"
                v-el:fees v-else>
        </div>

          <!-- <label for="f_chargename" class=" col-sm-1 control-label">付费方姓名:</label>
          <div class="col-sm-2">
            <input class="form-control" type="text" v-model="model.f_chargename" placeholder="付费方姓名">
          </div> -->
        </div>
        <div class="row" style="margin-top:10px;" v-if = "bankinfo">
          <div class="col-sm-4 form-group form-input-group">
            <label for="f_newbank_accopen" class=" control-label">新银行开户名:</label>
            <input class="form-control" type="text" title="新银行开户名" v-model="model.f_newbank_accopen"
             placeholder="新银行开户名" v-next-el='newbank' v-el:accopen>
          </div>
          <div class="col-sm-8 form-group form-input-group" :class="[$v.f_bank_idnumber.identityCardValid ? 'has-error' : 'has-success']">
            <label for="f_bank_idnumber" class=" control-label" >新开户身份证号</label>
              <input class="form-control" type="text" title="新开户身份证号" v-model="model.f_bank_idnumber"
               v-validate:f_bank_idnumber='{identityCardValid: true }'  placeholder="新开户身份证号">
          </div>

        </div>
        <div class="row" style="margin-top:10px;" v-if = "bankinfo">
          <div class="col-sm-4 form-group form-input-group">
          <label for="f_newbank_name" class="control-label">新银行名:</label>
             <v-select :value.sync="model.f_newbank_name"
                 :options='banknames' placeholder='新银行开户名'
                 close-on-select></v-select>
          </div>
          <div class="col-sm-8 form-group form-input-group">
          <label for="f_newbank_account" class="control-label">新银行账号:</label>
            <input class="form-control" type="text" title="新银行账号" v-model="model.f_newbank_account"
            placeholder="新银行账号" v-el:account>
          </div>
        </div>
        <div class="row" style="margin-top:10px;">
          <div class="col-sm-12 form-group form-input-group">
            <label for="f_note" class=" control-label">备注:</label>
              <textarea class="form-control" title="备注" v-model="model.f_note"
               rows="3" placeholder="备注"></textarea>
          </div>
        </div>
      </form>
      <print-bill :show="print" v-ref:printbill :bill-url='billUrl' v-on:toggle="close" :data='row'></print-bill>
      <div style="text-align:right;height:auto;margin-top:40px;margin-right:50px;">
        <button class="btn btn-success" @click="confirm()"  title="演示系统暂不支持" :disabled="true" id='comprehen-comOpe-chaAcc-traMan-确认'>
          确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认
        </button>
        <button class="btn btn-default" @click="clear()" style="width:80px;margin-left:20px;" id='comprehen-comOpe-chaAcc-traMan-取消'>
          取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消
        </button>
      </div>
      <upload :blodid="blodid" :reload-all="true"></upload>
    </validator>
  </div>
</template>
<script>

let transfersaveGen = async function (self) {
  try {
    let condition = 'f_userinfo_id = ' + self.row.f_userinfo_id + ''
    let bank = await self.$SqlService.singleTable('t_infobank', condition)
    self.setbank(bank)
    let res = await self.$LogicService.changeAccount(self.transferdata)
    console.log('过户完成返回id', res)
    if (self.config.hasPrint) {
      if (self.config.printType === '普通收据') {
        if (self.config.hasBillManage) {
          self.row.id = res.data
          self.row.f_bill_type = '过户收费'
          self.row.f_bill_style = self.model.f_print[0]
          self.billUrl = 'rs/report/transfer_bill'
          self.print = true
        } else {
          self.row.id = res.data
          self.billUrl = 'rs/report/transfer_bill'
          self.print = true
        }
      } else if (self.config.printType === '国税发票') {
        // TODO
      } else if (self.config.printType === '电子发票') {
        // TODO
      }
    } else {
      self.$dispatch('success')
    }
  } catch (error) {
    self.$showAlert(error, 'danger', 0)
    self.$dispatch('refresh')
  }
}

let asyncTransferManage = async function(self){
  await self.$getConfig(self,'TransferManage')
  console.log('过户config', self.config)
}

export default {
  title: '过户管理',
  props: ['row'],
  data () {
    return {
      config: {
        hasPrint : true,  //默认打票
        hasBillManage :false,  //默认不启用发票管理
        printType: '普通收据' // 收据/电子票/专用发票/国税发票
      },
      bankinfo: false,
      numspan: false,
      namespan: false,
      phonespan: false,
      model: {
        f_print: [],
        f_newcredentials: ['身份证'],
        f_payment: ['现金缴费'],
        f_paymenttype: ['现金缴费'],
        f_transfer_fees: this.$appdata.getSingleValue('过户费'),
        f_newbank_accopen: '',
        f_newbank_name: ''
      },
      transferdata: {
        userinfo: {}
      },
      print: false,
      billUrl: '',
      blodid: '', // 附件

      banknames: this.$appdata.getParam('银行名称'),
      costtypes: this.$appdata.getParam('缴费类型'),
      payments: this.$appdata.getParam('付款方式'),
      printstyle: this.$appdata.getParam('打印格式'),
      credentials: this.$appdata.getParam('证件类型')
    }
  },
  ready() {
    this.model.f_print = [this.config.printType]
    this.blodid = this.row.f_userinfo_id
    asyncTransferManage(this)
  },
  methods: {
    // 用户更改信息
    getuserinfo () {
      return {
        f_userinfo_id: this.row.f_userinfo_id,
        f_user_name: this.model.f_newuser_name,
        f_credentials: this.model.f_newcredentials[0],
        f_idnumber: this.model.f_newidnumber,
        f_user_phone: this.model.f_newuser_phone,
        f_cost_type: this.model.f_paymenttype[0],
        version: this.row.userinfo_version,
        infobank: {
          f_userinfo_id: this.row.f_userinfo_id,
          f_bank_name: this.model.f_newbank_name[0],
          f_bank_accopen: this.model.f_newbank_accopen,
          f_bank_account: this.model.f_newbank_account,
          f_bank_idnumber: this.model.f_bank_idnumber
        }
      }
    },
    changecost (val) {
      console.log('观察到变化了，，，，，银行的')
      if (val[0] === '银行代扣') {
        this.bankinfo = true
      } else {
        this.bankinfo = false
      }
      this.$resetValidation()
    },
    // 过户表及操作表信息
    getOperateInfo () {
      return {
        f_userinfo_id: this.row.f_userinfo_id,
        record_userinfo: this.row.f_userinfo_id,
        f_user_id: this.row.f_user_id,
        f_user_name: this.row.f_user_name,
        f_user_phone: this.row.f_user_phone,
        f_idnumber: this.row.f_idnumber,
        f_credentials: this.row.f_credentials,
        f_payment: this.model.f_payment[0],
        f_bill_style: this.model.f_print[0],
        f_newuser_name: this.model.f_newuser_name,
        f_newuser_phone: this.model.f_newuser_phone,
        f_newcredentials: this.model.f_newcredentials[0],
        f_newidnumber: this.model.f_newidnumber,
        f_bank_accopen: null,
        f_bank_name: null,
        f_bank_account: null,
        f_newbank_accopen: this.model.f_newbank_accopen,
        f_newbank_name: this.model.f_newbank_name[0],
        f_newbank_account: this.model.f_newbank_account,
        f_bank_idnumber: this.row.f_bank_idnumber,
        f_newbank_idnumber: this.model.f_bank_idnumber,
        f_transfer_fees: this.model.f_transfer_fees ? this.model.f_transfer_fees : 0,
        f_note: this.model.f_note,
        f_operat_type: '过户',
        f_describe: this.$login.f.name + '对用户' + this.row.f_user_name + '进行过户',
        f_state: '有效',
        f_transfer_state: '有效',
        f_operator: 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,
        deletebank: false
      }
    },
    // 银行信息的设置
    setbank (bank) {
      if (bank.data[0]) {
        if (this.model.f_paymenttype[0] === '现金缴费') {
          delete this.transferdata.userinfo.infobank
        } else if (this.model.f_paymenttype[0] === '银行代扣') {
          delete this.transferdata.userinfo.infobank.f_userinfo_id
        }
      } else {
        if (this.model.f_paymenttype[0] === '现金缴费') {
          this.transferdata.deletebank = true
          delete this.transferdata.userinfo.infobank
        } else if (this.model.f_paymenttype[0] === '银行代扣') {
          this.transferdata.f_bank_name = bank.data[0].f_bank_name
          this.transferdata.f_bank_accopen = bank.data[0].f_bank_accopen
          this.transferdata.f_bank_account = bank.data[0].f_bank_account
          this.transferdata.f_bank_idnumber = bank.data[0].f_bank_idnumber
        }
      }
    },
    transfer () {
      transfersaveGen(this)
    },
    confirm () {
      this.newbankname()
      this.transferdata = Object.assign({}, this.getOperateInfo())
      this.transferdata.userinfo = Object.assign({}, this.getuserinfo())
      console.log('过户保存', this.transferdata)
      this.transfer()
    },
    clear () {
      this.$dispatch('refresh')
    },
    close () {
      this.print = false
      this.clear()
    },
    newbankname () {
      if (this.model.f_paymenttype[0] !== '银行代扣') {
        this.model.f_newbank_accopen = null
      }
    }
  }
}
</script>

<style>
</style>
