<template>
  <Dialog :title="dialogTitle" width="80%" v-model="dialogVisible" center>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="108px">
        <el-form-item label="负责人" prop="twoDimOwner">
          <el-select class="!w-265px" v-model="queryParams.twoDimOwner" clearable filterable>
            <el-option
              v-for="dict in userInit"
              :key="dict.id"
              :label="dict.nickname"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="beginTime">
          <el-date-picker
            v-model="queryParams.beginTime"
            value-format="YYYY-MM-DD"
            type="date"
            clearable
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            class="!w-280px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary">
            <Icon icon="ep:search" class="mr-5px" /> 搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" /> 重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <el-card class="hl-card">
      <!-- 列表 -->
      <ContentWrap>
        <el-table ref="multipleTable" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
          class="hl-table" @selection-change="handleSelectionChange" @row-click="clickRow" selection>
          <el-table-column fixed label="序号" type="index" width="70" align="center" />
          <el-table-column fixed label="项目编码" align="center" prop="code" min-width="130" />
          <el-table-column fixed label="项目名称" align="center" prop="projectName" min-width="180" />
          <el-table-column fixed label="客户简称" align="center" prop="customerBrief" min-width="100" />
          <el-table-column fixed label="子项目名称" align="center" prop="name" min-width="180" />
          <el-table-column label="设计类型" align="center" prop="type" width="100px">
            <template #default="scope">
              {{ scope.row.subType=='BLUEPRINT_WORKBLANK'?"毛坯":scope.row.subType=='BLUEPRINT_2D'?"2D":"3D" }}
            </template>
          </el-table-column>
          <el-table-column label="负责人" align="center" prop="creator" min-width="120" >
            <template #default="scope">
              {{ userInit.find((user) => user.id == scope.row.twoDimOwner)?.nickname }}
            </template>
          </el-table-column>
          <el-table-column label="开始日期" align="center" prop="startTwoDimDate" min-width="100" :formatter="dateFormatter2"/>
          <el-table-column label="结束日期" align="center" prop="twoDimDate" min-width="100" :formatter="dateFormatter2"/>
          <el-table-column label="设计天数" align="center" prop="designNum" min-width="100" />
          <el-table-column label="时间段顺序" align="center" prop="seqNo" min-width="130" />
        </el-table>
        <!-- 分页 -->
        <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
          @pagination="getList" />
      </ContentWrap>
    </el-card>
    <template #footer>
      <el-button @click="dialogVisible = false" size="large">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import {
  dateFormatter,
  dateFormatter2,
  dateFormatter3,
  formatDate,
  formatDate1
} from '@/utils/formatTime'
import * as ProjectOrderApi from '@/api/heli/projectorder'
import { ref } from "vue";
import { ElTable } from 'element-plus'

import { ProjectOrderVO } from "@/api/heli/projectorder";
import * as UserApi from "@/api/system/user";
import * as PlansubdetailApi from "@/api/heli/plansubdetail";
const userInit = ref()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  twoDimOwner: undefined,
  beginTime: undefined,

})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    if (queryParams.beginTime) {
      queryParams.startTime = queryParams.beginTime + ' 00:00:00'
    }
    const data = await PlansubdetailApi.getPlanSubDetailPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  if (!queryParams.twoDimOwner) {
    // 使用 setTimeout 避免渲染过程中直接调用 message.error
    setTimeout(() => {
      message.error('负责人为空,请确认')
    }, 100)
    return // 添加 return 防止继续执行
  }
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.twoDimOwner=''
  queryParams.beginTime=''
  handleQuery()
}

const multipleTable:any = ref<InstanceType<typeof ElTable>>()
const multipleSelection:any = ref([])

const handleSelectionChange = (val: ProjectOrderVO[]) => {
  if (val.length > 1) {
    multipleTable.value.clearSelection()
    multipleTable.value.toggleRowSelection(val.pop())
  } else {
    multipleSelection.value = val.pop()
  }

}

const clickRow = (row: any) => {
 if(row){
   multipleTable.value!.toggleRowSelection(row, undefined)
 }else{
  multipleTable.value!.clearSelection()
 }
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  userInit.value = await UserApi.getDeptName('设计部')
  // handleQuery()
  queryParams.twoDimOwner=''
  queryParams.beginTime=''
  list.value=[]
  total.value=0
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

