<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-09-08 17:11:02
 * @LastEditors: 无尘
 * @LastEditTime: 2020-10-27 14:45:14
-->
<template>
  <div class="app-detail">
    <el-form label-width="90px">
      <h3 class="model-title">应用信息</h3>
      <div class="app-info">
        <div :class="['app-logo', { 'no-logo': !appData.applicationLogo }]">
          <img v-if="!!appData.applicationLogo" :src="appData.applicationLogo" alt="">
        </div>
        <div>
          <p class="app-name">{{ appData.applicationName || '--' }}</p>
          <p class="app-desc">{{ appData.applicationDesc }}</p>
        </div>
      </div>
      <el-form-item label="版本" style="margin-bottom: 10px">{{ appData.applicationVersion || '--' }}</el-form-item>
      <el-form-item label="到期时间">
        <span>{{ appData.overTime | timeStampToYmd }}{{ !appData.overTime ? '--': ''}}</span> <span class="color-f5222d p-l-10">{{ appData.expirationDay > 0 && ( 2592000000 >= appData.expirationDay) ? '(即将到期)' : '' }}</span> <span v-if="appData.expirationDate < 0" :class="['p-l-10',2592000000 >= appData.expirationDay ? 'color-f5222d' : 'color-303133']">还有{{ appData.overTime | timeStampSpace }}到期</span>
        <el-tag v-if="appData.expirationDate > 0" type="danger" style="margin: 0 20px 0 10px">已到期</el-tag>
        <el-button class="m-l-26" type="text" @click="toRenew">去续费 <i class="iconfont iconjiantouxiangyou"></i></el-button>
      </el-form-item>
      <h3 class="model-title" style="margin-top: 40px">应用设置</h3>
      <el-form-item label="可见范围">
        <h4 class="sub-title">管理成员</h4>
        <div class="admin-list">
          <el-tag type="info" v-for="el in appData.adminRange" :key="el.roleId">{{ el.roleName }}</el-tag>
          <span v-if="appData.adminRange.length == 0" class="no-admin">暂无成员</span>
        </div>
        <h4 class="sub-title">门店成员</h4>
        <div class="admin-list">
          <el-tag type="info" v-for="el in appData.storeAdminRange" :key="el.roleId">{{ el.roleName }}</el-tag>
          <span v-if="appData.storeAdminRange.length == 0" class="no-admin">暂无成员</span>
        </div>
      </el-form-item>
      <h3 class="model-title" style="margin-top: 40px">授权信息</h3>
      <div class="auth-info">
        <ul>
          <li class="font-14 color-303133" v-for="(item, index) in authInfoList" :key="index+'list'">{{index + 1}}. {{ item.rightsName || '--'}}</li>
        </ul>
      </div>
    </el-form>
  </div>
</template>

<script>
import fetch from '@/api/app-controls';
const { getAppDetail } = fetch;
import fetchData from '@/api/app-auth.js';
const { getAppAuthInfo } = fetchData;
export default {
  name: 'AppDetail',
  data() {
    return {
      appData: {
        applicationId: '',
        applicationName: '',
        applicationDesc: '',
        applicationVersion: '',
        overTime: '',
        expirationDate: '',
        expirationDay: '',
        adminRange: [],
        storeAdminRange: [],
        applicationLogo: '',
        serverId: ''
      },
      authInfoList: []
    };
  },
  created() {
    const { menuId, applicationId } = this.$route.query;
    if (!menuId) {
      this.$router.go(-1);
    }
    this.$emit('showTab', '/app-list');
    this.getAppDetail(menuId, applicationId);
    this.getAuthInfo();
  },
  methods: {
    /**
     * @description: 续费
     * @author: 无尘
     */
    toRenew() {
      const { serverId } = this.appData;
      window.open(`/market/product/server/${serverId}`, '_blank', 'noopener');
    },
    /**
     * @description: 获取应用详情信息
     * @param {String} menuId
     * @param {String} applicationId
     * @author: 无尘
     */
    getAppDetail(menuId, applicationId) {
      getAppDetail({ menuId, applicationId }).then(res => {
        const { applicationName, applicationDesc, applicationVersion, overTime, adminRange, storeAdminRange, applicationLogo, serverId } = res.result || {};
        this.appData.expirationDate = new Date().getTime() - overTime; // 到期时间
        this.appData.expirationDay = overTime - new Date().getTime(); // 剩余到期天数

        Object.assign(this.appData, { applicationName, applicationDesc, applicationVersion, overTime, applicationLogo, serverId });
        if (typeof adminRange == 'string' && adminRange.length > 0) {
          this.appData.adminRange = JSON.parse(adminRange);
        }
        if (typeof storeAdminRange == 'string' && storeAdminRange.length > 0) {
          this.appData.storeAdminRange = JSON.parse(storeAdminRange);
        }
      });
    },
    /**
     * @description: 获取授权信息
     * @author: 无尘
     */
    getAuthInfo() {
      const that = this;
      const param = {
        appId: that.$route.query.appId
      };
      getAppAuthInfo(param)
        .then(res => {
          that.authInfoList = res.result || [];
        })
        .catch(function(error) {
        });
    }
  }
};
</script>

<style lang="less" scoped>
.color-f5222d {
  color: #f5222d;
}
.m-l-26 {
  margin-left: 26px;
}
.app-detail {
  padding: 20px 30px;
  min-height: 100%;
  background-color: #fff;
  .model-title {
    margin-bottom: 25px;
    font-size: 16px;
    font-weight: 700;
    color: @gray01;
    line-height: 22px;
  }
  .app-info {
    margin-left: 28px;
    margin-bottom: 20px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .app-logo {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-shrink: 0;
      margin-right: 17px;
      width: 46px;
      height: 46px;
      &.no-logo {
        background-color: #E4E7ED;
      }
      img {
        max-width: 100%;
        max-height: 100%;
      }
    }
    .app-name {
      font-size: 14px;
      font-weight: 700;
      color: @gray01;
      line-height: 20px;
    }
    .app-desc {
      font-size: 14px;
      font-weight: 400;
      color: @gray03;
      line-height: 20px;
    }
  }
  .sub-title {
    font-size: 14px;
    font-weight: 700;
    color: @gray01;
  }
  .admin-list {
    margin-bottom: 10px;
    padding: 20px;
    width: 100%;
    min-height: 150px;
    border: 1px solid #E6E7EB;
    .no-admin {
      color: @gray03;
    }
  }
  .auth-info {
    min-width: 525px;
    min-height: 164px;
    margin-top: 20px;
    background: #F5F7FA;
    padding: 8px 16px;
    ul {
      li {
        height: 32px;
        line-height: 32px;
      }
    }
  }
}
</style>
