<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="120px">
        <el-form-item label="生产派工单号" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入生产派工单号" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>
        <el-form-item label="生产任务单号" prop="taskCode">
          <el-input v-model="queryParams.taskCode" 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="projectSubName">
          <el-input v-model="queryParams.projectSubName" placeholder="请输入子项目名称" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>
        <el-form-item label="子项目编号" prop="projectSubCode">
          <el-input v-model="queryParams.projectSubCode" placeholder="请输入子项目编号" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>
        <el-form-item label="单据状态" prop="dispatchStatus">
          <el-select v-model="queryParams.dispatchStatus" placeholder="请选择单据状态" clearable class="!w-240px">
            <el-option v-for="dict in getIntDictOptions(DICT_TYPE.HELI_DISPATCH_PRODUCTION_STATUS)" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="任务负责人" prop="ownerName">
          <el-input v-model="queryParams.ownerName" placeholder="请输入任务负责人" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>
        <el-form-item label="图号" prop="blueprintNo">
          <el-input v-model="queryParams.blueprintNo" placeholder="请输入图号" 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 type="index" width="70" label="序号" align="center" />
        <el-table-column fixed label="零件名称" align="center" prop="materialName" width="180" />
        <el-table-column label="材质" align="center" prop="compositionName" width="80px" />
        <el-table-column label="数量" align="center" prop="amount" width="80px" />
        <el-table-column label="图号" align="center" prop="blueprintNo" width="80px" />
        <el-table-column label="规格型号" align="center" prop="spec" width="120px" />
        <el-table-column label="系统单位" align="center" prop="unit" width="100px">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_MATERIAL_UNIT" :value="scope.row.unit" />
          </template>
        </el-table-column>
        <el-table-column label="客户名称" align="center" prop="customerName" width="180px" />
        <el-table-column label="项目结束日期" align="center" prop="projectEndTime" :formatter="dateFormatter2" width="160px" />
        <el-table-column label="项目名称" align="center" prop="projectName" width="180" />
        <el-table-column label="子项目名称" align="center" prop="projectSubName" width="180" />
        <el-table-column fixed label="生产任务单号" align="center" prop="taskCode" width="180" />
        <!-- <el-table-column label="生产派工单号" align="center" prop="code" width="180"  >
            <template #default="scope">
              <el-button type="primary" plain link @click="openDetail('detail', scope.row.id)">{{ scope.row.code }}</el-button>
            </template>
        </el-table-column>
        <el-table-column label="项目编号" align="center" prop="projectCode" width="220" /> -->
        <!-- <el-table-column label="子项目编号" align="center" prop="projectSubCode" width="260" /> -->
        <!-- <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="210" />
        <el-table-column label="备注" align="center" prop="remark" width="160" /> -->
        <el-table-column fixed="right" label="任务负责人" align="center" prop="ownerName" width="140" />
        <el-table-column fixed="right" label="要求完成时间" align="center" prop="requiredCompletedDate" :formatter="dateFormatter2" width="160" />
        <el-table-column fixed="right" label="单据状态" align="center" prop="dispatchStatus" width="140">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_DISPATCH_PRODUCTION_STATUS" :value="scope.row.dispatchStatus" />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template #default="scope">
            <!-- <el-button link type="primary" @click="openDetail('update', scope.row.id)">
              编辑
            </el-button> -->
            <el-button link type="primary" @click="openDispatching('update',scope.row.id,scope.row.ownerId,scope.row.taskCode)">
              编辑
            </el-button>
            <el-button link type="primary" @click="openDispatching('detail',scope.row.id,scope.row.ownerId,scope.row.taskCode)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </ContentWrap>
    <Dispatchdialog ref="Dispatchingref" @success="exitClick" />
  </el-card>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import {  dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import * as TaskDispatchApi from '@/api/heli/taskdispatch'
import { useCommonStore } from '@/store/modules/common'
import { useUserStore } from '@/store/modules/user'
import Dispatchdialog from './detailDialog.vue'
defineOptions({ name: 'TaskDispatch' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const commonStore = useCommonStore()
const userStore = useUserStore()
const username = userStore.getUser.nickname
const Dispatchingref = ref()
const dispatchType = 'PRODUCTION'
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  dispatchType: dispatchType,
  taskCode: undefined,
  planId: undefined,
  projectCode: undefined,
  projectSubCode: undefined,
  customerName: undefined,
  dispatchStatus: 0,
  remark: undefined,
  status: undefined,
  createTime: [],
  ownerName: username,
  blueprintNo:undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {

    const route = useRoute();

    if(route){
      const idid = route.query.idid;
      queryParams.taskCode=idid
    }


    const data = await TaskDispatchApi.getTaskDispatchPage(queryParams)
    history.state.idid=undefined
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
const exitClick = () => {
  getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

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

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await TaskDispatchApi.exportTaskDispatch(queryParams)
    download.excel(data, '派工单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const openDetail = (type: string, id?: number) => {
  commonStore.setStore('active', type)
  commonStore.setStore('id', id)
  commonStore.setStore('dispatchType', dispatchType)
  router.push({
    name: 'TaskDispatchDetail',
    query: {
      operateId: Math.random()
    }
  })
}

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