<template>
  <view class="task-add-page">
    <view class="content-scroll" >
      <view class="form-card">
        <view class="section-title">任务信息</view>

        <view class="form-item" @click="openProjectPicker">
          <text class="form-label required">项目</text>
          <view class="form-value">
            <text v-if="formData.projectName" class="value-text">{{ formData.projectName }}</text>
            <text v-else class="placeholder">请选择项目</text>
          </view>
          <text class="arrow">›</text>
        </view>

        <view class="form-item">
          <text class="form-label required">任务类型</text>
          <view class="pill-group">
            <view
              class="pill-item"
              v-for="type in typeOptions"
              :key="type.value"
              :class="{ active: formData.type === type.value }"
              @click="selectType(type.value)"
            >
              {{ type.label }}
            </view>
            <text v-if="!typeOptions.length" class="placeholder">暂无可用任务类型</text>
          </view>
        </view>

        <view class="form-item">
          <text class="form-label required">优先级</text>
          <view class="pill-group">
            <view
              class="pill-item priority"
              v-for="level in levelOptions"
              :key="level.value"
              :class="{ active: formData.level === level.value }"
              @click="selectLevel(level.value)"
            >
              {{ level.label }}
            </view>
            <text v-if="!levelOptions.length" class="placeholder">暂无可用优先级</text>
          </view>
        </view>

        <view class="form-item">
          <text class="form-label required">截止时间</text>
          <view class="form-value" @click="openExpireTimePicker">
            <text v-if="formData.expireTime" class="value-text">{{ formData.expireTime }}</text>
            <text v-else class="placeholder">请选择截止时间</text>
          </view>
          <text class="arrow">›</text>
        </view>

        <view class="form-item align-start">
          <text class="form-label required">任务内容</text>
          <textarea
            v-model="formData.description"
            class="textarea-input"
            placeholder="请填写任务内容、输出要求或其他说明，最多500字"
            placeholder-style="color:#999;"
            :maxlength="500"
          />
        </view>
      </view>

      <view class="form-card">
        <view class="section-title">附件</view>
        <view class="attachment-tip">请上传不超过200MB的文件，支持常见图片、Office、PDF、压缩包等格式。</view>
        <view class="attachment-grid">
          <view
            class="attachment-item"
            v-for="(file, index) in formData.attachments"
            :key="file.url + index"
            @click="previewAttachment(file)"
          >
            <view class="file-icon">{{ getAttachmentIcon(file) }}</view>
            <view class="file-name">{{ file.displayName }}</view>
            <view class="remove-btn" @click.stop="removeAttachment(index)">✕</view>
          </view>
          <view
            class="attachment-item add-item"
            v-if="formData.attachments.length < ATTACHMENT_LIMIT"
            @click="handleAddAttachment"
          >
            <text class="add-icon">＋</text>
            <text class="add-text">上传</text>
          </view>
        </view>
      </view>

      <view class="form-card">
        <view class="section-title">负责人</view>
        <view class="form-item">
          <text class="form-label required">负责人</text>
          <view class="responsible-content">
            <view class="selected-members" v-if="formData.members.length">
              <view
                class="member-chip"
                v-for="member in formData.members"
                :key="member.userId"
              >
                <text>{{ member.userName }}</text>
                <text class="chip-remove" @click.stop="removeMember(member.userId)">✕</text>
              </view>
            </view>
            <view class="placeholder" v-else>请选择负责人，可多选</view>
          </view>
          <view class="picker-trigger" @click="openMemberModal">
            <text>{{ formData.members.length ? '调整' : '选择' }}</text>
            <text class="arrow">›</text>
          </view>
        </view>
      </view>
    </view>

    <view class="submit-bar">
      <uv-button
        type="primary"
        :disabled="!canSubmit || submitting"
        :loading="submitting"
        loadingText="提交中..."
        @click="handleSubmit"
      >
        创建任务
      </uv-button>
    </view>

    <uv-picker
      v-model="showProjectPicker"
      :columns="projectColumns"
      keyName="label"
      @confirm="handleProjectConfirm"
    ></uv-picker>

    <uv-datetime-picker
      ref="expirePickerRef"
      v-model="expirePickerValue"
      mode="datetime"
      @confirm="onExpireTimeConfirm"
    ></uv-datetime-picker>

    <view class="member-modal" v-if="showMemberModal">
      <view class="modal-mask" @click="closeMemberModal"></view>
      <view class="modal-panel">
        <view class="modal-header">
          <text class="modal-title">选择负责人</text>
          <text class="modal-close" @click="closeMemberModal">✕</text>
        </view>
        <view class="search-box">
          <input
            v-model="memberKeyword"
            class="search-input"
            placeholder="搜索姓名或部门"
            placeholder-style="color:#999;"
          />
        </view>
        <scroll-view class="member-list" scroll-y>
          <view
            class="member-item"
            v-for="user in filteredMemberOptions"
            :key="user.userId"
            @click="toggleMember(user.userId)"
          >
            <view class="member-info">
              <text class="member-name">{{ user.userName }}</text>
              <text class="member-dept" v-if="user.deptName">{{ user.deptName }}</text>
            </view>
            <view class="select-indicator" :class="{ active: selectedMemberIds.includes(user.userId) }"></view>
          </view>
          <view class="empty-tip" v-if="!filteredMemberOptions.length">
            <text>未找到匹配的人员</text>
          </view>
        </scroll-view>
        <view class="modal-actions">
          <uv-button @click="closeMemberModal">取消</uv-button>
          <uv-button type="primary" @click="confirmMemberSelection">确定</uv-button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { createTask, getProjectListAll, getUserList } from '@/api';
