<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 v-if="takeOutList.length" 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>
    <el-empty v-if="!takeOutList.length && !loading" description="暂无绑定外卖平台！"></el-empty>
    <el-dialog title="绑定美团外卖" :visible.sync="centerDialogVisible" width="520px" center>
      <div class="bind">
        <div v-if="!showMT" @click="showMT = true" class="more">更多授权方式</div>
        <el-radio-group class="choose" fill="#FC7200" v-model="mtBindType">
          <el-radio :label="1">直接绑定</el-radio>
          <span class="tip tip1">绑定后美团外卖订单将自动推送给本台</span>
          <el-radio v-if="showMT" :label="2">无冲突模式绑定</el-radio>
          <span v-if="showMT" class="tip">已绑定其他第三方平台，请使用无冲突模式</span>
        </el-radio-group>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="centerDialogVisible = false">取 消</el-button>
        <el-button class="btn" size="small" type="primary" @click="bindMT">确 定</el-button>
      </span>
    </el-dialog>
  </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: false,
      centerDialogVisible: false,
      showMT: false,
      mtBindType: 1,
      waimaiId: "",
    };
  },
  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() {
    let memberType = this.$store.state.userInfo.memberType;
    if (memberType !== 1) {
      this.getShopList();
    }
  },
  methods: {
    getShopList() {
      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;
      });
    },
    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) {
      this.waimaiId = v.waimaiId;
      if (v.waimaiId === 6) {
        this.showMT = false;
        this.mtBindType = 1;
        this.centerDialogVisible = true;
      } else {
        this.bind();
      }
    },
    bindMT() {
      this.centerDialogVisible = false;
      this.bind();
    },
    bind() {
      let params = {
        shopId: this.shopList[this.curIdx].id,
        waimaiId: this.waimaiId,
        isNew: 1,
        mtBindType: this.mtBindType,
      };
      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",
        center: true,
      })
        .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;
          }
        }
      }
    }
  }
  .btn {
    background-color: #fc7200;
    border: #fc7200;
  }
  .bind {
    display: flex;
    flex-direction: column;
    .more {
      text-align: right;
      color: #175199;
      cursor: pointer;
      font-size: 12px;
    }
    .choose {
      display: flex;
      flex-direction: column;
      /deep/ .el-radio__label {
        font-size: 14px;
        color: #222222;
        font-weight: bold;
      }
      /deep/ .el-radio__input.is-checked .el-radio__inner {
        border-color: #fc7200;
        background: #fc7200;
      }
      .tip {
        color: #9ea7b7;
        margin-left: 20px;
        font-size: 12px;
      }
      .tip1 {
        margin: 5px 0 33px 20px;
      }
    }
  }
}
</style>
