<template>
  <div>
    <el-dialog title="修改协管门店" custom-class="customer-dailog" :visible.sync="dialogVisible"
      :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="fromSubStoreIds">
          <el-select v-model="form.storeIds" 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 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 { doFetchqs, doFetch } from "../../axios/api";
import url from "../../axios/url";
import { mapState } from "vuex";
import { checkFalse, checkStatus, checkSuccess } from "../../../../static/js/checkStatus";
export default {
  name: "customermainstoreDialog",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    obj: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      form: {
        memberId: '',
        subStoreIdsBak: '', // 修改前的协管门店id
        storeIds: [],
        remark: '',
        optType: 2 // 1服务门店，2协管门店
      },
      rules: {
        subStoreId: [
          { 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) {
          this.btnLoading = true;
          doFetch(url.updateMemberStore, Object.assign({}, this.form, { storeIds: this.form.storeIds.join(',') }))
            .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;
        }
      });
    },
  },
  watch: {
    dialogVisible(n, o) {
      if (n) {
        const { shopNames, memberId } = this.obj;
        let storeIds = shopNames.map(item => item.storeId);
        this.subStoreList=[].concat(shopNames)
        this.form = Object.assign({}, this.form, { memberId, storeIds, subStoreIdsBak: storeIds.join(',') });
      }
    }
  }
};

</script>
<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>