<template>
  <div>
    <el-dialog
      title="批量修改等级"
      custom-class="customer-dialog"
      :visible.sync="visible"
      :close-on-click-modal="false"
      @close="cancel"
      width="600px"
    >
      <div class="checkedCustomers">
        当前选中<span class="num">
          {{ selectObj.selectAll?selectObj.totalCount:selectObj.multipleList.length }} </span>位客户
      </div>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="133px"
      >
        <el-form-item v-if="showEditClique" label="等级类型：" prop="gradeType">
          <el-radio v-model="form.isClique" :label="1" @change="form.gradeId = ''">
            集团等级
          </el-radio>
          <el-radio v-model="form.isClique" :label="0" @change="form.gradeId = ''">
            商户等级
          </el-radio>
        </el-form-item>
        <el-form-item label="修改后等级：" prop="gradeId">
          <el-select v-model="form.gradeId" placeholder="请选择" style="width: 400px;">
            <el-option
              v-for="item in gradeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </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: 400px;"
          />
        </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 { mapState } from 'vuex';
import { checkFalse, checkStatus, checkSuccess } from '../../../../static/js/checkStatus';
export default {
  name: 'GradeDailog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    gradeList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    ajaxObj: {
      type: Object,
      default: () => {
        return {
          pageName: '',
        };
      },
    },
    selectObj: {
      type: Object,
      default: () => {
        return {
          visible: false,
          totalCount: 0,
          selectAll: false,
          multipleList: [],
        };
      },
    },
  },
  data () {
    return {
      form: {
        gradeId: '',
        remark: '',
        isClique: 0,
      },
      rules: {
        gradeId: [{ required: true, message: '请选择等级', trigger: 'change' }],
        remark: [
          { required: true, message: '请输入原因备注', trigger: 'change' },
        ],
      },
      btnLoading: false,
    };
  },
  computed: {
    ...mapState(['showEditClique']),
  },
  watch: {
    dialogVisible (n, o) {
      this.visible = n;
      if (n) {
        if (this.showEditClique) {
          this.form.isClique = 1;
        }
      }
    },
  },
  methods: {
    cancel () {
      this.$refs.form.resetFields();
      this.$emit('update:dialogVisible', false);
    },
    submit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { selectAll, multipleList } = this.selectObj;
          const { memberSearchStr, pageSize, phoneNameCard, pageName } = this.ajaxObj;
          this.btnLoading = true;
          const memberIdsArr = [];
          multipleList.forEach(item => {
            memberIdsArr.push(item.memberId);
          });
          doFetch(url.updateGrade, Object.assign({}, this.form, {
            memberIds: selectAll == true ? '-1' : memberIdsArr.join(','),
            isCurrent: selectAll == true ? 2 : multipleList.length == pageSize ? 1 : 0,
            memberSearchParamStr: selectAll == true ? (memberSearchStr || '-1') : '',
            phoneNameCard: phoneNameCard,
            pageName: pageName,
          }))
            .then(res => {
              if (res.data.errorCode === 0) {
                checkSuccess();
                this.$refs.form.resetFields();
                this.$emit('update:dialogVisible', false);
                this.$emit('successImport', res.data.result);
                this.$emit('refresh');
              } else {
                checkFalse();
              }
            })
            .catch(err => {
              checkStatus(err);
            }).finally(_ => (this.btnLoading = false));
        } else {
          return false;
        }
      });
    },
  },
};

</script>
<style lang="less" scoped>
.customer-dialog {
  .checkedCustomers {
    margin-left: 133px;
    margin-bottom: 20px;
    font-size: 14px;
    font-weight: 400;
    color: #909399;
    line-height: 20px;
    .num {
      color: #303133;
    }
  }
  .sub-tip {
    margin: 4px 0 -5px;
    font-size: 12px;
    font-weight: 400;
    color: #909399;
    line-height: 17px;
  }
}
</style>
<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>
