<template>
  <a-modal
    centered
    class="dealerAccountEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <!-- 表单 -->
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="to-purchase-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="销售单号">
            <span>{{ detailData ? detailData.salesBillNo : '--' }}</span>
          </a-form-model-item>
          <a-form-model-item label="客户名称">
            <span>{{ detailData ? detailData.buyerName : '--' }}</span>
          </a-form-model-item>
          <a-form-model-item label="销售类型" prop="purchaseFlowType">
            <v-select
              v-model="form.purchaseFlowType"
              showType="radio"
              id="purchase-salesType"
              code="PURCHASE_FLOW_TYPE"
              placeholder="请选择销售类型"
              allowClear></v-select>
          </a-form-model-item>
          <a-form-model-item label="直采分类" prop="purchaseType">
            <a-radio-group v-model="form.purchaseType" id="purchase-purchaseType">
              <a-radio value="TIRE">
                轮胎
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item label="备注">
            <a-input
              id="purchase-explainInfo"
              type="textarea"
              :maxLength="30"
              v-model.trim="form.bizRemark"
              placeholder="请输入备注(最多30字符)"
              allowClear />
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="purchase-back" @click="isShow = false">取消</a-button>
          <a-button type="primary" id="purchase-save" @click="handleSave" style="margin-left: 15px;">确定</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { allCreatePurchase } from '@/api/salesNew'
export default {
  name: 'ToPurchaseModal',
  mixins: [commonMixin],
  components: { STable, VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '整单转采购单'
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        purchaseFlowType: undefined,
        purchaseType: 'TIRE',
        bizRemark: ''
      },
      rules: {
        purchaseFlowType: [{ required: true, message: '请选择销售类型', trigger: 'change' }],
        purchaseType: [{ required: true, message: '请选择直采分类', trigger: 'change' }]
      },
      detailData: null //  详情数据
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      _this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(_this.form))
          formData.salesBillSn = _this.detailData.salesBillSn
          _this.spinning = true
          allCreatePurchase(formData).then(res => {
            if (res.status == '200') {
              _this.isShow = false
              _this.$message.success(res.message)
              _this.$emit('ok')
            }
            _this.spinning = false
          })
        } else {
          return false
        }
      })
    },
    //  获取详情
    setDetail (data) {
      this.detailData = data
    },
    // 重置表单
    resetForm () {
      this.detailData = null
      this.$refs.ruleForm.resetFields()
      this.form = {
        purchaseFlowType: undefined,
        purchaseType: 'TIRE',
        remark: ''
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetForm()
      }
    }
  }
}
</script>

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