<template>
  <div class="box-center">
    <div class="head">
      <div>AI销讲助手管理系统</div>
      <div class="callback" @click="goloign" style="cursor: pointer">
        {{ backFlag ? "返回" : "退出" }}
      </div>
    </div>
    <div class="hid">选择后台</div>
    <div class="content">
      <!-- 质控后台 -->
      <div class="tab" @click="goindex(0)" v-if="tabFlag1">
        <div><img src="/img/checktab1.png" alt="" /></div>
        <div class="text">AI销讲助手后台</div>
      </div>
      <div class="tab" @click="goindex(1)" v-if="tabFlag2">
        <div><img src="/img/checktab2.png" alt="" /></div>
        <div class="text">代理商后台</div>
      </div>
      <div class="tab" @click="goindex(2)" v-if="tabFlag3">
        <div><img src="/img/checktab3.png" alt="" /></div>
        <div class="text">公司后台</div>
      </div>
      <div class="tab" @click="goindex(3)" v-if="tabFlag4">
        <div><img src="/img/checktab4.png" alt="" /></div>
        <div class="text">项目后台</div>
      </div>
    </div>
  </div>
</template>

<script>
import { getStore, setStore } from "@/util/store";
export default {
  data() {
    return {
      // 质控管家标志
      tabFlag1: false,
      // 代理商标志
      tabFlag2: false,
      // 公司后台标志
      tabFlag3: false,
      // 项目后台标志
      tabFlag4: false,
      backFlag: false,
      info: {},
    };
  },
  computed: {},
  mounted() {
    // 获取用户信息
    this.info = getStore({ name: "userInfo" });
    // console.log(this.$route.query.backFlag);
    if (this.$route.query.backFlag) {
      this.backFlag = this.$route.query.backFlag;
    }
    this.getTab();
  },
  methods: {
    // 获取可选权限对
    getTab() {
      this.$api.api.getTab().then((res) => {
        // console.log(res)
        // 循环数组，给定各种标志
        this.tabFlag1 = false;
        this.tabFlag2 = false;
        this.tabFlag3 = false;
        this.tabFlag4 = false;
        // return
        res.data.map((item) => {
          if (item.orgType == 0) {
            this.tabFlag1 = true;
          }
          if (item.orgType == 1) {
            this.tabFlag2 = true;
          }
          if (item.orgType == 2) {
            this.tabFlag3 = true;
          }
          if (item.orgType == 3) {
            this.tabFlag4 = true;
          }
        });
      });
    },
    goloign() {
      // 对此进行判断，获取this.$route的参数，如果没有则退出登陆，有的话就返回之前的页面
      if (this.backFlag) {
        this.$router.back();
        localStorage.setItem("allClose", false);
      } else {
        this.$confirm("是否退出系统, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          this.$store.dispatch("LogOut").then(() => {
            this.$router.push({ path: "/login" });
          });
        });
      }
    },
    // 获取代理商
    getAgentList() {
      this.$api.api
        .zkagentPage({
          current: 1,
          pageSize: 10,
        })
        .then((res) => {
          // console.log(res);
          // this.list = res.data.records;
          // this.total = res.data.total;
          if (res.data.total == 0) {
            this.$message.warning("您当前并未绑定项目");
            return;
          } else if (res.data.total > 1) {
            // 公司后台
            this.$router.push({ path: "/chose", query: { flag: 1 } });
          } else {
            if (res.data.records[0].lockFlag != 0) {
              this.$message.warning("您已经被禁用");
              return;
            }
            this.$router.push({ path: "/wel" });
            localStorage.setItem("topName", res.data.records[0].agentName);
            localStorage.setItem("agentId", res.data.records[0].id);
            localStorage.setItem("orgCode", "");
            localStorage.setItem("houseId", "");
          }
        });
    },
    getCompanyList() {
      this.$api.api
        .zkorg({
          current: 1,
          size: 10,
          name: this.companyName,
          orgType: localStorage.getItem("orgType"),
        })
        .then((res) => {
          // console.log(res);
          // this.list = res.data.records;
          // this.total = res.data.total;
          if (res.data.total == 0) {
            this.$message.warning("您当前并未绑定公司");
            return;
          } else if (res.data.total > 1) {
            // 公司后台
            this.$router.push({ path: "/chose", query: { flag: 2 } });
          } else {
            if (res.data.records[0].lockFlag != 0) {
              this.$message.warning("您已经被禁用");
              return;
            }
            this.$router.push({ path: "/wel" });
            localStorage.setItem("topName", res.data.records[0].name);
            localStorage.setItem("orgCode", res.data.records[0].orgCode);
            localStorage.setItem("agentId", "");
            localStorage.setItem("houseId", "");
          }
        });
    },
    findByUserName() {
      this.$api.api
        .findByUserName({
          username: this.info.username,
        })
        .then((res) => {
          // console.log(res);
          this.info.selectHouseType = res.selectHouseType;
          // 获取项目
          this.zkhousePage();
        });
    },
    addLoginCount(houseId){
      this.$api.api.addLoginCount({
        houseId,
        serviceId: 'pig',
      }).then(res=>{
        console.log(res);
      })
    },
    zkhousePage() {
      let obj = {
        current: 1,
        size: 10,
        orgType: localStorage.getItem("orgType"),
      };
      if (this.info.selectHouseType == 1) {
        this.$api.api.zkhousePage(obj).then((res) => {
          // console.log(res)
          // this.list = res.data.records;
          // this.total = res.data.total;
          if (res.data.total == 0) {
            this.$message.warning("您当前并未绑定项目");
            return;
          } else if (res.data.total > 1) {
            // 公司后台
            this.$router.push({ path: "/chose", query: { flag: 3 } });
          } else {
            if (res.data.records[0].lockFlag != 0) {
              this.$message.warning("您已经被禁用");
              return;
            }
            this.$router.push({ path: "/wel" });
            localStorage.setItem("topName", res.data.records[0].propertyName);
            localStorage.setItem("orgCode", res.data.records[0].orgCode);
            localStorage.setItem("agentId", "");
            localStorage.setItem("houseId", res.data.records[0].id);
            this.addLoginCount(res.data.records[0].id)
          }
        });
      }
      if (this.info.selectHouseType == 2) {
        this.$api.api
          .findHouseByArea({
            id: null,
            ...obj,
          })
          .then((res) => {
            // console.log(res)
            // this.list = res.data.records;
            // this.total = res.data.total;
            if (res.data.total == 0) {
              this.$message.warning("您当前并未绑定项目");
              return;
            } else if (res.data.total > 1) {
              // 公司后台
              this.$router.push({ path: "/chose", query: { flag: 3 } });
            } else {
              if (res.data.records[0].lockFlag != 0) {
                this.$message.warning("您已经被禁用");
                return;
              }
              this.$router.push({ path: "/wel" });
              localStorage.setItem("topName", res.data.records[0].propertyName);
              localStorage.setItem("orgCode", res.data.records[0].orgCode);
              localStorage.setItem("houseId", res.data.records[0].id);
              localStorage.setItem("agentId", "");
              this.addLoginCount(res.data.records[0].id)
            }
          });
      }
    },
    goindex(idx) {
      localStorage.setItem("orgType", idx);
      if (idx == 0) {
        localStorage.setItem("topName", "");
        localStorage.setItem("orgCode", "");
        localStorage.setItem("houseId", "");
        localStorage.setItem("agentId", "");
        // 质控管家后台
        this.$router.push({ path: "/wel" });
      }
      if (idx == 1) {
        this.getAgentList();
        // 代理商
        // this.$router.push({ path: "/chose", query: { flag: 1 } });
      }
      if (idx == 2) {
        // 获取公司
        this.getCompanyList();
      }
      if (idx == 3) {
        this.findByUserName();
        // 项目后台
        // this.$router.push({ path: "/chose", query: { flag: 3 } });
      }
      localStorage.setItem("allClose", true);
      // this.$router.push({ path: "/chose" });
    },
  },
};
</script>

<style lang="scss" scoped >
.box-center {
  min-width: 1200px;
  height: 100vh;
  background: #ffffff;
}
.head {
  height: 64px;
  background: #409eff;
  display: flex;
  padding: 0 20%;
  box-sizing: border-box;
  justify-content: space-between;
  line-height: 64px;
  color: #fff;
  font-size: 24px;
  .callback {
    font-size: 14px;
  }
}
.hid {
  margin-top: 25px;
  height: 50px;
  background: #f8f8f8;
  border-radius: 4px;
  text-align: center;
  line-height: 50px;
  font-size: 18px;
  color: #32363d;
}
.content {
  margin-top: 30px;
  padding: 0 20%;
  display: flex;
  justify-content: space-around;
  .tab {
    width: 225px;
    height: 225px;
    background: #ffffff;
    border: 1px solid #e0e0e0;
    text-align: center;
    cursor: pointer;
    img {
      height: 88px;
      width: 88px;
      margin-top: 47px;
    }
    .text {
      font-size: 20px;
      margin-top: 30px;
      color: #333333;
      line-height: 20px;
    }
  }
}
</style>
