<template>
  <a-modal
    centered
    class="fundAount-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    :title="(isEdit?'编辑':'新增')+'资金账户'"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="fundAount-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol" >
        <a-form-model-item label="账户类型" prop="settleStyleSn">
          <v-select
            code="SETTLE_STYLE"
            id="fundAount-settleStyleSn"
            v-model="form.settleStyleSn"
            @change="settleStyleChange"
            allowClear
            placeholder="请选择账户类型"
          ></v-select>
        </a-form-model-item>
        <a-form-model-item label="账户名称" prop="accountName">
          <a-input
            id="fundAount-accountName"
            :maxLength="30"
            v-model.trim="form.accountName"
            placeholder="请输入账户名称(最多30个字符)"
            allowClear />
        </a-form-model-item>
        <a-form-model-item label="账户号码" prop="accountNo">
          <a-input
            id="fundAount-accountNo"
            :maxLength="30"
            v-model.trim="form.accountNo"
            placeholder="请输入账户号码(最多30个字符)"
            allowClear />
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;">
          <a-button type="primary" :loading="confirmLoading" @click="handleSubmit" id="chooseCustom-btn-submit">保存</a-button>
          <a-button @click="isShow=false" style="margin-left: 15px" id="chooseCustom-btn-back">取消</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 { settleAccountSave, settleAccountDetail } from '@/api/settleAcount'
export default {
  name: 'BasicInfoModal',
  components: { VSelect },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      confirmLoading: false,
      isShow: this.openModal, //  是否打开弹框
      isEdit: false,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        settleStyleSn: '',
        settleStyleName: '',
        accountName: '',
        accountNo: ''
      },
      rules: {
        settleStyleSn: [
          { required: true, message: '请选择账户类型', trigger: 'change' }
        ],
        accountName: [
          { required: true, message: '请输入账户名称(最多30个字符)', trigger: 'change' }
        ],
        accountNo: [
          { required: true, message: '请输入账户号(最多30个字符)', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.ruleForm.resetFields()
      this.form = {
        settleStyleSn: '',
        settleStyleName: '',
        accountName: '',
        accountNo: ''
      }
      this.isEdit = false
    },
    settleStyleChange (v, name) {
      this.form.settleStyleName = name.dispName
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.spinning = true
          settleAccountSave(_this.form).then(res => {
            if (res.status == 200) {
              _this.isShow = false
              _this.$emit('ok', res.data)
              _this.spinning = false
              _this.$message.info(res.message)
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    // 编辑信息
    setData (data) {
      if (data && data.settleAccountSn) {
        settleAccountDetail({ settleAccountSn: data.settleAccountSn }).then(res => {
          if (res.status == '200') {
            this.form = Object.assign(this.form, res.data || {})
            this.isEdit = true
          }
        })
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetForm()
      }
    }
  }
}
</script>

<style lang="less">
  .fundAount-basicInfo-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .radio-s{
      margin-bottom: 8px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
