<template>
	<view class="business">
    <!--登录后内容-->
    <template v-if="is_sys == 1 || (is_sys != 1 && shopIsLogin)">
      <view class="business-header">
        <view class="headerbox" @click="changeTips" v-if="service">
          <image :src="service.merchant.mer_avatar" mode=""></image>
          <span class='font line1'>{{service.merchant.mer_name || '暂无店铺'}}</span>
          <template v-if="Object.values(storeList).length > 1">
            <text v-if="!downStatus" class="iconfont icon-xiala1 spin"></text>
            <text v-else class="iconfont icon-xiala1"></text>
          </template>
        </view>
      </view>
      <view v-if="Object.values(list).length > 0" class="business-content">
        <view @click="goNext(item)" class="listBox" v-for="(item,index) in list">
          <text :class="item.icon" class="businessIcon"></text>
          <view>{{item.title}}</view>
        </view>
      </view>
      <emptyPage v-else title="暂无任何操作权限~"></emptyPage>
      <shopList ref="shopList" @changeStoreClose="changeClose" @getService="getService" :is_sys='is_sys'></shopList>
    </template>
    <template v-else>
      <view class="login-content">
        <view class="login-form">
          <view class="title">账号密码</view>
          <view class="login-item">
            <text class="item-icon iconfont icon-shouji"></text>
            <input class="item-input" v-model="loginInfo.account" placeholder="请输入登录账号" name="text" />
          </view>
          <view class="login-item">
            <text class="item-icon iconfont icon-mimatubiao"></text>
            <input class="item-input" v-model="loginInfo.password" placeholder="请输入登录密码" name="password" />
          </view>
          <view class="login-btn" @click="clickLogin">登录</view>
        </view>
      </view>
    </template>
    <!--二维码-->
    <uni-popup ref="qrCodePopup" type="center">
      <view class="qr-code-content">
        <image class="image" :src="qrCode"></image>
        <view class="close-qr-code" @click="closeQrCode()">关闭</view>
      </view>
    </uni-popup>
	</view>
</template>

<script>
import emptyPage from '@/components/emptyPage.vue'
import shopList from '@/components/shopList';
import {promoteQrCodes,onlinePaymentQrCodes} from "@/api/service";
import { getSiteQrCode } from '@/api/exchange.js';
import {mapGetters} from "vuex";
import {storeLogin} from "@/api/store";

