<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('FactoryModeling.WorkTeam.searchTeamNameLabel')" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          :placeholder="t('FactoryModeling.WorkTeam.searchTeamNamePlaceholder')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('FactoryModeling.WorkTeam.searchProcessLabel')" prop="teamType">
        <el-select
          v-model="queryParams.teamType"
          :placeholder="t('FactoryModeling.WorkTeam.searchProcessPlaceholder')"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MES_ORG_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('FactoryModeling.WorkTeam.searchRemarkLabel')" prop="remark">
        <el-input
          v-model="queryParams.remark"
          :placeholder="t('FactoryModeling.WorkTeam.searchRemarkPlaceholder')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> {{ t('FactoryModeling.WorkTeam.searchButtonText') }}</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> {{ t('FactoryModeling.WorkTeam.resetButtonText') }}</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['mes:work-team:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> {{ t('FactoryModeling.WorkTeam.addButtonText') }}
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['mes:work-team:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> {{ t('FactoryModeling.WorkTeam.exportButtonText') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane :label="t('FactoryModeling.WorkTeam.tabsAll')" name="" />
      <el-tab-pane :label="t('FactoryModeling.WorkTeam.tabsDay')" name="1" />
      <el-tab-pane :label="t('FactoryModeling.WorkTeam.tabsNight')" name="2" />
      <el-tab-pane :label="t('FactoryModeling.WorkTeam.tabsLongDay')" name="3" />
    </el-tabs>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column :label="t('FactoryModeling.WorkTeam.tableTeamNameColumn')" align="center" prop="teamName" />
      <el-table-column :label="t('FactoryModeling.WorkTeam.tableLeaderColumn')" align="center" prop="teamLeaderName" />
      <el-table-column :label="t('FactoryModeling.WorkTeam.tableGroupTypeColumn')" align="center" prop="groupType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MES_GROUP_TYPE" :value="scope.row.groupType" />
        </template>
      </el-table-column>
      <el-table-column :label="t('FactoryModeling.WorkTeam.tableWorkTimeColumn')" align="center" prop="startTime">
        <template #default="scope">
          <el-text>{{getHour(scope.row.startTime)+'-'+getHour(scope.row.endTime)}}</el-text>
        </template>
      </el-table-column>
      <el-table-column :label="t('FactoryModeling.WorkTeam.tableProcessColumn')" align="center" prop="teamType" sortable>
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MES_ORG_TYPE" :value="scope.row.teamType" />
        </template>
      </el-table-column>
      <el-table-column :label="t('FactoryModeling.WorkTeam.tableRemarkColumn')" align="center" prop="remark" />
      <el-table-column :label="t('FactoryModeling.WorkTeam.tableEnableColumn')" align="center" prop="isEnable">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isEnable" />
        </template>
      </el-table-column>
      <el-table-column
        :label="t('FactoryModeling.WorkTeam.tableCreateTimeColumn')"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column :label="t('FactoryModeling.WorkTeam.tableOperateColumn')" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['mes:work-team:update']"
          >
            {{ t('FactoryModeling.WorkTeam.tableEditAction') }}
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['mes:work-team:delete']"
          >
            {{ t('FactoryModeling.WorkTeam.tableDeleteAction') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WorkTeamForm ref="formRef" @success="getList" />
  <!-- 子表的列表 -->
  <ContentWrap>
    <el-tabs model-value="workTeamDetail">
      <el-tab-pane :label="t('FactoryModeling.WorkTeam.detailTabLabel')" name="workTeamDetail">
        <WorkTeamDetailList :work-team-id="currentRow.id" :team-name="currentRow.teamName"/>
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { WorkTeamApi, WorkTeamVO } from '@/api/mes/workteam'
import WorkTeamForm from './WorkTeamForm.vue'
import WorkTeamDetailList from './components/WorkTeamDetailList.vue'

/** 生产班组 列表 */
defineOptions({ name: 'WorkTeam' })

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

const loading = ref(true) // 列表的加载中
const list = ref<WorkTeamVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  teamName: undefined,
  teamLeaderId: undefined,
  teamType: undefined,
  groupType: undefined,
  remark: undefined,
  isEnable: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WorkTeamApi.getWorkTeamPage(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 WorkTeamApi.deleteWorkTeam(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WorkTeamApi.exportWorkTeam(queryParams)
    download.excel(data, '生产班组.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

/** 初始化 **/
onMounted(() => {
  getList()
})
/** tab 切换 */
let activeName = ''
const handleTabClick = (tab: TabsPaneContext) => {
  queryParams.groupType = tab.paneName
  handleQuery()
}

const getHour = (timeStamp) =>{
  let now = new Date(timeStamp);
  let hours = now.getHours();
  let minutes = now.getMinutes();
  let seconds = now.getSeconds();
  if(hours<10)hours = '0'+hours;
  if(minutes<10)minutes = '0'+minutes;
  if(seconds<10)seconds = '0'+seconds;

  return hours+":"+minutes+":"+seconds;
}
</script>
