<template>
  <el-dialog
    :visible.sync="visibleProxy"
    :show-close="false"
    width="480px"
    :close-on-click-modal="false"
    custom-class="todo-detail-dialog"
    append-to-body
  >
    <div class="dialog-inner">
      <div class="dialog-header">
        <div class="dialog-title-wrap">
          <div class="dialog-title">待办详情</div>
          <div v-if="todo" class="dialog-subtitle">{{ todo.tag || '自定' }} · {{ sourceLabel }}</div>
        </div>
        <span class="dialog-close" @click="visibleProxy = false"><i class="el-icon-close"></i></span>
      </div>

      <div v-if="todo" class="dialog-body">
        <div class="detail-hero">
          <div class="detail-hero__icon">
            <i :class="sourceIcon"></i>
          </div>
          <div class="detail-hero__main">
            <div class="detail-hero__title">{{ emptyText(todo.title) }}</div>
            <div v-if="todo.dueTime" class="detail-hero__due">
              <i class="el-icon-time"></i>
              {{ formatDateTime(todo.dueTime) }}
            </div>
          </div>
        </div>

        <div class="detail-card">
          <div class="detail-row">
            <span class="detail-row__label"><i class="el-icon-document"></i>描述</span>
            <span class="detail-row__value">{{ emptyText(todo.desc) }}</span>
          </div>
          <div class="detail-row">
            <span class="detail-row__label"><i class="el-icon-date"></i>创建时间</span>
            <span class="detail-row__value">{{ formatDateTime(todo.createTime) }}</span>
          </div>
          <div v-if="todo.storeName" class="detail-row">
            <span class="detail-row__label"><i class="el-icon-office-building"></i>所属门店</span>
            <span class="detail-row__value">{{ emptyText(todo.storeName) }}</span>
          </div>
        </div>
      </div>

      <div class="dialog-footer">
        <el-button
          v-if="canComplete"
          type="primary"
          size="small"
          class="pill-btn"
          @click="handleComplete"
        >标记完成</el-button>
        <el-button
          v-if="canDismiss"
          size="small"
          class="pill-btn pill-btn--ghost"
          @click="handleDismiss"
        >忽略</el-button>
        <el-button size="small" class="pill-btn pill-btn--plain" @click="visibleProxy = false">关闭</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { handleTodo, dismissTodo } from '@/api/lqTodo'

export default {
  name: 'TodoDetailDialog',
  props: {
    visible: { type: Boolean, default: false },
    todo: { type: Object, default: null }
  },
  computed: {
    visibleProxy: {
      get() { return this.visible },
      set(v) { this.$emit('update:visible', v) }
    },
    canComplete() {
      return this.todo && !this.todo.isWorkflow && this.todo.source === 'manual'
    },
    canDismiss() {
      return this.todo && !this.todo.isWorkflow
    },
    sourceLabel() {
      const map = {
        manual: '自定义待办',
        approval: '审批待办',
        follow_up: '客户跟进',
        system: '系统提醒'
      }
      return map[this.todo?.source] || '待办'
    },
    sourceIcon() {
      const map = {
        manual: 'el-icon-edit-outline',
        approval: 'el-icon-s-check',
        follow_up: 'el-icon-phone-outline',
        system: 'el-icon-bell'
      }
      return map[this.todo?.source] || 'el-icon-tickets'
    }
  },
  methods: {
    emptyText(val) {
      if (val === undefined || val === null || String(val).trim() === '') return '无'
      return val
    },
    formatDateTime(val) {
      if (!val) return '无'
      const d = new Date(val)
      if (isNaN(d.getTime())) return String(val)
      const y = d.getFullYear()
      const m = String(d.getMonth() + 1).padStart(2, '0')
      const day = String(d.getDate()).padStart(2, '0')
      const h = String(d.getHours()).padStart(2, '0')
      const min = String(d.getMinutes()).padStart(2, '0')
      return `${y}-${m}-${day} ${h}:${min}`
    },
    async handleComplete() {
      if (!this.todo || !this.todo.id) return
      try {
        await handleTodo(this.todo.id)
        this.$message.success('已完成')
        this.$emit('success')
        this.visibleProxy = false
      } catch (e) {
        // 错误由拦截器处理
      }
    },
    async handleDismiss() {
      if (!this.todo || !this.todo.id) return
      try {
        await dismissTodo(this.todo.id)
        this.$message.success('已忽略')
        this.$emit('success')
        this.visibleProxy = false
      } catch (e) {
        // 错误由拦截器处理
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .todo-detail-dialog {
  border-radius: 20px;
  padding: 0;
  background: radial-gradient(circle at 0 0, rgba(255, 255, 255, 0.96) 0, rgba(248, 250, 252, 0.98) 40%, rgba(241, 245, 249, 0.98) 100%);
  box-shadow: 0 24px 48px rgba(15, 23, 42, 0.18), 0 0 0 1px rgba(255, 255, 255, 0.9);
  .el-dialog__header { display: none; }
  .el-dialog__body { padding: 0; }
}
.dialog-inner { display: flex; flex-direction: column; }
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 18px 20px 0;
  padding: 10px 14px;
  border-radius: 14px;
  background: rgba(219, 234, 254, 0.96);
}
.dialog-title-wrap { display: flex; flex-direction: column; gap: 2px; }
.dialog-title { font-size: 17px; font-weight: 600; color: #0f172a; }
.dialog-subtitle { font-size: 12px; color: #64748b; }
.dialog-close {
  cursor: pointer;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  color: #64748b;
  transition: all 0.15s;
  &:hover { background: rgba(0, 0, 0, 0.06); color: #0f172a; }
}
.dialog-body { padding: 14px 20px 6px; }
.detail-hero {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid rgba(226, 232, 240, 0.9);
  margin-bottom: 12px;
}
.detail-hero__icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 18px;
  box-shadow: 0 6px 14px rgba(37, 99, 235, 0.28);
}
.detail-hero__main { flex: 1; min-width: 0; }
.detail-hero__title {
  font-size: 16px;
  font-weight: 600;
  color: #0f172a;
  line-height: 1.45;
  word-break: break-all;
}
.detail-hero__due {
  margin-top: 6px;
  font-size: 12px;
  color: #64748b;
  display: flex;
  align-items: center;
  gap: 4px;
}
.detail-card {
  border-radius: 14px;
  background: rgba(248, 250, 252, 0.9);
  border: 1px solid #eef2f7;
  padding: 4px 0;
}
.detail-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 14px;
  font-size: 13px;
  line-height: 1.5;
  & + .detail-row { border-top: 1px dashed #e5e7eb; }
}
.detail-row__label {
  flex-shrink: 0;
  width: 88px;
  color: #64748b;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  i { font-size: 14px; color: #94a3b8; }
}
.detail-row__value {
  flex: 1;
  min-width: 0;
  color: #0f172a;
  word-break: break-all;
  white-space: pre-wrap;
}
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  padding: 12px 20px 18px;
  border-top: 1px solid rgba(229, 231, 235, 0.6);
}
::v-deep .pill-btn {
  border-radius: 999px;
  &.el-button--primary {
    background: #2563eb;
    border-color: #2563eb;
    box-shadow: 0 4px 10px rgba(37, 99, 235, 0.28);
  }
}
.pill-btn--ghost {
  border-radius: 999px;
  color: #64748b;
  border-color: #e2e8f0;
  background: #fff;
}
.pill-btn--plain {
  border-radius: 999px;
}
</style>
