<template>
  <div class="task-demand-content">
    <TableForm
      v-model:data="demandInfo"
      :show-disabled="true"
      ref="tableFormRef"
      tableName="SIMULATION_TASK_DEMAND_CREATE"
      :colNum="2"
      :hideKeys="['attachments']"
    >
      <template #form-downAttachments>
        <div class="attachments">
          <div class="file" v-for="item in attachmentFiles" :key="item.id">
            <span>{{ item.name }}</span>
            <el-icon size="18" @click="downloadFileById(item.id)"><Download /></el-icon>
          </div>
        </div>
      </template>
      <template #form-pMemberList>
        <el-select disabled v-model="demandInfo.pMemberList" filterable placeholder="请选择">
          <el-option
            v-for="item in deptOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </template>
    </TableForm>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue';
import TableForm from '@/components/common/table/tableForm.vue';
import { getDemandDetailApi } from '@/api/project/demand';
import { getMemberListIds } from '@/utils/task';
import { dataQueryDirApi } from '@/api/data/data';
import { downloadFileById } from '@/utils/file';
import { CommonStore } from '@/stores/common';
import { listDeptApi } from '@/api/system/departMent';

const props = defineProps<{
  demandUid: string;
}>();

const commonStore = CommonStore();
const demandInfo = ref<any>({});
const tableFormRef = ref();

const getDemandInfoFun = async () => {
  const res: any = await getDemandDetailApi({ demandId: props.demandUid });
  if (res.code === 200) {
    demandInfo.value = {
      ...res.data,
      planTime: [res.data.beginTime, res.data.endTime],
      pMemberList: getMemberListIds(res.data.pMemberList),
      aMemberList: getMemberListIds(res.data.aMemberList),
      tMemberList: getMemberListIds(res.data.tMemberList),
    };
    tableFormRef.value.setOptionsFun('phaseId', [
      { label: res.data.phaseName, value: res.data.phaseId },
    ]);
    const simTypeList: any = commonStore.getDictData(res.data.demandType);
    tableFormRef.value.setOptionsFun('simType', simTypeList.A);
  }
};

const attachmentFiles = ref<any[]>([]);
const getAttachmentsFun = async () => {
  const res: any = await dataQueryDirApi({ uuid: props.demandUid, current: 1, size: 99 });
  attachmentFiles.value =
    res.data?.data?.map((item: { originalName: any; fileSize: any; id: number }) => {
      return {
        name: item.originalName,
        size: item.fileSize,
        id: item.id,
      };
    }) || [];
};

const deptOptions = ref<any[]>([]);
const getListDeptFun = async () => {
  const res: any = await listDeptApi({ current: 1, size: 999 });
  if (res.code === 200) {
    deptOptions.value = res.data.data.map((item: { deptName: any; userId: any }) => {
      return {
        label: item.deptName,
        value: String(item.userId),
      };
    });
  }
};

watch(
  () => props.demandUid,
  (val) => {
    if (val) {
      getDemandInfoFun();
      getAttachmentsFun();
      getListDeptFun();
    }
  },
  { immediate: true }
);
</script>

<style lang="scss" scoped>
.task-demand-content {
  width: 100%;
  height: 100%;
  padding: 10px;
  overflow-y: auto;
}

.attachments {
  width: 100%;
  height: 100px;
  overflow-y: auto;

  .file {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-icon {
      color: var(--el-color-primary);
      cursor: pointer;
    }
  }
}
</style>
