<template>
  <Dialog :title="dialogTitle" width="80%" v-model="dialogVisible" center>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="108px">
        <el-form-item label="生产计划单号" label-width="130px" prop="planNo">
          <el-input v-model="queryParams.planNo" 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="projectName">
          <el-input v-model="queryParams.projectName" 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>
    <el-card class="hl-card">
      <!-- 列表 -->
      <ContentWrap>
        <el-table ref="multipleTable" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
          class="hl-table" @selection-change="handleSelectionChange" @row-click="clickRow" selection>
          <el-table-column fixed type="selection" width="40"/>
          <el-table-column fixed label="计划单号" align="center" prop="planNo" width="150" />
          <el-table-column fixed label="项目编号" align="center" prop="projectCode" width="220" />
          <el-table-column fixed label="项目变更次数" align="center" prop="changeNum" width="150" />
          <el-table-column label="客户名称" align="center" prop="customerName" width="150" />
          <el-table-column label="项目名称" align="center" prop="projectName" width="150" />
          <el-table-column label="业务员" align="center" prop="businessManName" width="150" />
          <el-table-column label="起止时间" align="center" prop="projectStartTime" width="260">
            <template #default="scope">
              {{
                formatDate(scope.row.projectStartTime, 'YYYY-MM-DD') +
                '~' +
                formatDate(scope.row.projectEndTime, 'YYYY-MM-DD')
              }}
            </template>
          </el-table-column>
          <el-table-column label="所属业务线" align="center" prop="businessLine" width="150">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.HELI_BUSINESS_LINE" :value="scope.row.businessLine" />
            </template>
          </el-table-column>
          <el-table-column label="性质" align="center" prop="property" 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="isUrgency" width="150">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.HELI_COMMON_IS_OR_NOT" :value="scope.row.isUrgency" />
            </template>
          </el-table-column>
          <el-table-column label="是否已变更" align="center" prop="hasAlter" width="150">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.HELI_COMMON_IS_OR_NOT" :value="scope.row.hasAlter" />
            </template>
          </el-table-column>
          <el-table-column label="是否要工艺" align="center" prop="hasCraft" width="150">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.HELI_COMMON_IS_OR_NOT" :value="scope.row.hasCraft" />
            </template>
          </el-table-column>
          <el-table-column label="工艺起止时间" align="center" prop="craftStartDate" width="260">
            <template #default="scope">
              {{
                formatDate(scope.row.craftStartDate, 'YYYY-MM-DD') +
                '~' +
                formatDate(scope.row.craftEndDate, 'YYYY-MM-DD')
              }}
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="220px" />
          <el-table-column label="备注" align="center" prop="description" width="150" />
          <el-table-column fixed="right" label="项目负责人" align="center" prop="projectOwner" width="150">
            <template #default="scope">
              {{ userInit.find((user) => user.id == scope.row.projectOwner)?.nickname }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="工艺负责人" align="center" prop="craftOwner" width="150">
            <template #default="scope">
              {{ userInit.find((user) => user.id == scope.row.craftOwner)?.nickname }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="生产状态" align="center" prop="status" width="150">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.HELI_PROJECT_PLAN_STATUS" :value="scope.row.status" />
            </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 #footer>
      <el-button @click="dialogVisible = false" size="large">取 消</el-button>
      <el-button @click="submitForm" type="success" size="large">保 存</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2, formatDate } from '@/utils/formatTime'
import * as PlanApi from '@/api/heli/plan'
import * as UserApi from '@/api/system/user'
import { ref } from "vue";
import { ElTable } from 'element-plus'

import { ProjectOrderVO } from "@/api/heli/projectorder";

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

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

})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PlanApi.getPlanPageByStatus(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 multipleTable: any = ref<InstanceType<typeof ElTable>>()
const multipleSelection: any = ref([])

const handleSelectionChange = (val: ProjectOrderVO[]) => {
  if (val.length > 1) {
    multipleTable.value.clearSelection()
    multipleTable.value.toggleRowSelection(val.pop())
  } else {
    multipleSelection.value = val.pop()
  }

}

const clickRow = (row: any) => {
  if (row) {
    multipleTable.value!.toggleRowSelection(row, undefined)
  } else {
    multipleTable.value!.clearSelection()
  }
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = () => {
  if (!multipleSelection.value || multipleSelection.value.length <= 0) {
    message.warning(t('common.selectText'))
    return
  }
  dialogVisible.value = false
  // 发送操作成功的事件
  emit('success', multipleSelection.value)
}

const userInit = ref()
/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true

  //用户列表数据
  userInit.value = await UserApi.getSimpleUserList()
  handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