export default {
  name: 'business',
  components: {
    shopList,
    emptyPage
  },
  data() {
    return {
      is_sys: '',
      downStatus: false,
      service: null,
      storeList: {},
      // 二维码
      qrCode: '',
      // 酒道馆登录相关
      loginInfo:{
        account: '',
        password: ''
      },
    }
  },
  computed: {
    list(){
      if(!this.service) return [];
      const merId = this.service.mer_id;
      const list = [];
      let merchantInfo = this.service.merchant || {};
      if(this.service.status){
        list.push({
          type: 'customer',
          title: '客服记录',
          url: '/pages/chat/customer_list/index?type=1&mer_id=' + merId,
          icon: 'iconfont icon-kefujilu'
        });
      }
      if(this.service.is_verify){
        if(merchantInfo.merchant_type == 1){
          // 酒道馆核销用户优惠券
          list.push({
            title: '优惠券核销',
            url: '/pages/users/write_off/index',
            icon: 'iconfont icon-dingdanhexiao'
          });
        }else{
          // 普通商户核销用户订单
          list.push({
            title: '订单核销',
            url: '/pages/admin/order_cancellation/index?mer_id=' + merId,
            icon: 'iconfont icon-dingdanhexiao'
          });
        }
      }
      if(this.service.customer){
        list.push({
          title: '订单管理',
          url: '/pages/admin/order/index?mer_id=' + merId,
          icon: 'iconfont icon-dingdanguanli'
        });
      }
      if(this.service.is_goods){
        list.push({
          title: '商品管理',
          url: '/pages/product/list/index?mer_id=' + merId,
          icon: 'iconfont icon-shangjiaguanli'
        });
      }
      if(this.service.is_user){
        list.push({
          title: '顾客管理',
          url: '/pages/admin/custom/index?mer_id=' + merId,
          icon: 'iconfont icon-kehu-xuanzhong'
        });
      }
      if(this.service.staff_manage){
        list.push({
          title: '客服经理',
          url: '/pages/admin/business/staff?mer_id=' + merId,
          icon: 'iconfont icon-kehu-xuanzhong'
        });
      }
      if(this.service.qr_code_show){
        list.push({
          type: 'qr_code',
          title: '推广码',
          url: '',
          icon: 'iconfont icon-erweima1'
        });
      }
      if(this.service.online_payment){
        list.push({
          type: 'online_payment_qr_code',
          title: '买单码',
          url: '',
          icon: 'iconfont icon-erweima1'
        });
      }
      if(this.service.product_exchange){
        list.push({
          type: 'exchange_qr_code',
          title: '特色菜兑换',
          url: '',
          icon: 'iconfont icon-erweima1'
        });
      }
      if(this.service.purchase_permission){
        list.push({
          title: '进货',
          url: '/pages/supplier/stock/with_goods',
          icon: 'iconfont icon-shangjiadingdan'
        });
      }

      return list;
    },
    ...mapGetters(['isLogin', 'uid', 'userInfo', 'viewColor', 'shopIsLogin', 'shopMerId'])
  },
  onLoad: function(options) {
    this.is_sys = options.is_sys;
    // 设置页面标题
    uni.setNavigationBarTitle({
      title: this.is_sys == 1 ? '平台管理' : (this.is_sys == 2 ? '酒道馆管理' : '商家管理')
    })
    // 登录后操作
    if(this.is_sys == 1 || this.shopIsLogin){
      this.getStoreList({is_sys: this.is_sys, appoint_mer_id: this.shopMerId || ''});
    }
  },
  methods: {
    getStoreList: function(data) {
      this.$nextTick(() => {
        this.$refs.shopList.getStoreList(data)
      });
    },
    changeTips(data) {
      if(Object.values(this.storeList).length > 1){
        this.downStatus = !this.downStatus;
        this.$refs.shopList.isShowStore();
      }
    },
    changeClose() {
      this.downStatus = false;
    },
    goNext(item){
      if(item.type == 'online_payment_qr_code'){
        this.createOnlinePaymentQrCode();
      }
      else if(item.type == 'exchange_qr_code'){
        this.createExchangeQrCode();
      }
      else if(item.type == 'qr_code' && this.service.qr_code_show == 1){
        this.createQrCode();
      }
      else if(item.type == 'customer' && this.service.status == 0){
        return this.$util.Tips({
          title: '客服已离线，请开启客服状态！'
        });
      }
      else{
        uni.navigateTo({
          url: item.url,
        });
      }
    },
    getService: function(data) {
      this.storeList = this.$refs.shopList.storeList || {};

      this.service = data;
      if(data && data.merchant){
        uni.setNavigationBarTitle({
          title: data.merchant.mer_name
        })
      }else{
        uni.setNavigationBarTitle({
          title: (!data.mer_id) ? '平台管理' : '商家管理'
        })
      }
    },
    // 二维码 - 生成推广二维码
    createQrCode(){
      let _this = this;
      promoteQrCodes({ mer_id: _this.service.mer_id }).then(res => {
        if (res.status == 200) {
          _this.qrCode = res.data.qr_code || '';
          _this.$refs.qrCodePopup.open('center');
        }
      }).catch(err => {
        this.$util.Tips({
          title: err
        });
      });
    },
    // 二维码 - 生成买单二维码
    createOnlinePaymentQrCode(){
      let _this = this;
      onlinePaymentQrCodes({ mer_id: _this.service.mer_id }).then(res => {
        if (res.status == 200) {
          _this.qrCode = res.data.qr_code || '';
          _this.$refs.qrCodePopup.open('center');
        }
      }).catch(err => {
        this.$util.Tips({
          title: err
        });
      });
    },
    // 二维码 - 生成特色菜兑换
    createExchangeQrCode(){
      let _this = this;
      // 点击按钮触发弹框输入框的显示
      uni.showModal({
        title: '兑换商品金额', // 弹框标题
        content: '',
        editable: true,//是否显示输入框
        placeholderText: '请输入用户兑换商品的总金额',//输入框提示内容
        showCancel: true, // 是否显示取消按钮
        cancelText: '取消', // 取消按钮文字
        confirmText: '确定', // 确认按钮文字
        success: (res) => {
          if (res.confirm && Number(res.content) > 0) {
            let inputValue = Number(res.content);
            let params = {
              mer_id: this.service.mer_id,
              total_money: inputValue,
              quota_type: 2,
            };
            getSiteQrCode(params).then(res => {
              if (res.status == 200) {
                _this.qrCode = res.data.qr_code || ''
                _this.$refs.qrCodePopup.open('center');
              }
            }).catch(err => {
              this.$util.Tips({
                title: err
              });
            });
          } else if (res.cancel) {
            console.log('用户点击了取消');
          }
        }
      })
    },
    // 推广二维码 - 关闭二维码
    closeQrCode(){
      this.$refs.qrCodePopup.close();
    },
    // 商户登录 - 提交登录信息
    clickLogin(){
      let _this = this;
      let params = _this.loginInfo || {};
      storeLogin(params).then(res => {
        if (res.status == 200) {
          let data = res.data || {};
          // console.log("商户登录成功")
          // console.log(data)
          _this.$store.commit("SHOP_LOGIN", {
            'token': data.token,
            'time': data.exp,
            'shop_mer_id': data.mer_id,
          });
          this.getStoreList({is_sys: this.is_sys, appoint_mer_id: data.mer_id || ''});
        }
      }).catch(err => {
        this.$util.Tips({ title: err });
      });
    }




  }
}
</script>

