<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">
              <label for="f_newuser_name" id="label" class="font_normal_body">转出方编号</label>
              <input class="input_search" style="width:60%" type="text" title="转出方编号" v-model="model.out_userinfo_code"
                  readonly   placeholder="用户姓名">
            </div>
            <div class="col-sm-4">
            <label for="f_newuser_name" id="label" class="font_normal_body">转出方姓名</label>
              <input class="input_search" style="width:60%" type="text" title="转出方姓名" v-model="model.out_user_name"
                  readonly   placeholder="用户姓名">
            </div>
          </div>
          <div class="row" style="margin-top:10px;">
            <div class="col-sm-4" title="回车搜索传出方信息" :class="[$v.in_userinfo_code.required ? 'has-error' : 'has-success']">
              <label for="f_newuser_name" id="label" class="font_normal_body">转入方</label>
              <input class="input_search" style="width:60%" type="text" title="回车搜索转入方信息" v-model="model.in_userinfo_code"
                     v-validate:in_userinfo_code='{required: true }'  @keydown.enter="searchByCode"  placeholder="转出方编号，按下回车进行查询">
            </div>
            <div class="col-sm-4" :class="[$v.in_user_name.required ? 'has-error' : 'has-success']">
              <label for="f_newuser_name" id="label" class="font_normal_body">转入方姓名</label>
              <input class="input_search" style="width:60%" type="text" title="转入方姓名" v-model="model.in_user_name"
                     v-validate:in_user_name='{required: true }' readonly  placeholder="转入方姓名">
            </div>
          </div>
          <div class="row" style="margin-top:10px;">
            <div class="col-sm-12" :class="[$v.f_comments.required ? 'has-error' : 'has-success']">
              <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"
                     v-validate:f_comments='{required: true }'
                     placeholder="备注"/>
            </div>
          </div>
        </form>
        <div style="text-align:right;height:auto;">
          <button class="button_search" @click="notarize = true" :disabled='!$v.valid'>
            确认
          </button>
          <button class="button_clear" @click="clear()">
            取消
          </button>
        </div>
      </validator>
    </div>
    <upload :blodid="blodid" v-if="config.showupload" isremark="true" fusetype="用户记录转移管理"></upload>
    <modal v-if="notarize" :show.sync="notarize" width="100%" title="信息确认" v-ref:modal large backdrop="false">
      <article slot="modal-body" class="modal-body" style="text-align: center">
          <strong>此操作具有一定的风险性，需要您再次确认转出方用户信息，请输入转入方用户编号</strong>
        <div class="row">
          <div class="col-sm-6 col-sm-offset-3">
            <label for="f_newuser_name" id="label" class="font_normal_body">再次确认用户编号</label>
            <input class="input_search" style="width:60%" type="text" title="用户编号" v-model="notarize_userinfo_code"
                      placeholder="用户编号">
          </div>
        </div>
        <!--<div class="row" v-if="row.f_meter_type== '物联网表'||row.f_meter_type== '机表' || row.f_meter_type == '金额卡表'">-->
          <!--<div class="col-sm-6 col-sm-offset-3">-->
            <!--<label for="f_auto_operate" id="f_auto_operate" class="font_normal_body">自动生成补费扣费</label>-->
            <!--<v-select :value.sync="model.f_auto_operate" v-model='model.f_auto_operate'-->
                      <!--style="width:60%"-->
                      <!--:options='autoOperate' placeholder='是否自动生成补费扣费'-->
                      <!--close-on-select-->
                      <!--condition="f_state = '{}'"></v-select>-->
          <!--</div>-->
        <!--</div>-->
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button class="button_search" @click="confirm()" :disabled='!notarize_userinfo_code'>
          确认
        </button>
        <button class="button_clear" @click="notarize = false">
          取消
        </button>
      </footer>
    </modal>
  </div>
</template>
<script>

import {HttpResetClass} from 'vue-client'

let asyncTransferManage = async function (self) {
  // await self.$getConfig(self, 'TransferRecord')
}

