<template>
  <div>
    <el-dialog
      title="修改等级"
      custom-class="customer-dialog"
      :visible.sync="visible"
      :close-on-click-modal="false"
      @close="cancel"
      width="600px"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="133px"
      >
        <el-form-item label="修改后等级：" prop="gradeId">
          <el-select v-model="form.gradeId" placeholder="请选择" style="width: 386px;">
            <template v-if="isClique">
              <el-option
                v-for="item in cliqueGradeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </template>
            <template v-else>
              <el-option
                v-for="item in gradeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="原因备注：" prop="remark">
          <el-input
            type="text"
            maxlength="20"
            show-word-limit
            v-model="form.remark"
            placeholder="请输入内容"
            style="width: 386px;"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { doFetch } from '../../axios/api';
import url from '../../axios/url';
import { checkFalse, checkStatus, checkSuccess } from '../../../../static/js/checkStatus';
export default {
  name: 'CustomergradeDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    isClique: {
      type: Boolean,
      default: false,
    },
    gradeId: {
      type: String,
      default: '',
    },
    cliqueGradeId: {
      type: String,
      default: '',
    },
    gradeList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    cliqueGradeList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    memberId: {
      type: String,
      default: '',
    },
  },
  data () {
    return {
      visible: false,
      form: {
        memberId: '',
        gradeId: '',
        remark: '',
      },
      rules: {
        gradeId: [
          { required: true, message: '请选择修改后等级', trigger: 'change' },
        ],
        remark: [
          { required: true, message: '请输入原因备注', trigger: 'change' },
        ],
      },
      btnLoading: false,
    };
  },
  watch: {
    dialogVisible (n, o) {
      this.visible = n;
      if (n) {
        const gradeId = this.isClique ? this.cliqueGradeId : this.gradeId;
        this.form = Object.assign(this.form, { gradeId, memberId: this.memberId });
      }
    },
  },
  methods: {
    cancel () {
      this.$refs.form.resetFields();
      this.$emit('update:dialogVisible', false);
    },
    submit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.btnLoading = true;
          const param = {
            ...this.form,
            isClique: Number(this.isClique),
          };
          doFetch(url.memberGrade, Object.assign({}, param))
            .then(res => {
              if (res.data.errorCode === 0) {
                setTimeout(() => {
                  this.btnLoading = false;
                  checkSuccess();
                  this.$refs.form.resetFields();
                  this.$emit('update:dialogVisible', false);
                  this.$emit('refresh');
                }, 2000);
              } else {
                checkFalse();
                this.btnLoading = false;
              }
            })
            .catch(err => {
              this.btnLoading = false;
              checkStatus(err);
            });
        } else {
          return false;
        }
      });
    },
  },
};

</script>
<style lang="less">
// 去除dailog-footer上边框
.customer-dialog .el-dialog__footer {
  padding-top: 0;
  padding-bottom: 20px;
  border-top: none !important;
}
.customer-dialog .el-dialog__body {
  padding-bottom: 0;
}
</style>
