<template>
  <div class="take-out" v-loading="loading" element-loading-text="数据加载中">
    <div class="store-list">
      <div class="store-list-item" @click="curIdx = index" :class=" index === curIdx ? 'active' : '' " v-for="(item, index) in shopList" :key="index">{{ item.name }}</div>
    </div>
    <div class="take-out-list">
      <div class="item" v-for="(v,index) in takeOutList" :key="index">
        <div class="item-top">
          <div class="top-left">
            <img :src="v.logo" alt="" class="top-left-img">
            <div class="name">{{v.name}}</div>
          </div>
          <!-- <div class="top-right">
            <span>开启后自动接单</span>
            <el-switch />
          </div> -->
        </div>
        <div class="item-bottom">
          <div class="left">
            <div class="l-l">
              <img :src="v.pcLogo" class="l-l-img" />
            </div>
            <div class="take-out-name" v-if="v.bindStatus">
              <div>店铺名称：{{v.shopName}}</div>
              <div>店铺ID：{{v.thirdShopId}}</div>
              <div>配送门店：{{v.thirdShopName}}</div>
            </div>
          </div>
          <div class="right">
            <el-button v-if="!v.bindStatus" v-loading.fullscreen.lock="fullscreenLoading" element-loading-text="关闭窗口后再操作！" type="primary" size="small" class="right-btn" @click="goBind(v)">去绑定</el-button>
            <el-button v-else @click="release(v)" size="small">解&nbsp;&nbsp;&nbsp;绑</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getShopList,
  getBindTakeOutList,
  getTakeOutList,
  bindTakeOut,
  deleteTakeOut,
  releaseTakeOut,
  setTakeOut,
} from "../../api/shop";
export default {
  data() {
    return {
      shopList: [],
      takeOutList: [],
      curIdx: -1,
      jumpUrl: "",
      fullscreenLoading: false,
      loading: true,
    };
  },
  watch: {
    curIdx(newVal, oldVal) {
      this.getBindTakeOutList(newVal);
    },
    jumpUrl(newVal, oldVal) {
      if (this.jumpUrl) {
        this.fullscreenLoading = true;
        let newWindowWidth = 800,
          newWindowHeight = 500,
          left = (window.screen.width - newWindowWidth) / 2,
          top = (window.screen.height - newWindowHeight) / 2 - 100;
        let features = `width=${newWindowWidth},height=${newWindowHeight},left=${left},top=${top}`;
        let windowOpen = window.open(newVal, "name", features);
        let loop = window.setInterval(() => {
          console.log("windowOpen.closed", windowOpen.closed);
          if (windowOpen.closed) {
            this.jumpUrl = null;
            this.fullscreenLoading = false;
            window.clearInterval(loop);
            this.getBindTakeOutList(this.curIdx);
          }
        }, 1000);
      }
    },
  },
  created() {
    getShopList().then((res) => {
      this.loading = true;
      getShopList().then((res) => {
        if (res.code === 200) {
          this.shopList = res.data;
          this.curIdx = 0;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
        this.loading = false;
      });
    });
  },
  methods: {
    getBindTakeOutList(index) {
      getBindTakeOutList({ shopId: this.shopList[index].id }).then((res) => {
        if (res.code === 200) {
          this.takeOutList = res.data;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    goBind(v) {
      let params = {
        shopId: this.shopList[this.curIdx].id,
        waimaiId: v.waimaiId,
        isNew: 1,
      };
      bindTakeOut(params).then((res) => {
        if (res.code === 200) {
          this.jumpUrl = res.data;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    release(v) {
      this.$confirm("解绑后将无法同步店铺订单，确认要解绑吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          releaseTakeOut({ id: v.id }).then((res) => {
            if (res.code === 200) {
              if (res.data) {
                this.jumpUrl = res.data;
              } else {
                this.$message({
                  type: "success",
                  message: "成功解绑!",
                });
                this.getBindTakeOutList(this.curIdx);
              }
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消解绑！",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.take-out {
  min-height: 200px;
  .store-list {
    display: flex;
    flex-wrap: nowrap;
    width: 100%;
    white-space: nowrap;
    overflow-x: auto;
    padding-bottom: 10px;
    &-item {
      padding: 8px 27px;
      background-color: #fff;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #b1b1b1;
      box-sizing: border-box;
      border: 1px solid #eee;
      border-radius: 17px;
      cursor: pointer;
      margin-right: 10px;
      &.active {
        color: #fc7200;
      }
    }
  }
  .store-list::-webkit-scrollbar {
    height: 6px;
  }
  .store-list::-webkit-scrollbar-track {
    background-color: #eee;
    /*border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;*/
  }
  .store-list::-webkit-scrollbar-thumb {
    background-color: #999;
    border-radius: 6px;
    -webkit-border-radius: 6px;
    -moz-border-radius: 6px;
  }

  .take-out-list {
    .item {
      background-color: #fff;
      .item-top {
        display: flex;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 20px 18px;
        align-items: center;
        border-bottom: 1px solid #eee;
        .top-left {
          display: flex;
          align-items: center;
          .top-left-img {
            width: 30px;
            height: 30px;
            margin-right: 10px;
          }
          .name {
            font-size: 16px;
            font-family: PingFang SC;
            font-weight: bold;
            color: #ffa608;
          }
        }
        .top-right {
          display: flex;
          align-items: center;
          span {
            font-size: 14px;
            font-family: PingFang SC;
            font-weight: 400;
            color: #b1b1b1;
            margin-right: 12px;
          }
        }
      }
      .item-bottom {
        display: flex;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 25px 18px;
        align-items: center;
        .left {
          display: flex;
          align-items: center;
          .l-l {
            width: 160px;
            height: 50px;
            font-size: 0;
            .l-l-img {
              width: 100%;
              height: 100%;
            }
          }
          .take-out-name {
            font-size: 13px;
            font-family: PingFang SC;
            font-weight: 400;
            color: #333333;
            line-height: 20px;
            margin-left: 30px;
          }
        }
        .right {
          .right-btn {
            background-color: #fc7200;
            border: #fc7200;
          }
        }
      }
    }
  }
}
</style>
