<template>
  <el-card class="hl-card">
    <template #header>
      <span>下料报工记录</span>
    </template>
    <ContentWrap class="fixed-search">
      <!-- 搜索工作栏 -->
      <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="110px"
      >
        <el-form-item label="报工日期" prop="endTime">
          <el-date-picker
              v-model="queryParams.endTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              class="!w-280px"
              :clearable="false"
          />
        </el-form-item>
        <el-form-item label="项目编号" prop="projectCode">
          <el-input
              v-model="queryParams.projectCode"
              placeholder="请输入项目编号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-280px"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input
              v-model="queryParams.projectName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-280px"
          />
        </el-form-item>
        <el-form-item label="子项目名称" prop="projectSubName">
          <el-input
              v-model="queryParams.projectSubName"
              placeholder="请输入子项目名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-280px"
          />
        </el-form-item>
        <el-form-item label="零件名称" prop="materialName">
          <el-input
              v-model="queryParams.materialName"
              placeholder="请输入零件名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-280px"
          />
        </el-form-item>
        <el-form-item label="报工人" prop="OwnerName">
          <el-input
              v-model="queryParams.ownerName"
              placeholder="请输入报工人名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-280px"
          />
        </el-form-item>
        <el-form-item style="margin-left: 20px">
          <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-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['heli:project-order: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" class="hl-table" height="500" border>
        <el-table-column fixed type="index" width="100" label="序号" align="center"/>
        <el-table-column fixed label="项目编号" align="center" prop="projectCode" width="180"/>
        <el-table-column fixed label="项目名称" align="center" prop="projectName" width="180"/>
        <el-table-column fixed label="子项目名称" align="center" prop="projectSubName" width="180"/>
        <el-table-column label="零件名称" align="center" prop="materialName" width="180"/>
        <el-table-column label="报工人" align="center" prop="ownerName" width="100"/>
        <el-table-column label="报工状态" align="center" prop="reportProcess" width="100">
          <template #default="scope">
              <span :style="{
              color: TaskInReportApi.getReportStatusColor(scope.row.reportProcess),
              fontWeight: 'bold'
              }"
              >
            {{ TaskInReportApi.getReportStatus({value: scope.row.reportProcess}) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="报工工序" align="center" prop="procedureName" width="160"/>
        <el-table-column label="下料类型" align="center" prop="matType" width="100">
          <template #default="scope">
            <span>{{ TaskInReportApi.getMatType({value: scope.row.matType}) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="长（直径）(mm)" align="center" prop="length" :formatter="formatAmount" width="220"/>
        <el-table-column label="宽(mm)" align="center" prop="widht" :formatter="formatAmount" width="160"/>
        <el-table-column label="高(mm)" align="center" prop="hight" :formatter="formatAmount" width="160"/>
        <el-table-column label="密度(t/m³)" align="center" prop="density" :formatter="formatAmount" width="160"/>
        <el-table-column label="重量(kg)" align="center" prop="weight" :formatter="formatAmount" width="160"/>
        <el-table-column label="总价(元)" align="center" prop="reportPrice" :formatter="formatAmount" width="160"/>
        <el-table-column label="报工日期" align="center" prop="reportTime" :formatter="dateFormatter2" width="120"/>
        <el-table-column label="修改人" align="center" prop="updaterName" width="160"/>
        <el-table-column label="修改时间" align="center" prop="updateTime" :formatter="dateFormatter2" width="120"/>
        <el-table-column label="修改原因" align="center" prop="remark" width="160"/>
        <el-table-column fixed="right" label="操作" align="center" width="160">
          <template #default="scope">
            <el-button
                link
                type="primary"
                @click="openForm('update', scope.row.id)"
                v-hasPermi="['heli:task-in-report:update']"
            >
              编辑
            </el-button>
            <el-button
                v-if="scope.row.reportProcess == 2"
                link
                type="primary"
                @click="rework('update', scope.row.id)"
            >
              返工
            </el-button>
            <el-button
                v-if="scope.row.reportProcess != 2"
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['heli:task-in-report:delete']"
            >
              删除
            </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>
  <!-- 表单弹窗：添加/修改 -->
  <TaskInReportForm ref="formRef" @success="getList"/>
  <reworkForm ref="reworkRef" @success="getList"/>
</template>

<script setup lang="ts">
import {getIntDictOptions, DICT_TYPE} from '@/utils/dict'
import {dateFormatter2} from '@/utils/formatTime'
import {formatAmount} from '@/utils/formatter'
import download from '@/utils/download'
import * as TaskInReportApi from '@/api/heli/taskinreport'
import TaskInReportForm from './TaskInReportForm.vue'
import dayjs from "dayjs";

import {Setting} from "@element-plus/icons-vue";
import ElTooltip from "element-plus/es/components/tooltip";
import {method} from "lodash-es";

defineOptions({name: 'TaskReport'})

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,
  projectSubName: undefined,
  materialName: undefined,
  dispatchDetailId: undefined,
  owner: undefined,
  amount: undefined,
  startTime: [],
  endTime: [],
  reportTime: [],
  hasReport: undefined,
  status: undefined,
  createTime: [],
  ownerName: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中


const formData = ref({
  id: undefined,
  dispatchDetailId: undefined,
  owner: undefined,
  amount: undefined,
  reportTime: undefined,
  length: undefined,
  hasReport: undefined,
  status: undefined,
  workType: undefined,
  remark: undefined,
  widht: undefined as number | undefined,
  hight: undefined as number | undefined,
  density: undefined as number | undefined,
  weight: undefined as number | undefined,
  price: undefined as number | undefined,
  reportPrice: undefined as number | undefined,
  matType: undefined,
  formalId: undefined,
  calFormal: undefined,
})

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

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

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

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

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TaskInReportApi.deleteTaskInReport(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {
  }
}
// 设置默认时间为当天
const setDefaultDate = () => {
  queryParams.endTime = [
    dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
    dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')
  ]
}
/** 导出按钮操作 */
const handleExport = async () => {
  try {
    if (!list.value || list.value.length == 0) {
      message.warning(t('common.selectText'))
      return
    }
    let idList = [];
    list.value.forEach(p => {
      idList.push(p.id)
    })
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await TaskInReportApi.exportTaskInReport({"idList": idList})
    download.excel(data, '每日报工记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

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


</script>
<style scoped>
/* 固定搜索栏样式 */
.fixed-search {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: white;
  padding: 16px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style>