<style scoped lang="scss">
.business {
  width: 100vw !important;
  min-height: 100vh !important;
  background: linear-gradient(180deg, #2291F8 0%, rgba(34, 145, 248, 0) 100%);

}
.businessIcon {
  color: #2291F8;
  font-size: 80rpx;
  display: inline-block;
  margin-bottom: 29rpx;
}
.business-header {
  //height: 305rpx;
  //position: fixed;
  padding-top: 20rpx;
  width: 100%;
  text-align: center;
  top: 0;
  left: 0;

  .headerbox {
    max-width: 360rpx;
    margin: 0 auto;
    position: relative;
    padding: 10rpx 0rpx 10rpx 0rpx;
    background-color: rgba(0, 0, 0, .25);
    border-radius: 30rpx;
    color: #FFFFFF;
    margin-top: 33rpx;

    .font {
      max-width: 260rpx;
      display: inline-block;
      margin-left: 10rpx;
      line-height: 28rpx;
    }

    image {
      width: 34rpx;
      height: 34rpx;
      position: relative;
      top: 4rpx;
    }

    .spin {
      display: inline-block;
      transform: rotate(180deg);
      font-size: 36rpx;
    }
  }
}
.business-content {
  width: 100%;
  padding: 0 calc((100% - (345rpx * 2)) / 3);
  //margin-top: 151rpx;
  margin-top: 55rpx;
  display: inline-flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: space-between;

  .listBox {
    width: 345rpx;
    height: 270rpx;
    background: #FFFFFF;
    box-shadow: 0rpx 5rpx 15rpx rgba(142, 82, 77, 0.1);
    border-radius: 20rpx;
    z-index: 1;
    margin-bottom: 20rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    image {
      width: 66rpx;
      height: 66rpx;
      background: #F34C20;
    }
  }
}
/deep/ radio .wx-radio-input.wx-radio-input-checked,
/deep/ radio .uni-radio-input.uni-radio-input-checked {
  border: 1px solid #2291F8 !important;
  background-color: #2291F8 !important
}
// 二维码弹框
.qr-code-content {
  width: 80vw;

  .image {
    width: 80vw;
    height: 80vw;
  }

  .close-qr-code {
    position: fixed;
    top: 35rpx;
    right: 70rpx;
    color: #FFFFFF;
    border: 2rpx solid #FFFFFF;
    height: 40rpx;
    line-height: 36rpx;
    padding: 0 20rpx;
    border-radius: 50rpx;
  }
}
// 登录
.login-content{
  .login-form{
    background: #F6f6f6;
    padding: 40% 30rpx 0 30rpx;
    min-height: 100vh;
    width: 100vw;
    .title{
      font-size: 36rpx;
      height: 80rpx;
      line-height: 60rpx;
      font-weight: bold;
    }
    .login-item{
      width: 100%;
      display: inline-flex;
      flex-direction: row;
      flex-wrap: nowrap;
      justify-content: flex-start;
      align-items: center;
      height: 80rpx;
      border-top: 2rpx solid #f3f3f3;
      border-bottom: 2rpx solid #f3f3f3;
      .item-icon{
        font-size: 40rpx;
        width: 70rpx;
      }
      .item-input{
        width: calc(100% - 80rpx);
        padding-left: 15rpx;
      }
    }
    .login-btn{
      width: 70%;
      background: #8e1318;
      color: #f4dede;
      height: 60rpx;
      font-size: 30rpx;
      line-height: 60rpx;
      text-align: center;
      border-radius: 100rpx;
      margin: 130rpx auto 0 auto;
    }
  }
}

</style>
