<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="130px"
    >
      <el-form-item label="项目编号" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="生产计划号" prop="taskNo">
        <el-input
          v-model="queryParams.taskNo"
          placeholder="请输入生产计划号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
	  <el-form-item label="子项目名称" prop="projectSubName">
	    <el-input
	      v-model="queryParams.projectSubName"
	      placeholder="请输入子项目名称"
	      clearable
	      @keyup.enter="handleQuery"
	      class="!w-240px"
	    />
	  </el-form-item>
	  <el-form-item label="零件名称" prop="materialName">
	    <el-input
	      v-model="queryParams.materialName"
	      placeholder="请输入零件名称"
	      clearable
	      @keyup.enter="handleQuery"
	      class="!w-240px"
	    />
	  </el-form-item>
	  <el-form-item label="任务负责人" prop="owner">
	    <!-- <el-input
	      v-model="queryParams.owner"
	      placeholder="请输入零件名称"
	      clearable
	      @keyup.enter="handleQuery"
	      class="!w-240px"
	    /> -->
		<UserSelect v-model="queryParams.owner"  @update:newValue="handleSelectedUser1" class="!w-265px"/>

	  </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>

        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['heli:shenhe:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>

    <el-table v-loading="loading" :data="list"  :show-overflow-tooltip="true" border class="hl-table" :row-class-name="tableRowClassName">
		  <el-table-column type="index" min-width="60" fixed label="序号" align="center" />



      <el-table-column label="项目编号" align="center" prop="projectCode" width="240px" />
	  <el-table-column label="项目名称" align="center" prop="projectName" width="240px"/>
      <el-table-column label="报工类型" align="center" prop="planType" width="160px">
        <template #default="scope">
         {{scope.row.type=='ASSEMBLE'?"装配任务派工单":"生产任务派工单"}}
        </template>
      </el-table-column>
	  <el-table-column label="生产计划号" align="center" prop="taskNo" width="240px"/>
	  <el-table-column label="生产状态" align="center" prop="planType" >
		  <template #default="scope">
		    <dict-tag :type="DICT_TYPE.HELI_PLANTYPE" :value="scope.row.planType" />
		  </template>
	  </el-table-column>
	  <el-table-column label="子项目名称" align="center" prop="projectSubName" />
	  <el-table-column label="零件名称" align="center" prop="materialName" />
	  <el-table-column label="工序名称" align="center" prop="procdureName" />
	  <el-table-column label="任务负责人" align="center" prop="owner" >
		  <template #default="scope">
		    {{ userList.find((user) => user.id === scope.row.owner)?.nickname }}
		  </template>
	  </el-table-column>
      <el-table-column label="预计结束日期" align="center" prop="endTime">
        <template #default="scope">
          {{  formatDateT(scope.row.endTime)}}
        </template>
      </el-table-column>
      <el-table-column label="总预计工时" align="center" prop="workTime" />
	  <el-table-column label="总派工数量" align="center" prop="amount" />
	  <el-table-column label="总报工工时" align="center" prop="bgWorkTime" />
	  <el-table-column label="总报工数量" align="center" prop="bgAmount" />
	  <el-table-column label="完成标志" align="center" prop="wgType">

		  <template #default="scope">
		    <dict-tag :type="DICT_TYPE.HELI_PG_PLANTYPE" :value="1" v-if="scope.row.wgType==2"/>
		    <dict-tag :type="DICT_TYPE.HELI_PG_PLANTYPE" :value="0" v-else/>
		  </template>
	  </el-table-column>

    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->

</template>

<script setup lang="ts">
import download from '@/utils/download'
import * as PlanSchedule from '@/api/heli/planSchedule'
import { getIntDictOptions, getStrDictOptions, DICT_TYPE, getDictLabel } from '@/utils/dict'
	import UserSelect from '@/views/heli/hlvuestyle/userSelect.vue'
import * as UserApi from '@/api/system/user'

defineOptions({ name: 'Shenhe' })
const userList = ref<UserApi.UserVO[]>([]) // 用户列表
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  projectCode: undefined,
  projectName: undefined,
  taskNo: undefined,
  projectSubName: undefined,
  materialName: undefined,
  owner: undefined,


})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const handleSelectedUser1 = (newValue: any) => {
  queryParams.owner = newValue?.id
}
const formatDateT = (timestamp) => {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`; // 返回 YYYY-MM-DD 格式
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PlanSchedule.getPlanBb(queryParams)
    list.value = data.list
	for(var i=0;i<list.value.length;i++){
		if(list.value[i].bgAmount>0&&list.value[i].bgAmount<list.value[i].amount){
			//判断生产中
			list.value[i].planType = 1;
			// list.value[i].wgType = 0;
		}else if(list.value[i].bgAmount>0&&list.value[i].bgAmount>=list.value[i].amount){
			//判断生产完成
			list.value[i].planType = 2;
			// list.value[i].wgType = 1;
		}else{
			//判断未生产
			list.value[i].planType = 0;
			// list.value[i].wgType = 0;
		}
	}
	//list.value[0].shenheUser = 360
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const tableRowClassName = ({ row }) => {
  var endTime = new Date(row.endTime);

// 获取当前时间的 Date 对象
  var currentTime = new Date();

// 将 endTime 和 currentTime 的时间部分设置为零
  endTime.setHours(0, 0, 0, 0);
  currentTime.setHours(0, 0, 0, 0);
  if (row.wgType!=2&&endTime < currentTime){
    return 'warning-row';
  }else {
    return ''
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()




/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ShenheApi.exportShenhe(queryParams)
    download.excel(data, '审核.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
	loading.value = true
  await getList()
  userList.value = await UserApi.getSimpleUserList()
})
</script>
<style>

.warning-row {
  background-color: #D3D3D3 !important;
}

</style>
