<template>
  <a-form
    ref="formRef"
    :model="formData"
    :rules="rules"
    layout="vertical"
    @finish="handleSubmit"
  >
    <a-row :gutter="16">
      <a-col :span="24">
        <a-form-item label="任务标题" name="title">
          <a-input
            v-model:value="formData.title"
            placeholder="请输入任务标题"
            :maxlength="100"
            show-count
          />
        </a-form-item>
      </a-col>
      
      <a-col :span="24">
        <a-form-item label="任务描述" name="description">
          <a-textarea
            v-model:value="formData.description"
            placeholder="请输入任务详细描述"
            :rows="3"
            :maxlength="500"
            show-count
          />
        </a-form-item>
      </a-col>
      
      <a-col :span="12">
        <a-form-item label="优先级" name="priority">
          <a-select
            v-model:value="formData.priority"
            placeholder="请选择优先级"
            style="width: 100%"
          >
            <a-select-option value="low">
              <div class="priority-option">
                <div class="priority-dot green"></div>
                <span>低</span>
              </div>
            </a-select-option>
            <a-select-option value="medium">
              <div class="priority-option">
                <div class="priority-dot orange"></div>
                <span>中</span>
              </div>
            </a-select-option>
            <a-select-option value="high">
              <div class="priority-option">
                <div class="priority-dot red"></div>
                <span>高</span>
              </div>
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      
      <a-col :span="12">
        <a-form-item label="任务类型" name="type">
          <a-select
            v-model:value="formData.type"
            placeholder="请选择任务类型"
            style="width: 100%"
          >
            <a-select-option value="feature">功能开发</a-select-option>
            <a-select-option value="bug">缺陷修复</a-select-option>
            <a-select-option value="improvement">优化改进</a-select-option>
            <a-select-option value="maintenance">维护任务</a-select-option>
            <a-select-option value="research">调研分析</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      
      <a-col :span="12">
        <a-form-item label="负责人" name="assignee">
          <a-select
            v-model:value="formData.assignee"
            placeholder="请选择负责人"
            style="width: 100%"
          >
            <a-select-option v-for="user in users" :key="user.id" :value="user.name">
              <div class="user-option">
                <div class="user-avatar">
                  <img :src="user.avatar" alt="" />
                </div>
                <span>{{ user.name }}</span>
                <span class="user-role">{{ user.role }}</span>
              </div>
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      
      <a-col :span="12">
        <a-form-item label="所属项目" name="project">
          <a-select
            v-model:value="formData.project"
            placeholder="请选择项目"
            style="width: 100%"
          >
            <a-select-option v-for="project in projects" :key="project.id" :value="project.name">
              <div class="project-option">
                <span class="project-icon">{{ project.icon }}</span>
                <span>{{ project.name }}</span>
              </div>
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      
      <a-col :span="12">
        <a-form-item label="预计工时" name="estimatedHours">
          <a-input-number
            v-model:value="formData.estimatedHours"
            placeholder="请输入预计工时"
            :min="0"
            :precision="0"
            addon-after="小时"
            style="width: 100%"
          />
        </a-form-item>
      </a-col>
      
      <a-col :span="12">
        <a-form-item label="截止日期" name="dueDate">
          <a-date-picker
            v-model:value="formData.dueDate"
            placeholder="请选择截止日期"
            style="width: 100%"
            :disabled-date="disabledDate"
            show-time
            format="YYYY-MM-DD HH:mm"
          />
        </a-form-item>
      </a-col>
      
      <a-col :span="24">
        <a-form-item label="协作者" name="collaborators">
          <a-select
            v-model:value="formData.collaborators"
            mode="multiple"
            placeholder="请选择协作者"
            style="width: 100%"
            :options="collaboratorOptions"
            :field-names="{ label: 'name', value: 'id' }"
          />
        </a-form-item>
      </a-col>
      
      <a-col :span="24" v-if="mode === 'edit'">
        <a-form-item label="添加备注" name="remark">
          <a-textarea
            v-model:value="formData.remark"
            placeholder="添加任务修改备注（选填）"
            :rows="2"
            :maxlength="200"
            show-count
          />
        </a-form-item>
      </a-col>
    </a-row>
    
    <a-form-item>
      <div class="form-actions">
        <a-button type="primary" html-type="submit" :loading="submitting">
          {{ mode === 'create' ? '创建任务' : '保存修改' }}
        </a-button>
        <a-button @click="handleCancel">
          取消
        </a-button>
      </div>
    </a-form-item>
  </a-form>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import type { FormInstance } from 'ant-design-vue'
import { message } from 'ant-design-vue'
import type { Dayjs } from 'dayjs'

