<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">
          {{ selectObj.selectAll?selectObj.totalCount:selectObj.multipleList.length }} </span>位客户
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="133px">
        <el-form-item label="协管门店：" prop="fromSubStoreIds">
          <el-form-item prop="subOptType" style="margin-bottom:12px;">
            <el-radio-group v-model="form.subOptType">
              <el-radio :label="1">增加</el-radio>
              <el-radio :label="2">减少</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="fromSubStoreIds">
            <el-select v-model="form.fromSubStoreIds" filterable remote multiple
              class="fromSubStoreIds-select" style="width:384px;" :remote-method="getSubStoreList"
              placeholder="请输入请输入门店名称/门店code" v-select-loadmore="getOnlineStore"
              @visible-change="(flag) => flag ? getSubStoreList() : ''">
              <el-option v-for="item in subStoreList" :key="item.storeId" :label="item.storeName"
                :value="item.storeId"></el-option>
              <el-option class="loadmore-loading" v-if="loading">
                <i class="loading-icon" v-loading="true"></i> 加载中...
              </el-option>
            </el-select>
          </el-form-item>
        </el-form-item>
        <el-form-item label="原因备注：" prop="remark">
          <el-input type="text" maxlength="20" show-word-limit v-model="form.remark"
            style="width: 386px;"></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 { mapState } from "vuex";
import { checkFalse, checkStatus, checkSuccess } from "../../../../static/js/checkStatus";
export default {
  name: "substoreDialog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    ajaxObj: {
      type: Object,
      default: () => {
        return {
          pageName: ''
        };
      }
    },
    selectObj: {
      type: Object,
      default: () => {
        return {
          totalCount: 0,
          selectAll: false,
          multipleList: []
        };
      }
    },
  },
  data() {
    return {
      form: {
        remark: "",
        optType: 2, // 1服务门店（主门店），2协管门店
        subOptType: 1, // 协管门店时必填 1增加，2删除
        fromSubStoreIds: [], // 批量处理协管门店时使用，存放门店id，以","分割
      },
      rules: {
        fromSubStoreIds: [
          { required: true, message: "请选择要操作的协管门店", trigger: "change" }
        ],
        remark: [
          { required: true, message: "请输入原因备注", trigger: "change" }
        ]
      },
      pageParam: {
        loading: false,
        searchName: '',
        currentPage: 1,
        pageSize: 20,
        totalPage: 1
      },
      subStoreList: [],
      btnLoading: false,
    };
  },
  computed: {
    ...mapState([ "showEditClique" ])
  },
  methods: {
    cancel() {
      this.$refs[ 'form' ].resetFields();
      this.$emit('update:dialogVisible', false);
    },
    getOnlineStore() {
      const { searchName, currentPage, loading, totalPage } = this.pageParam;
      if (loading || currentPage >= totalPage) return;
      this.pageParam.loading = true;
      doFetchqs(url.getOnlineStore, {
        currentPage: currentPage + 1,
        pageSize: 20,
        searchName: searchName
      }).then(res => {
        const { errorCode, message, result } = res.data || {};
        if (errorCode == 0) {
          this.subStoreList = this.subStoreList.concat(result.list || []);
          this.pageParam.totalPage = result.page.totalPage || 1;
          this.pageParam.currentPage = currentPage + 1;
        } else {
          checkFalse(message);
        }
      }).finally(() => this.pageParam.loading = false);
    },
    getSubStoreList(query) {
      this.pageParam.searchName = query;
      this.pageParam.currentPage = 0;
      this.subStoreList = [];
      this.getOnlineStore();
    },
    submit() {
      this.$refs[ 'form' ].validate(valid => {
        if (valid) {
          const { selectAll, multipleList } = this.selectObj;
          const { memberSearchStr, pageSize, phoneNameCard, pageName } = this.ajaxObj;
          this.btnLoading = true;
          let memberIdsArr = [];
          multipleList.forEach(item => {
            memberIdsArr.push(item.memberId);
          });
          doFetch(url.updateStore, Object.assign({}, this.form, {
            fromSubStoreIds: this.form.fromSubStoreIds.join(","),
            memberIds: selectAll == true ? "-1" : memberIdsArr.join(","),
            isCurrent: selectAll == true ? 2 : multipleList.length == pageSize ? 1 : 0,
            memberSearchParamStr: selectAll == true ? (memberSearchStr ? 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('refresh');
              } else {
                checkFalse();
              }
            })
            .catch(err => {
              checkStatus(err);
            }).finally(_=>this.btnLoading = false);
        } 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>
<style lang="less">
// 去除dailog-footer上边框
.customer-dailog .el-dialog__footer {
  padding-top: 0;
  padding-bottom: 20px;
  border-top: none !important;
}
.customer-dailog .el-dialog__body {
  padding-bottom: 0;
}
</style>