<template>
  <div class="page-header-index-wide dashboard-modern">
    <!-- 添加欢迎区块 -->
    <div class="welcome-banner" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
                                      color: white; padding: 24px; border-radius: 8px; margin-bottom: 24px;">
      <h2 style="color: white; margin: 0;">欢迎回到 MileStone ERP 📊</h2>
      <p style="margin: 8px 0 0 0; opacity: 0.9;">{{ getCurrentTimeGreeting() }}</p>
    </div>
    
    <!-- 核心指标卡片区域 - 增强视觉效果 -->
    <div class="metrics-section">
      <h3 style="margin-bottom: 16px; color: #1890ff;">📈 今日经营概况</h3>
      <a-row :gutter="24">
        <a-col :sm="24" :md="12" :xl="6" :style="{ paddingRight: '0px',marginBottom: '16px' }">
          <chart-card :loading="loading" title="今日销售" data-step="1" data-title="今日销售" data-intro="统计今日销售单据的总金额"
                      class="metric-card sales-card">
            <a-tooltip title="统计今日销售单据的总金额" slot="action">
              <a-icon type="info-circle-o" />
            </a-tooltip>
            <head-info :content="statistics.todaySale">
              <span slot="prefix">💰</span>
            </head-info>
          </chart-card>
        </a-col>
        <a-col :sm="24" :md="12" :xl="6" :style="{ paddingRight: '0px',marginBottom: '16px' }">
          <chart-card :loading="loading" title="今日零售" data-step="2" data-title="今日零售" data-intro="统计今日零售单据的总金额"
                      class="metric-card retail-card">
            <a-tooltip title="统计今日零售单据的总金额" slot="action">
              <a-icon type="info-circle-o" />
            </a-tooltip>
            <head-info :content="statistics.todayRetailSale">
              <span slot="prefix">🛒</span>
            </head-info>
          </chart-card>
        </a-col>
        <a-col :sm="24" :md="12" :xl="6" :style="{ paddingRight: '0px',marginBottom: '16px' }">
          <chart-card :loading="loading" title="今日采购" data-step="3" data-title="今日采购" data-intro="统计今日采购单据的总金额"
                      class="metric-card purchase-card">
            <a-tooltip title="统计今日采购单据的总金额" slot="action">
              <a-icon type="info-circle-o" />
            </a-tooltip>
            <head-info :content="statistics.todayBuy">
              <span slot="prefix">📦</span>
            </head-info>
          </chart-card>
        </a-col>
        <a-col :sm="24" :md="12" :xl="6" :style="{ paddingRight: '0px',marginBottom: '16px' }">
          <chart-card :loading="loading" title="当日利润预估" class="metric-card profit-card">
            <a-tooltip title="今日销售减去今日采购的简单估算" slot="action">
              <a-icon type="info-circle-o" />
            </a-tooltip>
            <head-info :content="getTodayProfit()">
              <span slot="prefix">💎</span>
            </head-info>
          </chart-card>
        </a-col>
      </a-row>
    </div>

    <!-- 月度数据区域 -->
    <div class="monthly-section" style="margin-top: 32px;">
      <h3 style="margin-bottom: 16px; color: #52c41a;">📅 本月累计数据</h3>
      <a-row :gutter="24">
        <a-col :sm="24" :md="8" :xl="8" :style="{ paddingRight: '0px',marginBottom: '16px' }">
          <chart-card :loading="loading" title="本月累计销售" class="monthly-card">
            <a-tooltip title="统计本月销售单据的总金额" slot="action">
              <a-icon type="info-circle-o" />
            </a-tooltip>
            <head-info :content="statistics.monthSale">
              <span slot="prefix">📊</span>
            </head-info>
          </chart-card>
        </a-col>
        <a-col :sm="24" :md="8" :xl="8" :style="{ paddingRight: '0px',marginBottom: '16px' }">
          <chart-card :loading="loading" title="本月累计零售" class="monthly-card">
            <a-tooltip title="统计本月零售单据的总金额" slot="action">
              <a-icon type="info-circle-o" />
            </a-tooltip>
            <head-info :content="statistics.monthRetailSale">
              <span slot="prefix">🛍️</span>
            </head-info>
          </chart-card>
        </a-col>
        <a-col :sm="24" :md="8" :xl="8" :style="{ paddingRight: '0px',marginBottom: '16px' }">
          <chart-card :loading="loading" title="本月累计采购" class="monthly-card">
            <a-tooltip placement="left" title="统计本月采购单据的总金额" slot="action">
              <a-icon type="info-circle-o" />
            </a-tooltip>
            <head-info :content="statistics.monthBuy">
              <span slot="prefix">📋</span>
            </head-info>
          </chart-card>
        </a-col>
      </a-row>
    </div>

    <!-- 历史对比区域 -->
    <div class="comparison-section" style="margin-top: 32px;">
      <a-row :gutter="24">
        <a-col :span="12">
          <h4 style="margin-bottom: 16px; color: #fa8c16;">📈 昨日数据</h4>
          <a-row :gutter="16">
            <a-col :span="8">
              <chart-card :loading="loading" title="昨日销售" class="comparison-card yesterday-card">
                <a-tooltip title="统计昨日销售单据的总金额" slot="action">
                  <a-icon type="info-circle-o" />
                </a-tooltip>
                <head-info :content="statistics.yesterdaySale"></head-info>
              </chart-card>
            </a-col>
            <a-col :span="8">
              <chart-card :loading="loading" title="昨日零售" class="comparison-card yesterday-card">
                <a-tooltip title="统计昨日零售单据的总金额" slot="action">
                  <a-icon type="info-circle-o" />
                </a-tooltip>
                <head-info :content="statistics.yesterdayRetailSale"></head-info>
              </chart-card>
            </a-col>
            <a-col :span="8">
              <chart-card :loading="loading" title="昨日采购" class="comparison-card yesterday-card">
                <a-tooltip title="统计昨日采购单据的总金额" slot="action">
                  <a-icon type="info-circle-o" />
                </a-tooltip>
                <head-info :content="statistics.yesterdayBuy"></head-info>
              </chart-card>
            </a-col>
          </a-row>
        </a-col>
        
        <a-col :span="12">
          <h4 style="margin-bottom: 16px; color: #722ed1;">🏆 年度累计</h4>
          <a-row :gutter="16">
            <a-col :span="8">
              <chart-card :loading="loading" title="年度销售" class="comparison-card yearly-card">
                <a-tooltip title="统计今年销售单据的总金额" slot="action">
                  <a-icon type="info-circle-o" />
                </a-tooltip>
                <head-info :content="statistics.yearSale"></head-info>
              </chart-card>
            </a-col>
            <a-col :span="8">
              <chart-card :loading="loading" title="年度零售" class="comparison-card yearly-card">
                <a-tooltip title="统计今年零售单据的总金额" slot="action">
                  <a-icon type="info-circle-o" />
                </a-tooltip>
                <head-info :content="statistics.yearRetailSale"></head-info>
              </chart-card>
            </a-col>
            <a-col :span="8">
              <chart-card :loading="loading" title="年度采购" class="comparison-card yearly-card">
                <a-tooltip placement="left" title="统计今年采购单据的总金额" slot="action">
                  <a-icon type="info-circle-o" />
                </a-tooltip>
                <head-info :content="statistics.yearBuy"></head-info>
              </chart-card>
            </a-col>
          </a-row>
        </a-col>
      </a-row>
    </div>
    <a-row :gutter="24">
      <a-col :sm="24" :md="12" :xl="8" :style="{ paddingRight: '0px',marginBottom: '12px' }">
        <a-card :loading="loading" :bordered="false" :body-style="{paddingRight: '5'}" data-step="4" data-title="销售统计"
                data-intro="统计往前6个月每月销售的总金额">
          <bar title="销售统计" :height="barHeight" :yaxisText="yaxisText" :dataSource="salePriceData"/>
        </a-card>
      </a-col>
      <a-col :sm="24" :md="12" :xl="8" :style="{ paddingRight: '0px',marginBottom: '12px' }">
        <a-card :loading="loading" :bordered="false" :body-style="{paddingRight: '5'}" data-step="5" data-title="零售统计"
                data-intro="统计往前6个月每月零售的总金额">
          <bar title="零售统计" :height="barHeight" :yaxisText="yaxisText" :dataSource="retailPriceData"/>
        </a-card>
      </a-col>
      <a-col :sm="24" :md="12" :xl="8" :style="{ paddingRight: '0px',marginBottom: '12px' }">
        <a-card :loading="loading" :bordered="false" :body-style="{paddingRight: '5'}" data-step="6" data-title="采购统计"
                data-intro="统计往前6个月每月采购的总金额">
          <bar title="采购统计" :height="barHeight" :yaxisText="yaxisText" :dataSource="buyPriceData"/>
        </a-card>
      </a-col>
    </a-row>
    <a-row :gutter="24">
      <a-col :sm="24" :md="24" :xl="24" :style="{ paddingRight: '0px',marginBottom: '6px' }">
        <a-card :bordered="false" :body-style="{padding: '5'}" data-step="7" data-title="服务和版权"
                data-intro="展示服务到期时间（快到期时会出现续费链接，请注意及时续费）、
          用户数量（是指最多可以录入的用户数量）、版权信息">
          <div class="hidden-xs" style="float:right;">
            <a-popover
              trigger="hover"
              :visible="hovered"
              @visibleChange="handleHoverChange">
              <div slot="content">
                <img src="/static/weixin.jpg" style="width:258px" />
              </div>
              <a-button type="link" v-if="showWeixinSpan()">MileStone Co. ERP</a-button>
            </a-popover>
            © 2015-2030 MILESTONE INDUSTRIAL DEVELOPMENT CORPORATION
          </div>
          <a-tag v-if="tenant.type==0" color="blue">试用到期：{{tenant.expireTime}}</a-tag>
          <a-tag v-if="tenant.type==0" color="blue">试用用户：{{tenant.userCurrentNum}}/{{tenant.userNumLimit}}</a-tag>
          <a-tag v-if="tenant.type==1" color="blue">服务到期：{{tenant.expireTime}}</a-tag>
          <a-tag v-if="tenant.type==1" color="blue">授权用户：{{tenant.userCurrentNum}}/{{tenant.userNumLimit}}</a-tag>
          <a v-if="hasExpire" style="color: red;" :href="payFeeUrl" target="_blank">立即续费</a>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>
