<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="120px"
    >
      <el-row>
        <el-col :span="5">
      <el-form-item :label="t('FactoryModeling.FactoryWorker.searchNameLabel')" prop="name">
            <el-input
              v-model="queryParams.name"
              class="!w-240px"
              clearable
          :placeholder="t('FactoryModeling.FactoryWorker.searchNamePlaceholder')"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item :label="t('FactoryModeling.FactoryWorker.searchStatusLabel')" prop="status">
            <el-select
              v-model="queryParams.status"
              class="!w-240px"
              clearable
              :placeholder="t('FactoryModeling.FactoryWorker.searchStatusPlaceholder')"
            >
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.MES_ORG_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item>
            <el-button @click="handleQuery"><Icon class="mr-5px" icon="ep:search" /> {{ t('FactoryModeling.FactoryWorker.searchButtonText') }}</el-button>
            <el-button @click="resetQuery"><Icon class="mr-5px" icon="ep:refresh" /> {{ t('FactoryModeling.FactoryWorker.resetButtonText') }}</el-button>
            <el-button
              v-hasPermi="['mes:organization:export']"
              :loading="exportLoading"
              plain
              type="success"
              @click="handleExport"
            >
              <Icon class="mr-5px" icon="ep:download" /> {{ t('FactoryModeling.FactoryWorker.exportButtonText') }}
            </el-button>
            <el-button plain type="danger" @click="toggleExpandAll">
              <Icon class="mr-5px" icon="ep:sort" /> {{ t('FactoryModeling.FactoryWorker.expandButtonText') }}
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <el-tabs v-model="activeName" @tab-click="handleTabClick">

    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsZhijiang')" name="zhijiang" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsChengxing')" name="chengxing" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsHonggan')" name="honggan" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsZhuanyi')" name="zhuanyi" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsJiashi')" name="jiashi" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsReya')" name="reya" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsQiebian')" name="qiebian" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsPanjian')" name="pinjian" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsDabao')" name="dabao" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsTiebiao')" name="tiebiao" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsPinyin')" name="pinyin" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsSufeng')" name="sufeng" />
    <el-tab-pane :label="t('FactoryModeling.FactoryWorker.tabsAll')" name="" />
  </el-tabs>

    <el-row>
      <el-col :span="12">
        <ContentWrap>
        <el-table
          v-if="refreshTable"
          v-loading="loading"
          :data="list"
          :default-expand-all="isExpandAll"
          :show-overflow-tooltip="true"
          :stripe="true"
          row-key="id"
          highlight-current-row
          @current-change="handleCurrentChange"
        >
          <el-table-column align="center" :label="t('FactoryModeling.FactoryWorker.tableNameColumn')" prop="name" width="200px"/>

          <el-table-column align="center" :label="t('FactoryModeling.FactoryWorker.tableOrgClassColumn')" prop="orgClass">
            <template #default="scope">
              <dict-tag v-if="notEmpty(scope.row.orgClass)" :type="DICT_TYPE.MES_ORG_CLASS" :value="scope.row.orgClass" />
            </template>
          </el-table-column>
          <el-table-column align="center" :label="t('FactoryModeling.FactoryWorker.tableOrgTypeColumn')" >
            <template #default="scope">
              <dict-tag v-if="notEmpty(scope.row.orgType)"  :type="DICT_TYPE.MES_ORG_TYPE" :value="scope.row.orgType" />
            </template>
          </el-table-column>
          <el-table-column align="center" :label="t('FactoryModeling.FactoryWorker.tableWorkerColumn')" prop="workerUserName" />
          <el-table-column align="center" fixed="right" :label="t('FactoryModeling.FactoryWorker.tableOperateColumn')" width="80">
            <template #default="scope">
              <el-button
                v-if="scope.row.orgClass=='workplace' && scope.row.status=='free'"
                v-hasPermi="['mes:organization:update']"
                link
                type="warning"
                @click="openForm(scope.row.orgType, scope.row.id,scope.row.name, scope.row.workerUserName)"
              >
                {{ t('FactoryModeling.FactoryWorker.tableDispatchAction') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        </ContentWrap>
      </el-col>
      <el-col :span="12">
        <!-- 列表 -->
        <WorkerRight :org-id="orgId" :org-type="queryParams.orgType"/>
      </el-col>
    </el-row>

  <!-- 表单弹窗：添加/修改 -->
  <OrgWorkerForm ref="formRef" @success="getList" />
</template>

<script lang="ts" setup>
import {getIntDictOptions, getBoolDictOptions, DICT_TYPE, getStrDictOptions} from '@/utils/dict'
import {defaultProps, handleTree} from '@/utils/tree'
import download from '@/utils/download'
import { OrganizationApi, OrganizationVO } from '@/api/mes/organization'

import WorkerRight from "./components/WorkerRight.vue";
import OrgWorkerForm from "./components/OrgWorkerForm.vue";

/** 产线工位 列表 */
defineOptions({ name: 'WorkerIndex' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const orgId = ref()
const loading = ref(true) // 列表的加载中
const list = ref<OrganizationVO[]>([]) // 列表的数据


const queryParams = reactive({
  name: undefined,
  parentId: undefined,
  sort: undefined,
  workerUserId: undefined,
  machineId: undefined,
  phone: undefined,
  email: undefined,
  isEnable: undefined,
  status: undefined,
  orgClass: undefined,
  orgType: undefined,
  createTime: [],
  workDate: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrganizationApi.getListOrgWorker(queryParams)
    list.value = handleTree(data, 'id', 'parentId')
  } finally {
    loading.value = false
  }
}

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

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


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

/** 展开/折叠操作 */
const isExpandAll = ref(true) // 是否展开，默认全部展开
const refreshTable = ref(true) // 重新渲染表格状态
const toggleExpandAll = async () => {
  refreshTable.value = false
  isExpandAll.value = !isExpandAll.value
  await nextTick()
  refreshTable.value = true
}

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

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

const notEmpty = (str:string) => {
  return str != "" && str != null;
}

/** 派工操作 */
const formRef = ref()
const openForm =async (type: string, id?: number,orgName?:string,workerName?:string) => {
  if(workerName != null && workerName.length > 0){
   await message.confirm("工位："+orgName+",今天已经派工，确定要重新派工吗？").then()
  }
  formRef.value.open(type, id)
}
</script>
