<template>
  <div>
    <el-dialog title="批量调整积分" custom-class="customer-dailog" :visible.sync="dialogVisible"
      :close-on-click-modal="false" @close="cancel" width="600px">
      <div class="checkedCustomers">当前选中<span class="num"> {{ multipleList.length }} </span>位客户
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="133px">
        <el-form-item label="调整积分：" prop="editType">
          <el-radio-group v-model="form.editType">
            <el-radio :label=1>增加积分</el-radio>
            <el-radio :label=2>扣除积分</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="调整额度：" prop="accumulatPoints">
          <el-input-number v-model="form.accumulatPoints" style="width: 115px;"
            controls-position="right" :min="1" :max="999999"></el-input-number>
        </el-form-item>
        <el-form-item label="原始成本主体：" prop="settlementStoreId"
          v-if="integralFlag == 'gic' && form.editType == 1">
          <el-select prop="settlementStoreId" style="width: 400px;" v-model="form.settlementStoreId"
            filterable remote reserve-keyword placeholder="请输入门店名称/门店code"
            :remote-method="getStoreCodeName" :loading="loading">
            <el-option v-for="item in storeList" :key="item.storeId" :label="item.storeName"
              :value="item.storeId"></el-option>
          </el-select>
          <p class="sub-tip">注：除输入选择具体门店外，还可输入总部，记总部名下</p>
        </el-form-item>
        <el-form-item label="原因备注：" prop="remark">
          <el-input type="text" maxlength="20" show-word-limit v-model="form.remark"
            style="width: 400px;"></el-input>
        </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, doFetchqs } from "../../axios/api";
import url from "../../axios/url";
import { checkFalse, checkStatus, checkSuccess } from "../../../../static/js/checkStatus";
export default {
  name: "integralDailog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    multipleList: {
      type: Array,
      default: () => {
        return [];
      }
    },
    integralFlag: {
      type: String,
      default: ''
    },
    pageSize: {
      type: Number,
      default: ''
    },
    memberSearchStr: {
      type: Number,
      default: ''
    },
    phoneNameCard: {
      type: String,
      default: ''
    },
    pageName: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      form: {
        editType: 1,
        accumulatPoints: 0,
        remark: "",
        settlementStoreId: ""
      },
      rules: {
        editType: [
          { required: true, message: "请选择调整方式", trigger: "change" }
        ],
        accumulatPoints: [
          { required: true, message: "请选择调整额度", trigger: "change" }
        ],
        remark: [
          { required: true, message: "请输入原因备注", trigger: "change" }
        ],
        settlementStoreId: [
          { required: true, message: "请填写原始成本主体", trigger: "change" }
        ]
      },
      btnLoading: false,
      loading: false,
      storeList: []
    };
  },
  methods: {
    cancel() {
      this.$refs[ 'form' ].resetFields();
      this.$emit('update:dialogVisible', false);
    },
    getStoreCodeName(val) {
      this.loading = true;
      doFetchqs(url.storeCodeName, {
        searchParam: val,
        flag: 1
      })
        .then(res => {
          if (res.data.errorCode === 0) {
            this.storeList = res.data.result;
          } else {
            checkFalse(res.data.message);
          }
        })
        .catch(err => {
          checkStatus(err);
        }).finally(_ => this.loading = false);
    },
    submit() {
      this.$refs[ 'form' ].validate(valid => {
        if (valid) {
          this.btnLoading = true;
          let memberIdsArr = [];
          this.multipleList.forEach(item => {
            memberIdsArr.push(item.memberId);
          });
          doFetch(url.updateIntegrals, Object.assign({}, this.form, {
            memberIds: this.selectAll == true ? "-1" : memberIdsArr.join(","),
            isCurrent: this.selectAll == true ? 2 : this.multipleList.length == this.pageSize ? 1 : 0,
            memberSearchParamStr: this.selectAll == true ? (this.memberSearchStr ? this.memberSearchStr : "-1") : "",
            phoneNameCard: this.phoneNameCard,
            pageName: this.pageName
          }))
            .then(res => {
              if (res.data.errorCode === 0) {
                setTimeout(() => {
                  this.btnLoading = false;
                  checkSuccess();
                  this.$refs[ 'form' ].resetFields();
                  this.$emit('update:dialogVisible', false);
                  this.$emit('refresh');
                }, 4500);
              } else {
                checkFalse();
                this.btnLoading = false;
              }
            })
            .catch(err => {
              this.btnLoading = false;
              checkStatus(err);
            });
        } else {
          return false;
        }
      });
    },
  }
};

</script>
<style lang="less" scoped>
.customer-dailog {
  .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>