<template>
  <Dialog custom-show type="xl" title="任务跟踪列表" hide-footer @close="closeDialog">
    <transition name="fade">
      <div v-show="showSearch" class="mb-[20px]">
        <el-form ref="queryFormRef" :model="queryParams">
          <el-row :gutter="20">
            <!-- 第一行 -->
            <el-col :span="6">
              <el-form-item label="任务ID" prop="task_id">
                <el-input v-model="queryParams.task_id" placeholder="请输入任务ID" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="事件代码" prop="event_code">
                <el-input v-model="queryParams.event_code" placeholder="请输入事件代码" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="排序依据" prop="sortBy">
                <el-select v-model="queryParams.sortBy" placeholder="选择排序依据" clearable>
                  <el-option label="创建时间" value="creation_time"></el-option>
                  <el-option label="更新时间" value="update_time"></el-option>
                  <el-option label="任务ID" value="task_id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="排序顺序" prop="sortOrder">
                <el-select v-model="queryParams.sortOrder" placeholder="选择排序顺序" clearable>
                  <el-option label="升序" value="asc"></el-option>
                  <el-option label="降序" value="desc"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" class="mt-[10px]">
            <el-col :span="6">
              <div class="flex">
                <div class="common-btn-primary" @click="handleQuery">搜索</div>
                <div class="common-btn" @click="resetQuery">重置</div>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </transition>
    <!-- 表格组件 -->
    <div class="common-table">
      <div class="table-header">
        <div class="td">任务ID</div>
        <div class="td">单位名称</div>
        <div class="td">任务描述</div>
        <div class="td">登记人</div>
        <div class="td">创建时间</div>
        <div class="td">更新时间</div>
        <div class="td">处理状态</div>
        <div class="td">操作</div>
      </div>
      <div v-for="(item, index) in taskList" :key="index" class="tr">
        <div class="td">{{ item.task_id }}</div>
        <div class="td">{{ item.unit_name }}</div>
        <div class="td">{{ item.task_description }}</div>
        <div class="td">{{ item.registrar }}</div>
        <div class="td">{{ formatDateTime(item.creation_time) }}</div>
        <div class="td">{{ formatDateTime(item.update_time) }}</div>
        <div class="td">
          <div :class="item.processing_status === '已完成' ? 'success-bg' : 'processing-bg'">{{ item.processing_status }}</div>
        </div>
        <div class="td">
          <div class="btn" @click="openUpdateDialog(item)">更新</div>
        </div>
      </div>
    </div>
    <div class="footer">
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.page_size"
        :total="total"
        layout="total, prev, pager, next"
        @pagination="getList"
      />
    </div>
  </Dialog>
  <RenWuGengXin
    v-model="newSectionState.showListDialog"
    :task="selectedTask"
    :event-id="eventId"
    title="任务进度更新"
    @update-success="getList"
  />
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { selectTask } from '@/api/emergencyCommandMap/JointDuty';
import RenWuGengXin from '@/views/emergencyCommandMap/RightSection/RenWuGengXin.vue';

const taskList = ref([]);
const total = ref(0);
const queryParams = reactive({
  page: 1,
  page_size: 10,
  task_id: '',
  event_code: '',
  sortBy: '',
  sortOrder: ''
});

const getList = () => {
  if (props.eventId) {
    queryParams.event_code = props.eventId; // 使用 eventId
  }

  selectTask(queryParams).then((res) => {
    if (res.code === 200) {
      taskList.value = res.data;
      total.value = res.total;
    }
  });
};
const formatDateTime = (dateTimeString: string) => {
  const date = new Date(dateTimeString);
  return date.toLocaleString();
};

const handleQuery = () => {
  queryParams.page = 1;
  getList();
};

const resetQuery = () => {
  Object.assign(queryParams, {
    page: 1,
    page_size: 10,
    task_id: '',
    event_code: '',
    sortBy: '',
    sortOrder: ''
  });
  getList();
};

// 关闭事件
const closeDialogState = reactive({
  show: false,
  form: {
    eventId: '',
    deaths: '',
    injuries: '',
    missing: '',
    fileNames: []
  }
});

// 选中的任务
const selectedTask = ref({
  task_id: '',
  sortBy: 'creation_time',
  pageNum: 2,
  pageSize: 20
});
const newSectionState = reactive({
  showListDialog: false
});

const openUpdateDialog = (task) => {
  selectedTask.value = { ...task };
  newSectionState.showListDialog = true;
};
const props = defineProps<{
  modelValue?: boolean;
  title?: string;
  height?: string;
  eventId?: string;
}>();
const emit = defineEmits(['update:modelValue']);

// 你可以在这里使用 props.eventId，例如在 fetchData 函数中：
const fetchData = async () => {
  if (props.eventId) {
    try {
      const res = await selectTask({ event_code: props.eventId });
      // 处理 res ...
    } catch (error) {
      console.error('请求任务数据失败:', error);
    }
  }
};

watch(
  () => props.modelValue,
  (newValue) => {
    if (newValue) {
      getList();
    }
  },
  {
    immediate: true
  }
);

// 关闭弹窗
const closeDialog = () => {
  emit('update:modelValue', false);
};
</script>

<style lang="scss" scoped>
.dialog-wrap {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index: 2000;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  .overlay {
    background-color: rgba(0, 0, 0, 0.5);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    cursor: pointer;
  }

  .dialog {
    width: 5000px;
    height: 2000px;
    margin: 0 auto;
    background-color: #fff;
    border-radius: 20px;
  }
}

.dialog {
  padding: 0 50px;

  .dialog-header {
    width: 100%;
    height: 150px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .icon-close {
      cursor: pointer;
    }
  }
}

.footer {
  height: 64px;
  display: flex;
  justify-content: flex-end;
  margin-bottom: 30px;

  .pagination-container {
    height: 64px;
    margin: 0;
  }

  :deep(.el-pagination__total) {
    color: #a7ccdf;
    font-size: 32px;
  }

  :deep(.el-pagination) {
    .btn-next,
    .btn-prev {
      background-color: transparent;
      border: none;

      .el-icon {
        font-size: 22px;
        color: #a7ccdf;
      }
    }

    .btn-prev:disabled,
    .btn-next:disabled {
      background-color: transparent;
      border: none;
    }

    .el-pager li {
      width: 64px;
      height: 64px;
      line-height: 64px;
      text-align: center;
      font-size: 32px;
      color: #a7ccdf;
      background-color: #0e3064;
      border: 1px solid #0c57a7;
      margin: 0 6px;

      &:hover {
        background-color: #038dff;
        border: 1px solid #038dff;
      }
    }

    .el-pager li.is-active {
      background-color: #038dff;
      border: 1px solid #038dff;
    }
  }
}

.flex {
  display: flex;
  align-items: flex-start;
}
.success-bg,
.processing-bg,
.btn {
  width: 154px;
  height: 56px;
  line-height: 56px;
  font-size: 32px;
  color: #ffffff;
  border-radius: 10px;
  text-align: center;
  margin-left: 30px;
}
.btn {
  background-color: #247dff;
  cursor: pointer;
}
</style>
