<template>
  <el-container>
    <sidebar></sidebar>
    <el-container>
      <el-header style="text-align: right;height: 80px;line-height: 80px;">
        <span @click="showMerchant" v-if="$store.state.userInfo.memberType === 1" class="header-button">成为商户</span>
        <span @click="$router.push({
          path: '/shopInfo/shopAccount'
        })" class="header-button">充值中心</span>
        <span @click="goPrinter" class="header-button">打印设置</span>
        <el-dropdown trigger="click">
          <span class="el-dropdown-link">
            <span style="cursor: pointer;">{{ userInfo.nickname }}</span>
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="loginOut">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-main style="background:#F1F2F5;">
        <router-view></router-view>
      </el-main>
    </el-container>
    <merchant-add ref="merchantAdd" :products="productList"></merchant-add>
  </el-container>
</template>

<script>
import sidebar from "../common/sider.vue";
import { mapState } from "vuex";
import bus from "../common/bus.js";
import merchantAdd from "../components/merchantAdd.vue";
import { getProductList } from "../api/shop.js";
export default {
  data() {
    return {
      audio: null,
      productList: [],
    };
  },
  computed: {
    ...mapState(["userInfo"]),
  },
  components: {
    sidebar,
    merchantAdd,
  },
  created() {
    bus.$emit("openGetSoundMsg");
    this.getProductList();
    let memberType = this.$store.state.userInfo.memberType;
    if (memberType === 1) {
      this.$confirm("您当前还不是商户，请先申请成为商户！", "提示", {
        confirmButtonText: "成为商户",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.$refs.merchantAdd.init(0);
        })
        .catch(() => {});
    }
  },

  methods: {
    showMerchant() {
      this.$refs.merchantAdd.init(0);
    },
    getProductList() {
      getProductList().then((res) => {
        if (res.code === 200) {
          this.productList = res.data;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    goPrinter() {
      this.$router.push({
        path: "/shopInfo/shopInfos",
        params: {
          tabNum: 3,
        },
      });
      if (this.$route.name === "shopInfos") {
        bus.$emit("goPrinter");
      }
    },
    loginOut() {
      this.$confirm("此操作将退出当前登录用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        localStorage.clear();
        bus.$emit("closeGetSoundMsg");
        this.$router.push({
          path: "/login",
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped="scoped">
.el-header {
  position: relative;
  width: 100%;
  height: 80px;
}

.el-main {
  position: absolute;
  left: 254px;
  right: 0;
  top: 80px;
  bottom: 0;
  // overflow-y: hidden;
  background-color: #f1f2f5;
  padding: 10px;
  min-width: 550px;
}

.header-button {
  font-size: 16px;
  font-family: PingFang SC;
  font-weight: 400;
  color: #777777;
  cursor: pointer;
  margin-right: 40px;
}

.header-button {
  font-size: 16px;
  font-family: PingFang SC;
  font-weight: 400;
  color: #777777;
  cursor: pointer;
  margin-right: 40px;
}

.home-content {
  display: flex;

  .side-box {
    // width: 200px;
    position: relative;
  }

  .content {
    flex: 1;
    flex-shrink: 1;
  }

  .title {
    color: #0074d9;
  }
}
</style>
