<template>
  <a-modal
    centered
    class="merchantInfoManagement-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
          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="ucModal-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 type="primary" @click="onSubmit">确定</a-button>
            <a-button style="margin-left: 50px;" @click="cansel">取消</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 },
      wrapperCol: { span: 16 },
      other: '',
      form: {
        dealerName: '',
        dealerLevel: undefined
      },
      rules: {
        dealerLevel: [
          { required: true, message: '请选择商户级别', trigger: 'change' }
        ]
      },
      dealerLevelBak: undefined
    }
  },
  methods: {
    setData (row) {
      this.form.dealerSn = row.dealerSn
      this.form.dealerName = row.dealerName
      this.form.dealerLevel = row.dealerLevel
      this.dealerLevelBak = row.dealerLevel
    },
    onSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (_this.dealerLevelBak && _this.form.dealerLevel != _this.dealerLevelBak) {
            this.$confirm({
              title: '提示',
              content: '商户价格级别变更后，该商户关联的经销商关系绑定将同步解除，确定变更该商户级别吗？',
              centered: true,
              onOk () {
                _this.submitForm()
              }
            })
          } else {
            _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
        }
      })
    },
    cansel () {
      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>