<script>
  import ChartCard from '@/components/ChartCard'
  import ACol from "ant-design-vue/es/grid/Col"
  import ATooltip from "ant-design-vue/es/tooltip/Tooltip"
  import MiniArea from '@/components/chart/MiniArea'
  import MiniBar from '@/components/chart/MiniBar'
  import MiniProgress from '@/components/chart/MiniProgress'
  import Bar from '@/components/chart/Bar'
  import LineChartMultid from '@/components/chart/LineChartMultid'
  import HeadInfo from '@/components/tools/HeadInfo.vue'
  import Trend from '@/components/Trend'
  import { getBuyAndSaleStatistics, buyOrSalePrice, getPlatformConfigByKey } from '@/api/api'
  import { handleIntroJs } from "@/utils/util"
  import { getAction,postAction } from '../../api/manage'

  export default {
    name: "IndexChart",
    components: {
      ATooltip,
      ACol,
      ChartCard,
      MiniArea,
      MiniBar,
      MiniProgress,
      Bar,
      Trend,
      LineChartMultid,
      HeadInfo
    },
    data() {
      return {
        hovered: false,
        systemTitle: window.SYS_TITLE,
        systemUrl: window.SYS_URL,
        loading: true,
        center: null,
        statistics: {},
        barHeight: document.documentElement.clientHeight-585,
        yaxisText: '金额',
        buyPriceData: [],
        salePriceData: [],
        retailPriceData: [],
        visitFields:['ip','visit'],
        visitInfo:[],
        hasExpire: false,
        payFeeUrl: '',
        tenant: {
          type: '',
          expireTime: '',
          userCurrentNum: '',
          userNumLimit: '',
          tenantId: ''
        }
      }
    },
    created() {
      setTimeout(() => {
        this.loading = !this.loading
      }, 1000)
      this.initInfo()
      this.initWithTenant()
    },
    mounted() {
      handleIntroJs('indexChart', 1)
    },
    methods: {
      initInfo () {
        getBuyAndSaleStatistics().then((res)=>{
          if(res.code === 200){
            this.statistics = res.data;
          }
        })
        buyOrSalePrice().then(res=>{
          if(res.code === 200){
            this.buyPriceData = res.data.buyPriceList
            this.salePriceData = res.data.salePriceList
            this.retailPriceData = res.data.retailPriceList
          }
        })
        getPlatformConfigByKey({"platformKey": "pay_fee_url"}).then((res)=> {
          if (res && res.code === 200) {
            this.payFeeUrl = res.data.platformValue
          }
        })
      },
      initWithTenant() {
        getAction("/user/infoWithTenant",{}).then(res=>{
          if(res && res.code === 200) {
            this.tenant = res.data
            let currentTime = new Date(); //新建一个日期对象，默认现在的时间
            let expireTime = new Date(res.data.expireTime); //设置过去的一个时间点，"yyyy-MM-dd HH:mm:ss"格式化日期
            let difftime = expireTime - currentTime; //计算时间差
            //如果距离到期还剩5天就进行提示续费
            if(difftime<86400000*5) {
              this.hasExpire = true
              //针对免费租户发送试用到期的消息提醒
              if(res.data.type === '0') {
                //先检查有无发送过，只发送一次
                getAction("/msg/getMsgCountByType",{'type': '试用到期'}).then(res=>{
                  if(res && res.code === 200) {
                    if(res.data.count === 0) {
                      //发送消息
                      let msgParam = {
                        'msgTitle': '试用到期提醒',
                        'msgContent': '试用期即将结束，请您及时续费，过期将会影响正常使用！',
                        'type': '试用到期',
                        'userId': this.tenant.tenantId
                      }
                      postAction("/msg/add",msgParam).then(res=>{
                        if(res && res.code === 200) {

                        }
                      })
                    }
                  }
                })
              }
            }
          }
        })
      },
      handleHoverChange(visible) {
        this.hovered = visible
      },
      showWeixinSpan() {
        let host = window.location.host
        if(host === 'cloud.gyjerp.com') {
          return true
        } else {
          return false
        }
      },
      getCurrentTimeGreeting() {
        const now = new Date()
        const hour = now.getHours()
        const date = now.toLocaleDateString('zh-CN', {
          year: 'numeric',
          month: 'long', 
          day: 'numeric',
          weekday: 'long'
        })
        
        let greeting = ''
        if (hour < 6) {
          greeting = '夜深了，注意休息 🌙'
        } else if (hour < 12) {
          greeting = '早上好，新的一天开始了 ☀️'
        } else if (hour < 18) {
          greeting = '下午好，继续加油 💪'
        } else {
          greeting = '晚上好，今天辛苦了 🌅'
        }
        
        return `${greeting} | ${date}`
      },
      getTodayProfit() {
        // 简单计算当日利润预估（销售-采购）
        const saleText = this.statistics.todaySale || '0'
        const buyText = this.statistics.todayBuy || '0'
        const sale = parseFloat(saleText.replace(/[,¥]/g, '') || '0')
        const buy = parseFloat(buyText.replace(/[,¥]/g, '') || '0')
        const profit = sale - buy
        
        if (profit > 0) {
          return '¥' + profit.toLocaleString()
        } else if (profit < 0) {
          return '-¥' + Math.abs(profit).toLocaleString()
        }
        return '¥0'
      }
    }
  }
