<template>
  <div class="flex-row">
    <div class="span" style="overflow-y: auto;">
      <validator name='v'>
        <form novalidate class="form-horizontal">
          <div class="row" style="margin-top:10px;">
            <div class="col-sm-4" :class="[$v.f_newuser_name.required ? 'has-error' : 'has-success']">
              <label for="f_newuser_name" id="label" class="font_normal_body" >*新&ensp;姓&ensp;名</label>
              <input class="input_search" style="width:60%" 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"  :class="[$v.f_newuser_phone.required ? 'has-error' : '']">
              <label id="label" for="f_newuser_phone" class="font_normal_body">*新&ensp;电&ensp;话</label>
              <input title="新电话"  class="input_search" style="width:60%" type="text" v-model="model.f_newuser_phone" maxlength="11"
                     v-validate:f_newuser_phone='{required: true}'
                     placeholder="新电话"  v-next-el='number' v-el:phone>
            </div>
            <div class="col-sm-4" :class="[$v.f_newrent_phone.required ? 'has-error' : '']">
              <label id="label" for="f_newrent_phone" class="font_normal_body">&nbsp;备用电话</label>
              <!--v-validate:f_newrent_phone='{required: false}'-->
              <input title="备用电话"  class="input_search" style="width:60%" type="text" v-model="model.f_newrent_phone" maxlength="11"

                     placeholder="备用电话"  v-next-el='number' v-el:phone>
            </div>
            <div class="col-sm-4">
              <label for="f_credentials" id="label" class="font_normal_body">&nbsp;证件类型</label>
              <v-select :value.sync="model.f_newcredentials"
                        :options='credentials' placeholder='证件类型'
                        close-on-select></v-select>
            </div>
            <div class="col-sm-8" :class="[$v.f_newidnumber.identityCardValid ? 'has-error' : '']" v-if="model.f_newcredentials[0] === '身份证'">
              <label id="label" class="font_normal_body">*证件号码</label>
              <input type="text"  v-model="model.f_newidnumber" v-validate:f_newidnumber='{identityCardValid: true}' class="input_search" style="width:80%" placeholder="新身份证号">
            </div>
            <div class="col-sm-8" v-if="model.f_newcredentials[0] !== '身份证'"> <!---->
              <label class="font_normal_body">&nbsp;证件号码</label>
              <input type="text"  v-model="model.f_newidnumber" class="input_search" style="width:80%" placeholder="新身份证号" >
            </div>
            <div class="col-sm-4">
              <label id="label" for="f_print" class="font_normal_body">&nbsp;打印格式</label>
              <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 class="row" style="margin-top:10px;">
            <div  class="col-sm-4" :class="[$v.f_transfer_fees.dctest ? 'has-error' : 'has-success']">
              <label id="label" for="f_transfer_fees" class="font_normal_body" title="单值管理：过户费">&nbsp;过&ensp;户&ensp;费</label>
              <input class="input_search" style="width:60%" type="number"  v-model="model.f_transfer_fees"  placeholder="过户费"
                     v-next-el='accopen' v-el:fees v-validate:f_transfer_fees='{dctest: [0, ">=" ]}'>
            </div>
            <div class="col-sm-4">
              <label id="label" for="f_paymenttype" class="font_normal_body">&nbsp;付款方式</label>
              <v-select :value.sync="model.f_payment"
                        v-model="model.f_payment"
                        :options='payments' placeholder='付款方式'
                        close-on-select></v-select>
            </div>
          </div>
          <div class="row" style="margin-top:10px;" v-if = "bankinfo">
            <div class="col-sm-4">
              <label id="label" for="f_newbank_accopen" class=" font_normal_body">新银行开户名:</label>
              <input class="input_search" style="width:60%" type="text" title="新银行开户名" v-model="model.f_newbank_accopen"
                     placeholder="新银行开户名" v-next-el='newbank' v-el:accopen>
            </div>
            <div class="col-sm-8" :class="[$v.f_bank_idnumber.identityCardValid ? 'has-error' : 'has-success']">
              <label for="f_bank_idnumber" class=" font_normal_body" >新开户身份证号</label>
              <input class="input_search" style="width:60%" 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">
              <label  id="label" for="f_newbank_name" class="font_normal_body">新银行名</label>
              <v-select :value.sync="model.f_newbank_name"
                        :options='banknames' placeholder='新银行开户名'
                        close-on-select></v-select>
            </div>
            <div class="col-sm-8">
              <label id="label" for="f_newbank_account" class="font_normal_body">新银行账号</label>
              <input class="input_search" style="width:60%" 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">
              <label id="label" for="f_comments" class="font_normal_body">&nbsp;备&emsp;&emsp;注</label>
              <input class="input_search" style="width:87%" v-model="model.f_comments"
                     placeholder="备注"/>
            </div>
          </div>
        </form>
        <accept-bill :show="acceptprint" :bill-url='acceptUrl' v-on:toggle="close" :data='row' :billdata="printData" @closeaccept="closeAccept" v-ref:acceptbill></accept-bill>
        <validate-bill v-if="hasValidateBill" :data="model" @validate-bill="validateBill"></validate-bill>
        <print-bill :show="print" :bill-config='config' :bill-data='billData' v-on:toggle="close" @printok="printok" :data='row' v-ref:printbill ></print-bill>
        <div style="text-align:right;height:auto;">
          <button class="button_clear" @click="accept()" v-if="config.hasAcceptance" :disabled='!$v.valid'>受理单</button>
          <button class="button_search" @click="confirm()"  :disabled='!$v.valid'>
            确认
          </button>
          <button class="button_clear" @click="clear()" >
            取消
          </button>
        </div>
        <!--<upload :blodid="blodid" isremark="true" fusetype="过户"></upload>-->
      </validator>
    </div>
  </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)
      if (self.config.hasPrint && !this.$login.r.includes('不打印发票')) {
        if (self.model.f_print[0] != '电子发票') {
          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.print = true
          } else {
            self.row.id = res.data
            self.print = true
          }
        } else if (self.model.f_print[0] === '国税发票') {
          // TODO
          self.$dispatch('success')
        } else if (self.model.f_print[0] === '电子发票') {
          // TODO
          self.$dispatch('success')
        }
      } 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)

    self.model.f_print = self.config.printType instanceof Array ? self.config.printType : [self.config.printType]
    self.model.f_payment = [self.config.payment]
    self.mulPrint = self.config.printType instanceof Array

    // 获取当前票号用
    self.model.f_use_type = self.config.billType
    self.model.f_bill_type = self.model.f_print
    self.hasValidateBill = self.config.hasBillManage
  }

  export default {
    title: '过户管理',
    props: ['row'],
    data () {
      return {
        config: {
          hasPrint: false,  // 默认打票
          hasAcceptance: false,
          hasBillManage: false,  // 默认不启用发票管理
          billType: '燃气费',   // 票据类型（燃气费，其他费用，调用的发票代码不同）
          payment: '现金缴费',
          printType: '普通收据' // 收据/电子票/专用发票/国税发票
        },
        bankinfo: false,
        numspan: false,
        namespan: false,
        phonespan: false,
        model: {
          f_print: [],
          f_newcredentials: ['身份证'],
          f_payment: [],
          f_comments: '',
          f_paymenttype: ['现金缴费'],
          f_transfer_fees: this.$appdata.getSingleValue('过户费') ? this.$appdata.getSingleValue('过户费') : 0,
          f_newbank_accopen: '',
          f_newbank_name: ''
        },
        transferdata: {
          userinfo: {}
        },
        print: false,
        printData: {
          username: '',
          userphone: '',
          credentials: '',
          idnumber: ''
        },
        billData: {
          url: 'rs/report/transfer_bill',
          billnumber: ''
        },
        mulPrint: false,

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

        blodid: '', // 附件

        acceptprint: false,
        acceptUrl: '',

        banknames: this.$appdata.getParam('银行名称'),
        costtypes: this.$appdata.getParam('缴费类型'),
        payments: this.$appdata.getParam('付款方式'),
        printstyle: this.$appdata.getParam('打印格式'),
        credentials: this.$appdata.getParam('证件类型')
      }
    },
    ready () {
      console.log('this.row', this.row)
      this.model.f_comments = this.row.f_comments
      this.model.f_print = [this.config.printType]
      this.blodid = this.row.f_userinfo_id
      asyncTransferManage(this)
    },
    methods: {
      accept () {
        // 打印受理单
        this.printData.username = this.model.f_newuser_name ? `'${this.model.f_newuser_name}'` : '' + `'${this.model.f_newuser_name}'`
        this.printData.userphone = this.model.f_newuser_phone ? `'${this.model.f_newuser_phone}'` : '' + `'${this.model.f_newuser_phone}'`
        this.printData.credentials = this.model.f_newcredentials ? `'${this.model.f_newcredentials[0]}'` : '' + `'${this.model.f_newcredentials}'`
        this.printData.idnumber = this.model.f_newidnumber ? `'${this.model.f_newidnumber}'` : '' + `'${this.model.f_newidnumber}'`
        console.log('受累单中printData', this.printData)
        this.acceptUrl = 'rs/report/acceptTransfer_bill'
        this.acceptprint = true
      },
      closeAccept () {
        this.acceptprint = false
      },
      // 用户更改信息
      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_comments: this.model.f_comments,
          f_user_phone: this.model.f_newuser_phone,
          f_rent_phone: this.model.f_newrent_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) {
        if (val[0] === '银行代扣') {
          this.bankinfo = true
        } else {
          this.bankinfo = false
        }
        this.$resetValidation()
      },
      validateBill (val) {
        this.validateOk = val.isOk
        this.billData.bill = val.bill
      },
      printok () {
        this.$dispatch('success')
      },

      // 过户表及操作表信息
      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_serial_number: this.model.f_serial_number,
          f_userfiles_id: this.row.f_userfiles_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_newrent_phone: this.model.f_newrent_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_comments: this.model.f_comments,
          f_operat_type: '过户',
          f_describe: this.$login.f.name + '对用户' + this.row.f_user_name + '进行过户',
          f_state: '有效',
          f_operator: this.$login.f.name,
          f_operatorid: this.$login.f.id,
          f_orgid: this.$login.f.orgid,
          f_orgname: this.$login.f.orgs,
          f_depid: this.$login.f.depids,
          f_depname: this.$login.f.deps,
          f_zoneid: this.$login.f.zoneid,
          f_zones: this.$login.f.zones,

          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
          }
        }
      },
      confirm () {
        this.transferdata = Object.assign({}, this.getOperateInfo())
        this.newbankname()
        this.transferdata.userinfo = Object.assign({}, this.getuserinfo())
        console.log('过户保存', this.transferdata)
        transfersaveGen(this)
      },
      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 scoped>
  #label{
    margin-top: 5px;
  }
</style>
