<template>
  <a-modal
    centered
    class="checkAccount-modal"
    :footer="null"
    :maskClosable="false"
    title="调账"
    v-model="isShow"
    @cancel="isShow=false"
    :width="700">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="checkAccountModal-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="客户名称">
            {{ dealerName||'--' }}
          </a-form-model-item>
          <a-form-model-item label="调账金额" prop="changeAmount">
            <a-input-number
              id="checkAccountModal-changeAmount"
              v-model="form.changeAmount"
              style="width:90%;"
              :max="99999999"
              :precision="2"
              placeholder="请输入调账金额"/>
            <span style="margin-left:10px;">元</span>
          </a-form-model-item>
          <a-row :gutter="16">
            <a-col :span="10">
              <a-form-model-item label="调账前" :label-col="{span:10}" :wrapper-col="{ span: 10}">
                {{ (dealerNum||dealerNum==0) ?toThousands(dealerNum):'--' }}
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="调账后" :label-col="{span:10}" :wrapper-col="{ span: 10}">
                {{ (dealerNum||dealerNum==0) ?((form.changeAmount||form.changeAmount==0)&&form.changeAmount!='-') ?toThousands(dealerNum*1+form.changeAmount*1):'--' :'--' }}
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-form-model-item label="备注">
            <a-input
              id="checkAccountModal-remark"
              v-model="form.remark"
              style="width:90%;"
              :maxLength="50"
              type="textarea"
              placeholder="请输入备注（最多50字符）"/>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="checkAccountModal-save" @click="handleSubmit">提交</a-button>
          <a-button id="checkAccountModal-cancel" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
        </div>
      </a-spin>
    </div>
    <!-- 提交  选择审批人员 -->
    <chooseDepartUserModal :openModal="openDepartUserModal" @close="openDepartUserModal=false" @submit="handleSave"></chooseDepartUserModal>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import chooseDepartUserModal from './chooseDepartUserModal.vue'
// 接口
import { dealerAccountSubmit } from '@/api/dealerAccount'
export default {
  name: 'CheckAccountModal',
  mixins: [commonMixin],
  components: { chooseDepartUserModal },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    dealerName: {
      type: String,
      default: ''
    },
    dealerNum: {
      type: [String, Number],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false, // 页面加载状态
      formItemLayout: {// form表单中label设置
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      // 提交数据
      form: {
        dealerSn: undefined, // 客户sn
        changeAmount: undefined, // 调账金额
        remark: undefined// 备注
      },
      // 必填项判断
      rules: {
        changeAmount: [{ required: true, message: '请输入调账金额', trigger: 'blur' },
          { pattern: /^(?!0$).+/, message: '调账金额不能为0', trigger: 'blur' }
        ]
      },
      openDepartUserModal: false // 打开 选择审批人员 弹窗
    }
  },
  methods: {
    // 提交  调起选择审批人员
    handleSubmit () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.openDepartUserModal = true
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 保存
    handleSave (val) {
      const _this = this
      val.dealerSn = this.$route.params.sn
      _this.form = { ..._this.form, ...val }
      _this.spinning = true
      dealerAccountSubmit(_this.form).then(res => {
        if (res.status == 200) {
          _this.$emit('ok')
          _this.isShow = false
        }
        _this.spinning = false
      })
    },
    resetFormData () {
      this.form.dealerSn = undefined
      this.form.changeAmount = undefined
      this.form.remark = undefined
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetFormData()
      }
    }
  }
}
</script>

<style lang="less">
  .checkAccount-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 30px;
    }
  }
</style>
