<template>
  <a-modal
    centered
    class="updateDealerLevel-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="updateDealerLevel-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item label="商户名称">
            {{ form.dealerName }}
          </a-form-model-item>
          <a-form-model-item label="商户级别" prop="dealerLevel">
            <v-select code="DEALER_LEVEL" id="updateDealerLevel-dealerLevel" v-model="form.dealerLevel" allowClear placeholder="请选择商户级别"></v-select>
          </a-form-model-item>
          <div style="display: flex;justify-content: center;padding: 30px 0;">
            <a-button id="updateDealerLevel-cancel-btn" @click="cancel">取消</a-button>
            <a-button id="updateDealerLevel-submit-btn" style="margin-left: 15px;" type="primary" @click="onSubmit">确定</a-button>
          </div>
        </a-form-model>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect } from '@/components'
// 接口
import { updateDealerLevel } from '@/api/dealer'
export default {
  name: 'UpdateDealerLevel',
  mixins: [commonMixin],
  components: { VSelect },
  props: {
    openUdModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openUdModal, //  是否打开弹框
      spinning: false,
      title: '级别设置', // 标题
      labelCol: { span: 5 }, // form表单布局
      wrapperCol: { span: 16 },
      // 提交数据
      form: {
        dealerName: '', // 商户名称
        dealerLevel: undefined// 商户级别
      },
      // 表单验证规则
      rules: {
        dealerLevel: [
          { required: true, message: '请选择商户级别', trigger: 'change' }
        ]
      },
      dealerLevelBak: undefined, // 商户级别回显数据
      isProvincialCustomers: ''// 省仓客户
    }
  },
  methods: {
    // 设置数据
    setData (row) {
      this.form.dealerSn = row.dealerSn
      this.form.dealerName = row.dealerName
      this.form.dealerLevel = row.dealerLevel
      this.dealerLevelBak = row.dealerLevel
      this.isProvincialCustomers = row.serviceCenterSn ? '1' : '0'// 是否是省仓客户
    },
    // 提交判断必填项
    onSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.submitForm()
        } else {
          return false
        }
      })
    },
    // 提交
    submitForm () {
      const _this = this
      const params = JSON.parse(JSON.stringify(_this.form))
      _this.spinning = true
      updateDealerLevel(params).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$emit('ok')
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    // 取消
    cancel () {
      this.$emit('close')
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openUdModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style lang="less">
  .merchantInfoManagement-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
  }
</style>
