<template>
  <el-container>
    <sidebar></sidebar>
    <el-container>
      <el-header>
        <span @click="showMerchant" v-if="$store.state.userInfo.memberType === 1" class="header-button">成为商户</span>
        <div class="account" @click="goAccount">
          <img src="../../static/image/head-account2.png" alt="">
          <div class="num">￥{{$store.state.userInfo.amount}}</div>
        </div>
        <div class="print" @click="goPrinter">
          <img src="../../static/image/head-print2.png" alt="">
          <div class="num con">打印设置</div>
        </div>
        <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: {
    goAccount() {
      this.$router.push({
        name: "wallet",
      });
    },
    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({
        name: "print",
      });
    },
    loginOut() {
      this.$confirm("此操作将退出当前登录用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // localStorage.clear();
        this.$store.commit("setFirstLogin", true);
        bus.$emit("closeGetSoundMsg");
        this.$router.push({
          path: "/login",
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped="scoped">
/deep/.el-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  position: relative;
  width: 100%;
  height: 80px !important;
}

.el-main {
  position: absolute;
  left: 254px;
  right: 0;
  top: 80px;
  bottom: 0;
  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;
}

.account,
.print {
  display: flex;
  align-items: center;
  margin-right: 20px;
  cursor: pointer;
  img {
    width: 20px;
    height: 20px;
    margin-right: 5px;
  }
  .num {
    line-height: 25px;
    font-size: 16px;
    font-weight: 500;
    color: #fc7200;
  }
  .con {
    font-size: 15px;
    color: #333333;
  }
}
.home-content {
  display: flex;

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

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

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