</script>
<style lang="less" scoped>
  .dashboard-modern {
    .welcome-banner {
      box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
      transition: all 0.3s ease;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.25);
      }
    }
    
    .metrics-section, .monthly-section, .comparison-section {
      .ant-card {
        transition: all 0.3s ease;
        border-radius: 8px;
        overflow: hidden;
        
        &:hover {
          transform: translateY(-4px);
          box-shadow: 0 8px 25px rgba(0,0,0,0.1);
        }
      }
    }
    
    .metric-card {
      position: relative;
      
      &.sales-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 4px;
        height: 100%;
        background: linear-gradient(180deg, #f56565 0%, #e53e3e 100%);
      }
      
      &.retail-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 4px;
        height: 100%;
        background: linear-gradient(180deg, #48bb78 0%, #38a169 100%);
      }
      
      &.purchase-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 4px;
        height: 100%;
        background: linear-gradient(180deg, #4299e1 0%, #3182ce 100%);
      }
      
      &.profit-card::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 4px;
        height: 100%;
        background: linear-gradient(180deg, #ed8936 0%, #dd6b20 100%);
      }
    }
    
    .monthly-card {
      border-left: 4px solid #52c41a;
      background: linear-gradient(135deg, #f6ffed 0%, #ffffff 100%);
    }
    
    .comparison-card {
      &.yesterday-card {
        border-left: 3px solid #fa8c16;
        background: linear-gradient(135deg, #fff7e6 0%, #ffffff 100%);
      }
      
      &.yearly-card {
        border-left: 3px solid #722ed1;
        background: linear-gradient(135deg, #f9f0ff 0%, #ffffff 100%);
      }
    }
  }

  .circle-cust{
    position: relative;
    top: 28px;
    left: -100%;
  }
  .extra-wrapper {
    line-height: 55px;
    padding-right: 24px;

    .extra-item {
      display: inline-block;
      margin-right: 24px;

      a {
        margin-left: 24px;
      }
    }
  }
  /* 首页访问量统计 */
  .head-info {
    position: relative;
    text-align: left;
    padding: 0 32px 0 0;
    min-width: 125px;
    &.center {
      text-align: center;
      padding: 0 32px;
    }
    span {
      color: rgba(0, 0, 0, .45);
      display: inline-block;
      font-size: .95rem;
      line-height: 42px;
      margin-bottom: 4px;
    }
    p {
      line-height: 42px;
      margin: 0;
      a {
        font-weight: 600;
        font-size: 1rem;
      }
    }
  }
</style>