<template>
  <a-modal
    centered
    class="merchantInfoManagement-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancle="isShow=false"
    :width="600">
    <div>
      <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>
    </div>
  </a-modal>
</template>

<script>
import { VSelect } from '@/components'
import { updateDealerLevel } from '@/api/dealer'
export default {
  name: 'UpdateDealerLevel',
  components: { VSelect },
  props: {
    openUdModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openUdModal, //  是否打开弹框
      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.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))
      updateDealerLevel(params).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$emit('ok')
        }
      })
    },
    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>
