<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="客户名称">
          {{ form.dealerName }}
        </a-form-model-item>
        <a-form-model-item label="备注" prop="remarks">
          <a-textarea :rows="4" :maxLength="300" placeholder="请输入备注（最多300个字符）" 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: 50px;">
          <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 { verifyAccountBillCreate, findLastBillRemarks } from '@/api/verifyAccount.js'
export default {
  name: 'ASBaseModal',
  mixins: [commonMixin],
  components: { VSelect },
  props: {
    show: [Boolean],
    itemSn: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      opened: this.show,
      spinning: false,
      disabled: false,
      title: '新增对账单',
      confirmLoading: false,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        dealerSn: undefined,
        dealerName: undefined,
        remarks: ''
      },
      rules: {
        remarks: [{ required: true, message: '请输入备注', trigger: ['change', 'blur'] }]
      }
    }
  },
  methods: {
    //  保存
    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))
      _this.spinning = true
      verifyAccountBillCreate(form).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$emit('refashData', res.data)
          _this.cancel()
        }
        _this.spinning = false
      })
    },
    cancel () {
      this.opened = false
      this.$emit('cancel')
      this.$refs.ruleForm.resetFields()
    },
    async pageInit (data) {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields()
      })
      this.form = {
        dealerSn: data.dealer.dealerSn,
        dealerName: data.dealer.dealerName,
        remarks: ''
      }
      // 获取默认备注
      const res = await findLastBillRemarks({ sn: data.dealer.dealerSn })
      if (res.status == 200) {
        this.form.remarks = res.data.remarks
      }
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
    }
  }
}
</script>