export default {
  title: '过户管理',
  props: ['row', 'records'],
  data () {
    return {
      // 附件上传必须
      blodid: '',
      notarize: false,
      notarize_userinfo_code: '',
      config: {
        showupload: false,
        tableNames: [
          {
            table: 't_sellinggas',
            name: '收费记录',
            checked: true,
            disabled: true
          }, {
            table: 't_chargeprice',
            name: '划价记录',
            checked: true,
            disabled: true
          }, {
            table: 't_record',
            name: '记录',
            checked: true,
            disabled: true
          }]
      },
      model: {
        table_name: [],
        in_userinfo_id: '',
        in_userfiles_id: '',
        in_userinfo_code: '',
        in_user_id: '',
        out_userinfo_id: '',
        out_userfiles_id: '',
        out_user_id: '',
        // f_auto_operate: '否',
        out_userinfo_code: '',
        in_user_name: '',
        out_user_name: '',
        f_orgid: '',
        f_orgname: '',
        f_depid: '',
        f_depname: '',
        f_operatorid: '',
        f_operator: '',
        f_comments: ''
      },
      autoOperate: [{ label: '是', value: '是' }, { label: '否', value: '否' }]
    }
  },
  ready () {
    this.blodid = this.row.f_userinfo_id
    Object.assign(this.model, {
      out_userinfo_id: this.row.f_userinfo_id,
      out_userinfo_code: this.row.f_userinfo_code,
      out_userfiles_id: this.row.f_userfiles_id,
      out_user_id: this.row.f_user_id,
      out_user_name: this.row.f_user_name,
      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
    })
    asyncTransferManage(this)
  },
  methods: {
    confirm () {
      // 首先验证用户
      new HttpResetClass().load('post', 'rs/sql/sale_getUser',
        {
          data: {
            condition: `f_userinfo_code = '${this.notarize_userinfo_code}'`,
            orderitem: ` f_userinfo_code Desc`
          }
        },
        {resolveMsg: null, rejectMsg: '查找用户出错'}).then(
        res => {
          if (res.data.length === 0) {
            this.$showAlert('验证信息有误，请重新输入用户编号进行查询并谨慎核对！！！', 'warning', 2000)
          } else if (this.model.in_user_id !== res.data[0].f_user_id || this.model.in_userinfo_id !== res.data[0].f_userinfo_id || this.model.in_userfiles_id !== res.data[0].f_userfiles_id) {
            this.$showAlert('验证信息有误，请重新输入用户编号进行查询并谨慎核对！！！', 'warning', 2000)
            this.model.in_user_name = undefined
          } else {
            this.model.table_name = this.config.tableNames.map(item => item.table)
            // this.model.f_auto_operate = this.model.f_auto_operate[0]
            new HttpResetClass().load('post', 'rs/logic/transferRecords',
              {
                model: this.model,
                records: this.records
              },
              {resolveMsg: null, rejectMsg: '转移记录出错'}).then(
              res => {
                this.$showAlert('操作成功', 'success', 2000)
                this.clear()
              }
            )
          }
        }
      )
    },
    clear () {
      this.$dispatch('refresh')
    },
    searchByCode () {
      if (this.model.in_userinfo_code === this.model.out_userinfo_code) {
        this.$showAlert('转出方转入方信息不能一致，请谨慎核对！！！', 'warning', 2000)
        return
      }
      new HttpResetClass().load('post', 'rs/sql/sale_getUser',
        {
          data: {
            condition: `f_userinfo_code = '${this.model.in_userinfo_code}'`,
            orderitem: ` f_userinfo_code Desc`
          }
        },
        {resolveMsg: null, rejectMsg: '查找用户出错'}).then(
          res => {
            if (res.data.length > 0) {
              Object.assign(this.model, {
                in_userinfo_code: res.data[0].f_userinfo_code,
                in_user_id: res.data[0].f_user_id,
                in_userinfo_id: res.data[0].f_userinfo_id,
                in_userfiles_id: res.data[0].f_userfiles_id,
                in_user_name: res.data[0].f_user_name
              })
            } else {
              this.$showAlert('查无用户信息，请谨慎核对！！！', 'warning', 2000)
              this.model.in_user_name = undefined
            }
          }
      )
    }
  },
  events: {}
}
</script>

<style scoped>
#label {
  margin-top: 5px;
}
</style>
