<template>
  <el-card class="hl-card">
    <template #header>
      <span>设计进度一览</span>
    </template>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="110px"
      >
        <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="customerName">
          <el-input
            v-model="queryParams.customerName"
            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="projectSubName">
          <el-input
            v-model="queryParams.projectSubName"
            placeholder="请输入子项目名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="2D负责人" prop="twoDimOwnerName">
          <el-input
            v-model="queryParams.twoDimOwnerName"
            placeholder="请输入2D负责人"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="3D负责人" prop="threeDimOwnerName">
          <el-input
            v-model="queryParams.threeDimOwnerName"
            placeholder="请输入3D负责人"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </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>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" class="hl-table">
        <el-table-column fixed label="序号" type="index" width="60" align="center" />
        <el-table-column fixed label="项目编号" align="center" prop="projectCode" min-width="220" />
        <el-table-column fixed label="生产状态" align="center" prop="status" width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_PROJECT_PLAN_STATUS" :value="scope.row.planStatus" />
          </template>
        </el-table-column>
        <el-table-column fixed label="客户名称" align="center" prop="customerName" min-width="240" />
        <el-table-column fixed label="项目名称" align="center" prop="projectName" min-width="180" />
        <el-table-column fixed label="子项目名称" align="center" prop="projectSubName" min-width="180" />
        <el-table-column label="毛坯负责人" align="center" prop="blankOwnerName"  width="120"/>
        <el-table-column label="毛坯最新进度" align="center" prop="progressBlank" min-width="140" >
        <!-- 使用 scoped slots 自定义单元格渲染 -->
        <template #default="{ row }">
          <div
            :style="getCellStyle(row.progressBlank)"
         >
            {{ row.progressBlank }}
          </div>
        </template>
        </el-table-column>
        <el-table-column label="毛坯计划截止日期" align="center" prop="blankDate" :formatter="dateFormatter2"  min-width="180"/>
        <el-table-column label="毛坯是否延期" align="center" prop="blankDeferred" min-width="140"/>
        <el-table-column label="延期备注" align="center" prop="blankDelayReason" min-width="140"/>
<!--          <template #default="scope">-->
<!--            <span v-if="scope.row.blankDate && (new Date().getTime() - 86400000 - new Date(formatDate(scope.row.blankDate, 'YYYY-MM-DD')).getTime() >= 0 && (!scope.row.progressBlank || parseFloat(scope.row.progressBlank) < 100))" style="color: red;">是</span>-->
<!--            <span v-else>否</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="3D负责人" align="center" prop="threeDimOwnerName" min-width="120"/>
        <el-table-column label="3D最新进度" align="center" prop="progress3d" min-width="140" >
        <!-- 使用 scoped slots 自定义单元格渲染 -->
        <template #default="{ row }">
          <div
            :style="getCellStyle(row.progress3d)"
          >
            {{ row.progress3d }}
          </div>
        </template>
        </el-table-column>
        <el-table-column label="3D计划截止日期" align="center" prop="threeDimDate" :formatter="dateFormatter2" width="180"/>
        <el-table-column label="3D是否延期" align="center" prop="deferred3d" min-width="140"/>
        <el-table-column label="延期备注" align="center" prop="delayReason3d" min-width="140"/>
<!--          <template #default="scope">-->
<!--            <span v-if="scope.row.threeDimDate && (new Date().getTime() - 86400000 - new Date(formatDate(scope.row.threeDimDate, 'YYYY-MM-DD')).getTime() >= 0 && (!scope.row.progress3d || parseFloat(scope.row.progress3d) < 100))" style="color: red;">是</span>-->
<!--            <span v-else>否</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="2D负责人" align="center" prop="twoDimOwnerName"  min-width="120"/>
        <el-table-column label="2D最新进度" align="center" prop="progress2d" min-width="140">
        <!-- 使用 scoped slots 自定义单元格渲染 -->
        <template #default="{ row }">
          <div
            :style="getCellStyle(row.progress2d)"
          >
            {{ row.progress2d }}
          </div>
        </template>
        </el-table-column>
        <el-table-column label="2D计划截止日期" align="center" prop="twoDimDate" :formatter="dateFormatter2" min-width="180"/>
        <el-table-column label="2D是否延期" align="center" prop="deferred2d" min-width="140"/>
        <el-table-column label="延期备注" align="center" prop="delayReason2d" min-width="140"/>
<!--          <template #default="scope">-->
<!--            <span v-if="scope.row.twoDimDate && (new Date().getTime() - 86400000 - new Date(formatDate(scope.row.twoDimDate, 'YYYY-MM-DD')).getTime() >= 0 && (!scope.row.progress2d || parseFloat(scope.row.progress2d) < 100))" style="color: red;">是</span>-->
<!--            <span v-else>否</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </el-card>
</template>

<script setup lang="ts">
import {dateFormatter, dateFormatter2, formatDate} from '@/utils/formatTime'
import download from '@/utils/download'
import * as ProcessDesignApi from '@/api/heli/processdesign'
import {useCommonStateWithOut} from "@/store/modules/common";
import {DICT_TYPE} from "@/utils/dict";

defineOptions({ name: 'ProcessDesign3D' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const commonStore = useCommonStateWithOut()

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  planCode: undefined,
  projectCode: undefined,
  processDesignTypeList: ['BLUEPRINT_3D','BLUEPRINT_2D','BLUEPRINT_WORKBLANK'],
  customerName: undefined,
  projectName: undefined,
  businessLine: undefined,
  property: undefined,
  projectSubName: undefined,
  threeDimOwnerName: undefined,
  twoDimOwnerName: undefined,
  isOverview: true
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProcessDesignApi.getProcessDesignPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

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

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProcessDesignApi.deleteProcessDesign(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProcessDesignApi.exportProcessDesign(queryParams)
    download.excel(data, '工艺设计.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
// 定义样式计算函数
const getCellStyle = (status) => {
  switch (status) {
    case '已完成':
      return { backgroundColor: '#f2f2f2' } // 浅灰色
    case '进行中':
      return { backgroundColor: '#90EE90' } // 浅绿色
    default:
      return {} // 未开始保持透明背景
  }
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
