<template>
  <div>
    <div class="columns is-multiline mx-0">
<div class="column is-12">
    <div class="field">
  <label class="label">{{$lang('source-account')}}<b class="ml-1 has-text-danger">*</b></label>
  <div class="control">
    <SearchBox v-bind="{api:'internalaccount', field:'label', column:['label'], first: true, optionid: row.id}" 
    @option="selected('_source', $event)" v-if="!record.id"></SearchBox>
    <span v-else>{{record.account__code}}</span>
  </div>
  <p class="help is-danger" v-if="errors.source">{{ errors.source }}</p>
</div>
</div>
<div class="column is-12">
    <div class="field">
  <label class="label">{{$lang('dest-account')}}<b class="ml-1 has-text-danger">*</b></label>
  <div class="control">
    <SearchBox v-bind="vbind" @option="selected('_target', $event)" v-if="vbind"></SearchBox>
    <span v-else>{{record.account__code}}</span>
  </div>
  <p class="help is-danger" v-if="errors.target">{{ errors.target }}</p>
</div>
</div>
<div class="column is-12">
  <div class="field">
      <label class="label">{{ $lang('amount-only') }}<b class="ml-1 has-text-danger">*</b></label>
      <div class="control">
      <InputNumber v-bind="{record: record, attr: 'amount', placeholder: ''}" @number="selected('amount', $event)"></InputNumber>
    </div>
    <p class="help is-danger" v-if="errors.amount">{{errors.amount}}</p>
    </div>
  </div>
  <div class="column is-12">
      <div class="field">
  <label class="label">{{ $lang('content') }}<b class="ml-1 has-text-danger">*</b></label>
  <div class="control">
    <textarea class="textarea" rows="2" v-model="record.content"></textarea>
  </div>
  <p class="help is-danger" v-if="errors.content">{{errors.content}}</p>
</div>
    </div>
</div>
<div class="mt-5">
    <button class="button is-primary has-text-white" @click="confirm()">{{ $lang('confirm') }}</button>
  </div>
  <Modal @close="showmodal=undefined" v-bind="showmodal" @confirm="update()" v-if="showmodal"></Modal>
</div>
</template>
<script>
export default {
  props: ['pagename', 'row'],
  data() {
    return {
      record: {},
      errors: {},
      showmodal: undefined,
      vbind: undefined
    }
  },
  methods: {
    selected(attr, obj) {
      this.record[attr] = obj
      this.record = this.$copy(this.record)
      if(attr==='_source') {
        let currency = obj? obj.currency : undefined
        this.vbind = undefined
        setTimeout(()=>this.vbind = {api:'internalaccount', field:'label', column:['label'], first: true, filter: {currency: currency}})
      }
    },
    checkError() {
      this.errors = {}
      if(this.$empty(this.record._source)) this.errors.source = 'Chưa chọn tài khoản nguồn'
      if(this.$empty(this.record._target)) this.errors.target = 'Chưa chọn tài khoản đích'
      if(Object.keys(this.errors).length===0) {
        if(this.record._source.id===this.record._target.id) this.errors.target = 'Tài khoản nguồn phải khác tài khoản đích'
      }
      if(this.$empty(this.record.amount)) this.errors.amount = 'Chưa nhập số tiền'
      else if(this.$formatNumber(this.record.amount)<=0) this.errors.amount = 'Số tiền phải > 0'
      else if(this.record._source.balance<this.$formatNumber(this.record.amount)) this.errors.source = 'Tài khoản nguồn không đủ số dư để điều chuyển'
      if(this.$empty(this.record.content)) this.errors.content = 'Chưa nhập nội dung'
      return Object.keys(this.errors).length>0
    },
    confirm() {
      if(this.checkError()) return
      this.showmodal = {component: `dialog/Confirm`,vbind: {content: this.$lang('confirm-action'), duration: 10}, 
      title: this.$lang('confirm'), width: '500px', height: '100px'}
    },
    async update() {
      let content = `${this.record.content} (${this.record._source.code} -> ${this.record._target.code})`
      let obj1 = {code: this.record._source.code, amount: this.record.amount, content: content, type: 'DR', category: 2, user: this.$store.login.id}
      let rs1 = await this.$insertapi('accountentry', obj1, undefined, false)
      if(rs1==='error') return
      let obj2 = {code: this.record._target.code, amount: this.record.amount, content: content, type: 'CR', category: 2, user: this.$store.login.id}
      let rs2 = await this.$insertapi('accountentry', obj2, undefined, false)
      if(rs2==='error') return
      let data = await this.$getdata('internalaccount', {code__in: [this.record._source.code, this.record._target.code]})
      this.$updatepage(this.pagename, data)
      this.$dialog(`Điều chuyển vốn <b>${this.$numtoString(this.record.amount)}</b> từ <b>${this.record._source.code}</b> tới <b>${this.record._target.code}</b> thành công.`, 'Thành công', 'Success', 10)
      this.$emit('close')
    }
  }
}
</script>