import { useDictStore } from '@/store/dict';
import { chooseAndUploadImages, batchUploadFilesToQiniu } from '@/utils/qiniu.js';

const dictStore = useDictStore();

const ATTACHMENT_LIMIT = 9;

const formData = ref({
  projectId: '',
  projectName: '',
  type: '',
  level: '',
  description: '',
  expireTime: '',
  attachments: [],
  members: []
});

const presetProjectId = ref('');
const submitting = ref(false);
const loading = ref(false);

const showProjectPicker = ref(false);
const projectColumns = ref([[]]);
const projectOptions = ref([]);

const showMemberModal = ref(false);
const memberOptions = ref([]);
const memberKeyword = ref('');
const selectedMemberIds = ref([]);

const expirePickerRef = ref(null);
const expirePickerValue = ref(Date.now());

const typeOptions = computed(() => {
  return dictStore.getDictByType('task_type').map(item => ({
    label: item.dictLabel,
    value: item.dictValue
  }));
});

const levelOptions = computed(() => {
  return dictStore.getDictByType('task_level').map(item => ({
    label: item.dictLabel,
    value: item.dictValue
  }));
});

const filteredMemberOptions = computed(() => {
  if (!memberKeyword.value.trim()) {
    return memberOptions.value;
  }
  const keyword = memberKeyword.value.trim().toLowerCase();
  return memberOptions.value.filter(user => {
    const name = (user.userName || '').toLowerCase();
    const dept = (user.deptName || '').toLowerCase();
    return name.includes(keyword) || dept.includes(keyword);
  });
});

const canSubmit = computed(() => {
  return Boolean(
    formData.value.projectId &&
    formData.value.type &&
    formData.value.level &&
    formData.value.expireTime &&
    formData.value.description.trim() &&
    formData.value.members.length
  );
});

