<template>
  <view class="container">
    <u-navbar
      leftIconSize="40rpx"
      leftIconColor="#333333"
      title="任务详情"
    >
    </u-navbar>
    
    <view class="detail-card" v-if="jobData">
      <view class="card-header">
        <view class="header-icon">
          <uni-icons type="calendar" size="24" color="#ffffff"></uni-icons>
        </view>
        <view class="header-info">
          <text class="card-name">{{ jobData.jobName }}</text>
        </view>
        <view class="status-tag" :class="jobData.status === '0' ? 'status-normal' : 'status-disabled'">
          {{ dictStr(jobData.status, statusList) }}
        </view>
      </view>
      
      <view class="card-body">
        <view class="info-section">
          <view class="section-title">
            <view class="title-icon"></view>
            <text>基本信息</text>
          </view>
          <view class="info-list">
            <view class="list-item">
              <text class="item-label">任务编号</text>
              <text class="item-value">{{ jobData.jobId }}</text>
            </view>
            <view class="list-item">
              <text class="item-label">任务名称</text>
              <text class="item-value">{{ jobData.jobName }}</text>
            </view>
            <view class="list-item">
              <text class="item-label">任务组名</text>
              <text class="item-value">{{ jobData.jobGroup }}</text>
            </view>
            <view class="list-item">
              <text class="item-label">调用目标</text>
              <text class="item-value">{{ jobData.invokeTarget }}</text>
            </view>
            <view class="list-item">
              <text class="item-label">cron表达式</text>
              <text class="item-value">{{ jobData.cronExpression }}</text>
            </view>
            <view class="list-item">
              <text class="item-label">错失执行策略</text>
              <text class="item-value">{{ dictStr(jobData.misfirePolicy, misfirePolicyList) }}</text>
            </view>
            <view class="list-item">
              <text class="item-label">是否并发</text>
              <text class="item-value">{{ dictStr(jobData.concurrent, concurrentList) }}</text>
            </view>
            <view class="list-item">
              <text class="item-label">创建者</text>
              <text class="item-value">{{ jobData.createBy }}</text>
            </view>
            <view class="list-item" v-if="jobData.createTime">
              <text class="item-label">创建时间</text>
              <text class="item-value">{{ jobData.createTime }}</text>
            </view>
            <view class="list-item" v-if="jobData.updateTime && jobData.updateTime !== jobData.createTime">
              <text class="item-label">更新时间</text>
              <text class="item-value">{{ jobData.updateTime }}</text>
            </view>
          </view>
        </view>
        
        <view class="info-section" v-if="jobData.remark">
          <view class="section-title">
            <view class="title-icon"></view>
            <text>备注</text>
          </view>
          <view class="remark-content">
            <text>{{ jobData.remark }}</text>
          </view>
        </view>
      </view>
    </view>
    
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script setup>
import { ref, reactive, toRefs, computed } from 'vue'
import { getJob } from '@/api/monitor/job'
import { getDicts } from "@/api/system/dict/data"
import { onLoad } from "@dcloudio/uni-app"

const jobId = ref(undefined)
const statusList = ref([])
const concurrentList = ref([])
const misfirePolicyList = ref([])
const data = reactive({
  jobData: null
})
const { jobData } = toRefs(data)

onLoad((options) => {
  if (options.id) {
    jobId.value = options.id
    getJob(jobId.value).then(response => {
      jobData.value = response.data
    })
  }
  
  // 获取字典数据
  getDicts('sys_job_status').then(res => {
    statusList.value = res.data
  })
  getDicts('sys_job_concurrent').then(res => {
    concurrentList.value = res.data
  })
  getDicts('sys_job_policy').then(res => {
    misfirePolicyList.value = res.data
  })
})

// 字典翻译
function dictStr(value, list) {
  const item = list.find(v => v.dictValue === value)
  return item ? item.dictLabel : value
}
</script>

<style lang="scss" scoped>
.container {
  background: #f5f7fa;
  padding-bottom: 24rpx;
}

.detail-card {
  margin: 24rpx;
  background: #ffffff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16rpx 20rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  
  .header-icon {
    width: 48rpx;
    height: 48rpx;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 12rpx;
    backdrop-filter: blur(10rpx);
  }
  
  .header-info {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    
    .card-name {
      color: #ffffff;
      font-size: 28rpx;
      font-weight: 600;
      line-height: 1.2;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
  
  .status-tag {
    padding: 8rpx 20rpx;
    border-radius: 20rpx;
    font-size: 24rpx;
    font-weight: 500;
    white-space: nowrap;

    &.status-normal {
      background-color: rgba(82, 196, 26, 0.2);
      color: #ffffff;
    }

    &.status-disabled {
      background-color: rgba(255, 77, 79, 0.2);
      color: #ffffff;
    }
  }
}

.card-body {
  padding: 24rpx;
}

.info-section {
  margin-bottom: 32rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .section-title {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;
    
    .title-icon {
      width: 6rpx;
      height: 28rpx;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      border-radius: 3rpx;
      margin-right: 12rpx;
    }
    
    text {
      color: #2c3e50;
      font-size: 28rpx;
      font-weight: 600;
    }
  }
  
  .info-list {
    .list-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx;
      background: #f8f9fa;
      border-radius: 8rpx;
      margin-bottom: 12rpx;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .item-label {
        color: #7f8c8d;
        font-size: 26rpx;
      }
      
      .item-value {
        color: #2c3e50;
        font-size: 28rpx;
        font-weight: 500;
        text-align: right;
        
        &.status-normal {
          color: #52c41a;
        }
        
        &.status-disabled {
          color: #ff4d4f;
        }
        
        &.status-default {
          color: #666666;
        }
      }
    }
  }
  
  .remark-content {
    padding: 20rpx;
    background: #f8f9fa;
    border-radius: 8rpx;
    
    text {
      color: #2c3e50;
      font-size: 26rpx;
      line-height: 1.6;
    }
  }
}
</style>