interface Props {
  mode?: 'create' | 'edit'
  initialData?: any
}

const props = withDefaults(defineProps<Props>(), {
  mode: 'create',
  initialData: null
})

interface User {
  id: number
  name: string
  avatar: string
  role: string
}

interface Project {
  id: number
  name: string
  icon: string
}

// 表单引用
const formRef = ref<FormInstance>()
const submitting = ref(false)

// 表单数据
const formData = ref({
  title: '',
  description: '',
  priority: 'medium' as 'low' | 'medium' | 'high',
  type: 'feature',
  assignee: '',
  project: '',
  estimatedHours: null as number | null,
  dueDate: null as Dayjs | null,
  collaborators: [] as string[],
  remark: ''
})

// 验证规则
const rules = {
  title: [
    { required: true, message: '请输入任务标题', trigger: 'blur' },
    { min: 2, max: 100, message: '标题长度为2-100个字符', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入任务描述', trigger: 'blur' }
  ],
  priority: [
    { required: true, message: '请选择优先级', trigger: 'change' }
  ],
  assignee: [
    { required: true, message: '请选择负责人', trigger: 'change' }
  ],
  project: [
    { required: true, message: '请选择所属项目', trigger: 'change' }
  ],
  estimatedHours: [
    { type: 'number', min: 0, message: '请输入有效的工时数字', trigger: 'change' }
  ],
  dueDate: [
    { required: true, message: '请选择截止日期', trigger: 'change' }
  ]
}

// 用户列表
const users = ref<User[]>([
  { id: 1, name: '张三', avatar: 'https://randomuser.me/api/portraits/men/32.jpg', role: '产品经理' },
  { id: 2, name: '李四', avatar: 'https://randomuser.me/api/portraits/women/44.jpg', role: 'UI设计师' },
  { id: 3, name: '王五', avatar: 'https://randomuser.me/api/portraits/men/67.jpg', role: '前端工程师' },
  { id: 4, name: '赵六', avatar: 'https://randomuser.me/api/portraits/women/23.jpg', role: '后端工程师' },
  { id: 5, name: '钱七', avatar: 'https://randomuser.me/api/portraits/men/89.jpg', role: '测试工程师' },
  { id: 6, name: '孙八', avatar: 'https://randomuser.me/api/portraits/women/56.jpg', role: '运维工程师' }
])

// 项目列表
const projects = ref<Project[]>([
  { id: 1, name: '智慧园区管理系统', icon: '🏢' },
  { id: 2, name: '客户关系管理升级', icon: '📊' },
  { id: 3, name: '移动端App开发', icon: '📱' },
  { id: 4, name: '数据中心建设', icon: '🖥️' },
  { id: 5, name: '内部分享平台', icon: '📚' }
])

// 协作者选项
const collaboratorOptions = computed(() => 
  users.value.map(user => ({
    label: user.name,
    value: user.id.toString(),
    avatar: user.avatar,
    role: user.role
  }))
)

// 事件
const emit = defineEmits<{
  (e: 'submit', data: any): void
  (e: 'cancel'): void
}>()

// 禁用过去的日期
function disabledDate(current: Dayjs) {
  return current && current < new Date().setHours(0, 0, 0, 0)
}

// 提交表单
function handleSubmit() {
  submitting.value = true
  
  setTimeout(() => {
    const data = {
      ...formData.value,
      dueDate: formData.value.dueDate?.format('YYYY-MM-DD HH:mm')
    }
    
    emit('submit', data)
    submitting.value = false
  }, 1000)
}

// 取消
function handleCancel() {
  emit('cancel')
}

// 重置表单
function reset() {
  formRef.value?.resetFields()
  if (props.initialData) {
    formData.value = { ...props.initialData }
    formData.value.collaborators = props.initialData.collaborators || []
  }
}

// 监听初始数据变化
watch(() => props.initialData, (newData) => {
  if (newData) {
    formData.value = { ...newData }
    formData.value.collaborators = newData.collaborators || []
  }
}, { immediate: true })

// 暴露方法
defineExpose({
  reset
})
</script>

<style scoped>
.priority-option {
  display: flex;
  align-items: center;
  gap: 8px;
}

.priority-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.priority-dot.green {
  background-color: #52c41a;
}

.priority-dot.orange {
  background-color: #fa8c16;
}

.priority-dot.red {
  background-color: #ff4d4f;
}

.user-option {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-avatar {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  overflow: hidden;
  background-color: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-role {
  margin-left: auto;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
}

.project-option {
  display: flex;
  align-items: center;
  gap: 8px;
}

.project-icon {
  font-size: 16px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
}
</style>