<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 :disabled="!!itemSn" ref="dealer" @change="custChange" />
        </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 dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import { verifyAccountBillCreate, verifyAccountBillModify, verifyAcountBillDetail, findLastBillRemarks } from '@/api/verifyAccount.js'
export default {
  name: 'ASBaseModal',
  mixins: [commonMixin],
  components: { VSelect, dealerSubareaScopeList },
  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: {
        dealerSn: [ { required: true, message: '请选择客户', trigger: ['change', 'blur'] } ],
        remarks: [{ required: true, message: '请输入备注', trigger: ['change', 'blur'] }]
      }
    }
  },
  methods: {
    // 客户
    custChange (v) {
      if (v && v.key) {
        this.form.dealerSn = v.key
        this.form.dealerName = v.row ? v.row.dealerName : ''
        this.getDefRemarks()
      } else {
        this.form.dealerSn = ''
        this.form.dealerName = ''
      }
    },
    // 获取上次备注
    async getDefRemarks () {
      const data = await findLastBillRemarks({ sn: this.form.dealerSn }).then(res => res.data)
      this.form.remarks = data ? data.remarks || '' : ''
    },
    //  保存
    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))
      // 编辑 or 新增
      const funs = form.verifyAccountBillSn ? verifyAccountBillModify : verifyAccountBillCreate

      _this.spinning = true
      funs(form).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          // 编辑
          if (this.itemSn) {
            _this.$emit('refashData')
          } else {
            // 新增
            _this.$router.push({ name: 'accountStatementEdit', params: { sn: res.data.verifyAccountBillSn } })
          }
          _this.cancel()
        }
        _this.spinning = false
      })
    },
    //  详情
    getDetail () {
      this.title = '编辑对账单'
      this.spinning = true
      this.confirmLoading = true
      verifyAcountBillDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          const data = res.data
          this.form = Object.assign(this.form, data)
          // 回显客户
          if (this.form.dealerSn) {
            this.$nextTick(() => {
              this.$refs.dealer.getDetail(this.form.dealerSn)
            })
          }
        } else {
          this.$refs.ruleForm.resetFields()
        }
        this.spinning = false
        this.confirmLoading = false
      })
    },
    cancel () {
      this.opened = false
      this.$emit('cancel')
      this.$refs.ruleForm.resetFields()
    },
    async pageInit () {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields()
        if (this.$refs.dealer) {
          this.$refs.dealer.resetForm()
        }
      })
      this.form = {
        dealerSn: undefined,
        dealerName: undefined,
        remarks: ''
      }
      //  编辑页
      if (this.itemSn) {
        this.getDetail()
      }
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
      if (newValue) {
        this.pageInit()
      }
    }
  }
}
</script>
