<template>
	<view class="business">
    <!--登录后内容-->
    <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>
    <!--二维码-->
    <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";

export default {
  name: 'business',
  components: {
    shopList,
    emptyPage
  },
  data() {
    return {
      pageTitle: '',
      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 || merchantInfo.merchant_type == 3){
          // 酒道馆核销用户优惠券
          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){
        if(merchantInfo.merchant_type == 1 || merchantInfo.merchant_type == 3){
          // 酒道馆核销用户优惠券
          // list.push({
          //   type: 'exchange_qr_code',
          //   title: '特色酒兑换',
          //   url: '',
          //   icon: 'iconfont icon-erweima1'
          // });
        }else{
          // 普通商户核销用户订单
          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;
    // 商户或者酒道馆是否登录  未登录进入登录页面
    if(this.is_sys != 1 && !this.shopIsLogin){
      uni.navigateTo({
        url: '/pages/admin/business/login?is_sys=' + this.is_sys
      })
      return false;
    }
    // 设置页面标题
    this.pageTitle = '门店商家管理';
    if(this.is_sys == 1) this.pageTitle = '平台管理';
    if(this.is_sys == 2) this.pageTitle = '酒道馆管理';
    else if(this.is_sys == 3) this.pageTitle = '烟酒店管理';



    uni.setNavigationBarTitle({
      title: this.pageTitle
    })
    // 登录后操作
    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(data) {
      // 判断：是否存在有效 员工列表
      if(Object.values(data).length <= 0){
        uni.showModal({
          title: '禁止访问',
          content: '无有效管理员信息！',
          success: function success(res) {
            uni.switchTab({
              url: '/pages/user/index',
            });
          }
        });
        return false;
      }
      // 处理信息
      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;
      let merchantInfo = this.service.merchant || {};
      // 点击按钮触发弹框输入框的显示
      uni.showModal({
        title: '兑换商品金额', // 弹框标题
        content: '',
        editable: true,//是否显示输入框
        placeholderText: '请输入用户兑换商品的总金额',//输入框提示内容
        showCancel: true, // 是否显示取消按钮
        cancelText: '取消', // 取消按钮文字
        confirmText: '确定', // 确认按钮文字
        success: (res) => {
          // 商户类别(merchant_type)：0=普通商户，1=酒道馆，2=供应商，3=烟酒店
          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: (merchantInfo.merchant_type == 1 || merchantInfo.merchant_type == 3) ? 1 : 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();
    },
  }
}
</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;
  }
}
</style>
