<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-row>
      <el-col :span="16">
        <el-form-item label="工作日期" prop="workDate">
          <el-date-picker
            v-model="queryParams.workDate"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            @change="handleQuery"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-160px"
          />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
           <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['mes:org-worker:export']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button>
        </el-form-item>
      </el-col>
    </el-row>

    <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane label="所有" name="" />
      <el-tab-pane label="白班" name="1" />
      <el-tab-pane label="夜班" name="2" />
      <el-tab-pane label="长白班" name="3" />
    </el-tabs>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column
        label="工作日期"
        align="center"
        prop="workDate"
        :formatter="dateFormatter2"
        width="140px"
      />
      <el-table-column label="工位" align="center" prop="orgName" sortable />
      <el-table-column label="班别" 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="工人" align="center" prop="workerName" sortable/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['mes:org-worker: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>

</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import {dateFormatter, dateFormatter2} from '@/utils/formatTime'
import download from '@/utils/download'
import { OrgWorkerApi, OrgWorkerVO } from '@/api/mes/orgworker'


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

const props = defineProps<{
  orgId?: number // org ID（主表的关联字段）
  orgType?: string
}>()

const loading = ref(true) // 列表的加载中
const list = ref<OrgWorkerVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orgId: undefined,
  workDate: [],
  groupType: undefined,
  orgType: undefined,
  workerId: undefined,
  reportId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrgWorkerApi.getOrgWorkerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrgWorkerApi.exportOrgWorker(queryParams)
    download.excel(data, '工位安排.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const getCurrentDate = () =>{
  let now = new Date();
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  if(month<10){month = '0'+month}
  let day = now.getDate();
  if(day<10)day = '0'+day;
  return year + "-" + month + "-" + day +" 00:00:00";
}
/** 初始化 **/
onMounted(() => {
  queryParams.workDate = [getCurrentDate(), getCurrentDate()]
  // getList()
})
/** tab 切换 */
let activeName = ''
const handleTabClick = (tab: TabsPaneContext) => {
  queryParams.groupType = tab.paneName
  handleQuery()
}


/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.orgId,
  (val: number) => {
    queryParams.orgId = val
    handleQuery()
  },
  { immediate: true, deep: true }
)
watch(
  () => props.orgType,
  (val: string) => {
    queryParams.orgType = val
    queryParams.orgId = null
    handleQuery()
  },
  { immediate: true, deep: true }
)

</script>
