<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 label="服务门店：" prop="mainStoreId">
          <el-select style="width: 382px" v-model="form.mainStoreId" filterable remote
            reserve-keyword placeholder="请输入门店名称/门店code" :remote-method="getMainStoreCodeName"
            popper-class="option-wrap" :loading="loading">
            <el-option v-for="item in mainstoreList" :key="item.storeId" :label="item.storeName"
              :value="item.storeId">
              <p class="name">
                {{ item.storeName }}
              </p>
              <p class="code">
                {{ item.storeCode }}
              </p>
            </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"
            placeholder="请输入内容" style="width: 382px;" />
        </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: 'MainstoreDailog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    ajaxObj: {
      type: Object,
      default: () => {
        return {
          pageName: '',
        };
      },
    },
    selectObj: {
      type: Object,
      default: () => {
        return {
          totalCount: 0,
          selectAll: false,
          multipleList: [],
        };
      },
    },
  },
  data() {
    return {
      visible: false,
      form: {
        remark: '',
        optType: 1, // 1服务门店（主门店），2协管门店
        mainOptType: 4, // 服务门店类型 1替换为最新协管的门店 2替换为最新协管的自营门店 3 替换为最新协管的自营/联营门店 4自定义服务门店
        mainStoreId: '',
      },
      rules: {
        mainStoreId: [
          { required: true, message: '请选择服务门店', trigger: 'change' },
        ],
        remark: [
          { required: true, message: '请输入原因备注', trigger: 'change' },
        ],
      },
      mainstoreList: [],
      loading: false,
      btnLoading: false,
    };
  },
  computed: {
    ...mapState([ 'showEditClique' ]),
  },
  watch: {
    dialogVisible(n, o) {
      this.visible = n;
    },
  },
  methods: {
    cancel() {
      this.$refs.form.resetFields();
      this.$emit('update:dialogVisible', false);
    },
    getMainStoreCodeName(val) {
      this.loading = true;
      doFetchqs(url.storeCodeName, {
        searchParam: val,
        flag: 1,
      })
        .then(res => {
          if (res.data.errorCode === 0) {
            this.mainstoreList = res.data.result;
          } else {
            checkFalse(res.data.message);
          }
        })
        .catch(err => {
          checkStatus(err);
        }).finally(_ => (this.loading = 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.updateStore, 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('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">
.el-select-dropdown.option-wrap .el-select-dropdown__item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 57px;
}
.option-wrap {
  color: #303133;
  .name {
    margin: 0 0 3px;
    font-size: 14px;
    line-height: 20px;
  }
  .code {
    margin: 0;
    font-size: 12px;
    line-height: 17px;
  }
}
</style>