const formatDateTime = (value) => {
  if (!value && value !== 0) return '';
  const date = typeof value === 'number' ? new Date(value) : new Date(value);
  if (Number.isNaN(date.getTime())) return '';
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hour = String(date.getHours()).padStart(2, '0');
  const minute = String(date.getMinutes()).padStart(2, '0');
  const second = String(date.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
};

const loadProjects = async () => {
  try {
    const res = await getProjectListAll();
    const list = Array.isArray(res?.data) ? res.data : Array.isArray(res) ? res : [];
    projectOptions.value = list.map(item => ({
      id: item.id,
      name: item.name
    }));
    projectColumns.value = [projectOptions.value.map(item => ({
      label: item.name,
      value: item.id
    }))];

    if (presetProjectId.value) {
      const matched = projectOptions.value.find(item => String(item.id) === String(presetProjectId.value));
      if (matched) {
        formData.value.projectId = String(matched.id);
        formData.value.projectName = matched.name;
      }
    }
  } catch (error) {
    console.error('加载项目列表失败:', error);
    uni.showToast({
      title: '加载项目失败',
      icon: 'none'
    });
  }
};

const loadMembers = async () => {
  try {
    const res = await getUserList({
      pageNum: 1,
      pageSize: 200,
      status: 0,
      delFlag: 0
    });
    const rows = Array.isArray(res?.rows) ? res.rows : Array.isArray(res?.data) ? res.data : [];
    memberOptions.value = rows.map(item => ({
      userId: String(item.userId || item.id),
      userName: item.nickName || item.userName || '',
      deptName: item.dept?.deptName || item.deptName || ''
    }));
  } catch (error) {
    console.error('加载用户列表失败:', error);
    uni.showToast({
      title: '加载负责人失败',
      icon: 'none'
    });
  }
};

const openProjectPicker = () => {
  if (!projectOptions.value.length) {
    uni.showToast({
      title: '暂无项目可选',
      icon: 'none'
    });
    return;
  }
  showProjectPicker.value = true;
};

const handleProjectConfirm = ({ value }) => {
  if (value && value.length) {
    const selected = value[0];
    formData.value.projectId = String(selected.value);
    formData.value.projectName = selected.label;
  }
};

const selectType = (typeValue) => {
  formData.value.type = typeValue;
};

const selectLevel = (levelValue) => {
  formData.value.level = levelValue;
};

const openExpireTimePicker = () => {
  if (formData.value.expireTime) {
    expirePickerValue.value = new Date(formData.value.expireTime.replace(/-/g, '/')).getTime();
  } else {
    expirePickerValue.value = Date.now();
  }
  if (expirePickerRef.value?.open) {
    expirePickerRef.value.open();
  }
};

const onExpireTimeConfirm = (event) => {
  if (event?.value) {
    formData.value.expireTime = formatDateTime(event.value);
  }
};

const handleAddAttachment = () => {
  const remaining = ATTACHMENT_LIMIT - formData.value.attachments.length;
  if (remaining <= 0) {
    uni.showToast({
      title: `最多上传${ATTACHMENT_LIMIT}个附件`,
      icon: 'none'
    });
    return;
  }

  uni.showActionSheet({
    itemList: ['上传图片', '上传文件'],
    success: ({ tapIndex }) => {
      if (tapIndex === 0) {
        chooseImages(remaining);
      } else if (tapIndex === 1) {
        chooseFiles(remaining);
      }
    }
  });
};

const chooseImages = async (count) => {
  try {
    const urls = await chooseAndUploadImages({
      count
    });
    const newItems = urls.map((url, idx) => ({
      type: 'image',
      url,
      displayName: `图片${formData.value.attachments.length + idx + 1}`
    }));
    formData.value.attachments = [...formData.value.attachments, ...newItems];
  } catch (error) {
    console.error('上传图片失败:', error);
    uni.showToast({
      title: error.message || '上传图片失败',
      icon: 'none'
    });
  }
};

const chooseFiles = async (count) => {
  const remainingCount = Math.min(count, ATTACHMENT_LIMIT - formData.value.attachments.length);
  if (remainingCount <= 0) {
    uni.showToast({
      title: `最多上传${ATTACHMENT_LIMIT}个附件`,
      icon: 'none'
    });
    return;
  }

  try {
    uni.chooseFile({
      count: remainingCount,
      success: async (res) => {
        try {
          uni.showLoading({
            title: '上传中...',
            mask: true
          });
          const uploadResults = await batchUploadFilesToQiniu(
            res.tempFiles.map(file => ({
              path: file.path,
              name: file.name
            }))
          );
          const newFiles = uploadResults.map(result => ({
            type: 'file',
            url: result.url,
            displayName: result.name || '附件'
          }));
          formData.value.attachments = [...formData.value.attachments, ...newFiles];
          uni.hideLoading();
          uni.showToast({
            title: '上传成功',
            icon: 'success'
          });
        } catch (error) {
          uni.hideLoading();
          console.error('上传文件失败:', error);
          uni.showToast({
            title: error.message || '上传失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('选择文件失败:', err);
        uni.showToast({
          title: '当前环境暂不支持选文件',
          icon: 'none'
        });
      }
    });
  } catch (error) {
    console.error('调用选择文件失败:', error);
    uni.showToast({
      title: '当前环境暂不支持选文件',
      icon: 'none'
    });
  }
};

const getAttachmentIcon = (file) => {
  if (file.type === 'image') {
    return '🖼️';
  }
  const name = file.displayName || '';
  const ext = name.includes('.') ? name.split('.').pop().toLowerCase() : '';
  const map = {
    pdf: '📕',
    doc: '📘',
    docx: '📘',
    xls: '📗',
    xlsx: '📗',
    ppt: '📙',
    pptx: '📙',
    zip: '📦',
    rar: '📦'
  };
  return map[ext] || '📄';
};

const removeAttachment = (index) => {
  formData.value.attachments.splice(index, 1);
};

const previewAttachment = (file) => {
  if (file.type === 'image') {
    const imageUrls = formData.value.attachments.filter(att => att.type === 'image').map(att => att.url);
    uni.previewImage({
      urls: imageUrls,
      current: file.url
    });
    return;
  }

  if (!file.url) {
    uni.showToast({
      title: '文件地址无效',
      icon: 'none'
    });
    return;
  }

  // #ifdef H5
  window.open(file.url, '_blank');
  // #endif

  // #ifdef APP-PLUS
  plus.runtime.openURL(file.url);
  // #endif

  // #ifndef H5 || APP-PLUS
  uni.downloadFile({
    url: file.url,
    success: (res) => {
      if (res.statusCode === 200) {
        uni.openDocument({
          filePath: res.tempFilePath
        });
      }
    }
  });
  // #endif
};

const openMemberModal = () => {
  selectedMemberIds.value = formData.value.members.map(member => member.userId);
  memberKeyword.value = '';
  showMemberModal.value = true;
};

const closeMemberModal = () => {
  showMemberModal.value = false;
};

const toggleMember = (userId) => {
  const index = selectedMemberIds.value.indexOf(userId);
  if (index >= 0) {
    selectedMemberIds.value.splice(index, 1);
  } else {
    selectedMemberIds.value.push(userId);
  }
};

const confirmMemberSelection = () => {
  const selected = memberOptions.value.filter(user => selectedMemberIds.value.includes(user.userId));
  formData.value.members = selected.map(user => ({
    userId: user.userId,
    userName: user.userName
  }));
  showMemberModal.value = false;
};

const removeMember = (userId) => {
  formData.value.members = formData.value.members.filter(member => member.userId !== userId);
};

const handleSubmit = async () => {
  if (submitting.value) {
    return;
  }

  if (!canSubmit.value) {
    uni.showToast({
      title: '请完善必填信息',
      icon: 'none'
    });
    return;
  }

  const payload = {
    id: null,
    projectId: formData.value.projectId,
    type: formData.value.type,
    level: formData.value.level,
    picture: formData.value.attachments.map(item => item.url).join(','),
    description: formData.value.description.trim(),
    expireTime: formData.value.expireTime,
    memberList: formData.value.members.map(member => ({
      userId: member.userId,
      userName: member.userName
    }))
  };

  submitting.value = true;
  uni.showLoading({
    title: '提交中...'
  });

  try {
    await createTask(payload);
    uni.hideLoading();
    uni.showToast({
      title: '创建成功',
      icon: 'success'
    });
    uni.$emit('taskListRefresh');
    setTimeout(() => {
      uni.navigateBack();
    }, 1200);
  } catch (error) {
    uni.hideLoading();
    submitting.value = false;
    console.error('创建任务失败:', error);
    uni.showToast({
      title: error.message || '创建失败，请重试',
      icon: 'none'
    });
  } finally {
    submitting.value = false;
  }
};

onLoad(async (options) => {
  if (options?.projectId) {
    presetProjectId.value = String(options.projectId);
  }
  loading.value = true;
  try {
    await dictStore.loadDictData();
    await Promise.all([loadProjects(), loadMembers()]);
  } finally {
    loading.value = false;
  }
});
</script>

<style scoped lang="scss">
.task-add-page {
  background: #f5f5f5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: 120rpx;
}

.content-scroll {
  flex: 1;
}

.form-card {
  margin: 16px;
  padding: 16px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.03);
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
  color: #333;
}

.form-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f2f2f2;
  gap: 12px;
}

.form-item:last-child {
  border-bottom: none;
}

.align-start {
  align-items: flex-start;
}

.form-label {
  width: 80px;
  font-size: 14px;
  color: #333;
}

.form-label.required::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}

.form-value {
  flex: 1;
  display: flex;
  align-items: center;
  min-height: 24px;
}

.value-text {
  color: #333;
  font-size: 14px;
}

.placeholder {
  color: #999;
  font-size: 14px;
}

.arrow {
  font-size: 18px;
  color: #999;
}

.textarea-input {
  flex: 1;
  min-height: 120px;
  padding: 12px;
  border-radius: 8px;
  background: #f8f9fb;
  font-size: 14px;
  color: #333;
  line-height: 1.6;
}

.pill-group {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.pill-item {
  padding: 6px 14px;
  border-radius: 20px;
  background: #f3f5f9;
  color: #666;
  font-size: 13px;
}

.pill-item.active {
  background: #2885ff;
  color: #fff;
}

.attachment-tip {
  font-size: 12px;
  color: #999;
  margin-bottom: 12px;
}

.attachment-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.attachment-item {
  width: calc(33.33% - 8px);
  min-width: 100px;
  height: 110px;
  background: #f6f7fb;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 12px;
  gap: 6px;
  position: relative;
}

.attachment-item.add-item {
  border: 1px dashed #d1d8e6;
  background: #fff;
  color: #2885ff;
}

.add-icon {
  font-size: 28px;
}

.add-text {
  font-size: 13px;
}

.file-icon {
  font-size: 26px;
}

.file-name {
  font-size: 12px;
  color: #333;
  text-align: center;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.remove-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.4);
  color: #fff;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.responsible-content {
  flex: 1;
}

.selected-members {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.member-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: #eef4ff;
  border-radius: 16px;
  font-size: 12px;
  color: #2885ff;
}

.chip-remove {
  font-size: 14px;
}

.picker-trigger {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #2885ff;
  font-size: 14px;
}

.submit-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 16px;
  background: #fff;
  border-top: 1px solid #eee;
}

.member-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 99;
}

.modal-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
}

.modal-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;

  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid #f2f2f2;
}

.modal-title {
  font-size: 16px;
  font-weight: 600;
}

.modal-close {
  font-size: 20px;
  color: #999;
}

.search-box {
  padding: 12px 16px;
  border-bottom: 1px solid #f2f2f2;
}

.search-input {
  background: #f5f6f8;
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 14px;
}

.member-list {
  height: 600px;

}

.member-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid #f5f5f5;
}

.member-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.member-name {
  font-size: 14px;
  color: #333;
}

.member-dept {
  font-size: 12px;
  color: #999;
}

.select-indicator {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}

.select-indicator.active {
  background: #2885ff;
  border-color: #2885ff;
}

.modal-actions {
  display: flex;
  gap: 12px;
  padding: 12px 16px 24px;
}

.empty-tip {
  padding: 40px 0;
  text-align: center;
  color: #999;
  font-size: 14px;
}
</style>

