<template>
  <el-card class="hl-card">
    <template #header>
      <span>2D图纸进度上报</span>
    </template>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="110px">
        <el-form-item label="计划单号" prop="planCode">
          <el-input v-model="queryParams.planCode" placeholder="请输入计划单号" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>
        <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="businessLine">
          <el-select clearable class="!w-240px" v-model="queryParams.businessLine" placeholder="下拉选择">
            <el-option v-for="dict in getStrDictOptions(DICT_TYPE.HELI_BUSINESS_LINE)" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="性质" prop="property">
          <el-select clearable class="!w-240px" v-model="queryParams.property" placeholder="下拉选择">
            <el-option v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PROJECT_PROPERTY)" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </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="子项目名称" prop="projectSubName">
          <el-input v-model="queryParams.projectSubName" placeholder="请输入子项目名称" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>
        <el-form-item style="margin-left:25px">
          <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 label="项目名称" align="center" prop="projectName" min-width="180" />
        <el-table-column label="子项目名称" align="center" prop="projectSubName" min-width="180" />
        <el-table-column label="性质" align="center" prop="property" min-width="120">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_PROJECT_PROPERTY" :value="scope.row.property" />
          </template>
        </el-table-column>
         <el-table-column  label="计划单号" align="center" prop="planCode" min-width="180">
          <template #default="scope">
            <el-button link type="primary" @click="openDetail('detail', scope.row.id)">
              {{ scope.row.planCode }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="子项目编号" align="center" prop="projectSubCode" min-width="350" />
        <el-table-column label="所属业务线" align="center" prop="businessLine" min-width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_BUSINESS_LINE" :value="scope.row.businessLine" />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="2D负责人" align="center" prop="twoDimOwnerName" width="120" />
        <el-table-column fixed="right" label="2D截止日期" align="center" prop="twoDimDate" :formatter="dateFormatter2" min-width="140" />
        <el-table-column fixed="right" label="完成情况" align="center" prop="progress" min-width="120">
          <template #default="scope">
            {{getDetailMsg(scope.row)}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="最新进度" align="center" prop="progress" min-width="120">
          <template #default="scope">
            {{scope.row.isOverProcess == 1 ? '已完成':'实施中'}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" min-width="160">
          <template #default="scope">
            <el-button link type="primary" @click="openDetail('update', scope.row.id)">
              编辑
            </el-button>
            <el-button link type="primary" @click="openDetail('detail', scope.row.id)">
              查看详情
            </el-button>
          </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 { getStrDictOptions, DICT_TYPE, getDictLabel, getIntDictOptions } from '@/utils/dict'
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 {useUserStore} from "@/store/modules/user";

defineOptions({ name: 'ProcessDesign2D' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const commonStore = useCommonStateWithOut()
const userStore = useUserStore()
const username = userStore.getUser.nickname

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  planCode: undefined,
  projectCode: undefined,
  processDesignType: 'BLUEPRINT_2D',
  customerName: undefined,
  projectName: undefined,
  businessLine: undefined,
  property: undefined,
  projectSubName: undefined,
  twoDimOwnerName: username
})
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 openDetail = (type: string, id?: number) => {
  commonStore.setStore('active', type)
  commonStore.setStore('id', id)
  commonStore.setStore('processDesignType', 'BLUEPRINT_2D')
  router.push({
    name: 'ProcessDesignDetail',
    query: {
      operateId: Math.random()
    }
  })
}
const getDetailMsg = (row) =>{
  if(row.isOverProcess){
    //完结的，按计划完成了还是没按计划完成
     if(row.twoDimDate != null && row.lastDate != null ){
        const starttemp = new Date(row.twoDimDate);
        starttemp.setHours(23,59,59,999);
        const start =starttemp.getTime()
        const end = new Date(row.lastDate).getTime();
        const diffTime = Math.abs(end - start);
        var day =  Math.floor(diffTime / (1000 * 60 * 60 * 24));
        if(day == 0){
          if(row.isDelay == 1 &&  row.lastDate > row.blankDate){
            return  '延后' + (day+1) +'天';
          }
          return '按时完成'
        }else{
            if(row.lastDate > row.blankDate){
            return  '延后' + day +'天';
          }else if (row.lastDate < row.blankDate){
            return '提前' + day +'天';
          }else{
            return '按时完成'
          }
        }

    }else{
      return '实施中';
    }


  }
 
}
/** 删除按钮操作 */
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
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
