<template>
  <a-modal
    v-model="opened"
    :title="title"
    centered
    :maskClosable="false"
    :width="600"
    :footer="null"
    @cancel="cancel"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="chooseCustom-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="客户名称" prop="dealerSn">
          <dealerSubareaScopeList id="manualEntryForm-dealer" :disabled="!!itemSn&&type == 0||type==1" ref="settleClientName" @change="custChange" />
        </a-form-model-item>
        <a-form-model-item label="借贷类型" prop="changeType">
          <v-select
            v-model="form.changeType"
            ref="changeType"
            showType="radio"
            id="manualEntryForm-type"
            code="VERIFY_ACCOUNT_CHANGE_TYPE"
            placeholder="请选择借贷类型"
            allowClear></v-select>
        </a-form-model-item>
        <a-form-model-item label="借贷金额" prop="changeAmount">
          <a-input-number
            id="manualEntryForm-amount"
            :min="0"
            :max="99999999"
            v-model.trim="form.changeAmount"
            allowClear
            style="width:200px;"
            placeholder="请输入借贷金额"/>
          元
        </a-form-model-item>
        <a-form-model-item label="备注">
          <a-textarea :rows="4" :maxLength="100" placeholder="请输入备注（最多100个字符）" v-model="form.remarks"></a-textarea>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;margin-top: 30px;">
          <a-button @click="cancel" style="margin-right: 15px" id="chooseCustom-btn-back">取消</a-button>
          <a-button type="primary" :loading="confirmLoading" @click="handleSubmit" id="chooseCustom-btn-submit">保存</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
// 组件
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import { verifyAccountChangeSave, verifyAccountChangeDetail } from '@/api/verifyAccountChange.js'
import { verifyAccountChangeInsert } from '@/api/verifyAccount.js'
export default {
  name: 'ASBaseModal',
  mixins: [commonMixin],
  components: { VSelect, dealerSubareaScopeList },
  props: {
    show: [Boolean],
    itemSn: {
      type: String,
      default: ''
    },
    dealerSn: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: '0'
    }
  },
  data () {
    let checkPending
    const checkAmount = (rule, value, callback) => {
      clearTimeout(checkPending)
      if (!value && value != 0) {
        return callback(new Error('请输入借贷金额'))
      }
      checkPending = setTimeout(() => {
        if (value <= 0) {
          callback(new Error('借贷金额必需大于0'))
        } else {
          callback()
        }
      }, 500)
    }
    return {
      opened: this.show,
      spinning: false,
      disabled: false,
      title: '调账',
      confirmLoading: false,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        dealerSn: undefined,
        dealerName: undefined,
        changeType: undefined,
        changeAmount: undefined,
        remarks: ''
      },
      rules: {
        dealerSn: [ { required: true, message: '请选择客户', trigger: ['change', 'blur'] } ],
        changeType: [ { required: true, message: '请选择借贷类型', trigger: ['change', 'blur'] } ],
        changeAmount: [
          { required: true, validator: checkAmount, trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  methods: {
    // 客户
    custChange (v) {
      if (v && v.key) {
        this.form.dealerSn = v.key
        this.form.dealerName = v.row ? v.row.dealerName : ''
      } else {
        this.form.dealerSn = ''
        this.form.dealerName = ''
      }
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.salesSaveFun()
        } else {
          return false
        }
      })
    },
    // 新建或编辑销售单
    salesSaveFun () {
      const _this = this
      const form = JSON.parse(JSON.stringify(_this.form))
      const fun = this.type == 0 ? verifyAccountChangeSave : verifyAccountChangeInsert
      if (this.type == 1) {
        form.verifyAccountBillSn = this.itemSn
      }
      _this.spinning = true
      fun(form).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          this.$emit('ok', res.data)
          _this.cancel()
        }
        _this.spinning = false
      })
    },
    //  详情
    getDetail () {
      this.title = '编辑调账'
      this.spinning = true
      this.confirmLoading = true
      verifyAccountChangeDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          const data = res.data
          data.changeAmount = Math.abs(data.changeAmount)
          this.form = Object.assign(this.form, data)
          // 回显客户
          if (this.form.dealerSn) {
            this.$nextTick(() => {
              this.$refs.settleClientName.getDetail(this.form.dealerSn)
            })
          }
        } else {
          this.$refs.ruleForm.resetFields()
        }
        this.spinning = false
        this.confirmLoading = false
      })
    },
    cancel () {
      this.opened = false
      if (this.$refs.settleClientName) {
        this.$refs.settleClientName.resetForm()
      }
      this.$refs.ruleForm.resetFields()
      this.$emit('cancel')
    },
    pageInit () {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields()
        if (this.$refs.settleClientName) {
          this.$refs.settleClientName.resetForm()
        }
      })
      this.form = {
        dealerSn: undefined,
        dealerName: undefined,
        changeType: undefined,
        changeAmount: undefined,
        remarks: ''
      }
      //  编辑页
      if (this.itemSn && this.type == 0) {
        this.getDetail()
      }
      // 回显客户
      if (this.type == 1) {
        this.$nextTick(() => {
          this.form.dealerSn = this.dealerSn
          this.$refs.settleClientName.getDetail(this.dealerSn)
        })
      }
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
      if (newValue) {
        this.pageInit()
      }
    }
  }
}
</script>
