<template>
  <div>
    <layout-card>
      <el-row :gutter="12">
        <el-col :span="5">
          <layout-department-tree
            :department-id="DEPARTMENT_ID"
            @node-click="
              router.push({
                path: '/enterprise_management/user',
                query: {
                  DEPARTMENT_ID: $event.id,
                },
              })
            "
          />
        </el-col>
        <el-col :span="19">
          <el-form
            :model="searchForm"
            label-width="50px"
            @submit.prevent="fnResetPaginationTransfer"
          >
            <el-row>
              <el-col :span="6">
                <el-form-item
                  label="用户名/姓名"
                  prop="KEYWORDS"
                  label-width="100px"
                >
                  <el-input
                    v-model="searchForm.KEYWORDS"
                    placeholder="请输入关键字"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="排班" prop="SHIFTDUTYONE">
                  <el-select
                    v-model="searchForm.SHIFTDUTYONE"
                    @change="fnGetUserScheduling($event, 'schedulingList1')"
                  >
                    <el-option
                      v-for="item in data.schedulingList"
                      :key="item.SHIFTWORKRULES_ID"
                      :label="item.NAME"
                      :value="item.SHIFTWORKRULES_ID"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label-width="0px" prop="SHIFTDUTYTWO">
                  <el-select v-model="searchForm.SHIFTDUTYTWO">
                    <el-option
                      v-for="item in data.schedulingList1"
                      :key="item.SHIFTWORKRULES_ID"
                      :label="item.NAME"
                      :value="item.SHIFTWORKRULES_ID"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label-width="10px">
                  <el-button type="primary" native-type="submit">
                    搜索
                  </el-button>
                  <el-button
                    native-type="reset"
                    @click="fnResetPaginationTransfer"
                  >
                    重置
                  </el-button>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label-width="10px" class="end">
                  <el-button @click="fnImportDialogChangeShow">
                    导入
                  </el-button>
                  <el-button @click="fnImportLearnersDialogChangeShow">
                    在线学习人员导入
                  </el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <layout-table
            v-model:pagination="pagination"
            :data="list"
            @get-data="fnGetDataTransfer"
          >
            <el-table-column label="序号" width="70">
              <template #default="{ $index }">
                {{ serialNumber(pagination, $index) }}
              </template>
            </el-table-column>
            <el-table-column prop="USERNAME" label="用户名" width="150" />
            <el-table-column prop="NAME" label="姓名" />
            <el-table-column prop="PHONE" label="手机号" />
            <el-table-column prop="DEPARTMENT_NAME" label="部门" />
            <el-table-column
              prop="POST_NAME"
              label="岗位"
              width="150"
              show-overflow-tooltip
            />
            <el-table-column prop="ROLE_NAME" label="角色" />
            <el-table-column label="排班类型">
              <template #default="{ row }">
                <span v-if="row.SHIFTDUTYONENAME && row.SHIFTDUTYTWONAME">
                  {{ row.SHIFTDUTYONENAME }}-{{ row.SHIFTDUTYTWONAME }}
                </span>
                <span v-else>全班</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="250">
              <template #default="{ row }">
                <el-button
                  v-if="buttonJurisdiction.edit"
                  type="primary"
                  text
                  link
                  @click="fnResetPassword(row.USER_ID, row.NAME)"
                >
                  重置密码
                </el-button>
                <el-button
                  v-if="buttonJurisdiction.edit && row.ISMAIN === '0'"
                  type="primary"
                  text
                  link
                  @click="fnSchedule(row)"
                >
                  排班表
                </el-button>
                <el-button
                  v-if="
                    !(
                      row.PERSONNEL_TYPE_BIANMA?.indexOf(
                        'TRAFFIC_EMPLOYMENT_DRIVE'
                      ) >= 0
                    )
                  "
                  type="primary"
                  text
                  link
                  @click="
                    router.push({
                      path: '/enterprise_management/user/edit',
                      query: { USER_ID: row.USER_ID },
                    })
                  "
                >
                  编辑
                </el-button>
                <el-button
                  v-if="
                    buttonJurisdiction.del &&
                    row.ISMAIN === '0' &&
                    !(
                      row.PERSONNEL_TYPE_BIANMA?.indexOf(
                        'TRAFFIC_EMPLOYMENT_DRIVE'
                      ) >= 0
                    )
                  "
                  type="primary"
                  text
                  link
                  @click="fnDelete(row.USER_ID, row.NAME)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
            <template #button>
              <el-button
                v-if="buttonJurisdiction.add"
                type="primary"
                @click="
                  router.push({ path: '/enterprise_management/user/add' })
                "
              >
                新增
              </el-button>
            </template>
          </layout-table>
        </el-col>
      </el-row>
    </layout-card>
    <layout-import-file
      v-model:visible="data.importDialogVisible"
      template-url="template/userExcelTemplate.xls"
      @submit="fnSubmitImport"
    />
    <layout-import-file
      v-model:visible="data.importLearnersDialogVisible"
      template-url="template/userstudyExcelTemplate.xls"
      title="在线学习人员导入"
      @submit="fnSubmitLearnersImport"
    />
    <scheduling
      :id="data.scheduleDialog.id"
      v-model:visible="data.scheduleDialog.visible"
      :info="data.scheduleDialog.info"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import {
  getUserList,
  getUserScheduling,
  setUserDelete,
  setUserImport,
  setUserLearnersImport,
  setUserResetPassword,
} from "@/request/enterprise_management.js";
import { reactive, ref } from "vue";
import LayoutDepartmentTree from "@/components/department_tree/index.vue";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import LayoutImportFile from "@/components/import_file/index.vue";
import Scheduling from "./components/scheduling.vue";

const router = useRouter();
const route = useRoute();
const departmentIdDefault = "";
const DEPARTMENT_ID = ref(route.query.DEPARTMENT_ID || departmentIdDefault);
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getUserList, {
    otherParams: { DEPARTMENT_ID: DEPARTMENT_ID.value },
    key: "userList",
  });
const buttonJurisdiction = await useButtonJurisdiction("user");
const data = reactive({
  schedulingList: [],
  schedulingList1: [],
  importDialogVisible: false,
  importLearnersDialogVisible: false,
  scheduleDialog: {
    visible: false,
    info: {},
    id: "",
  },
});
const fnGetUserScheduling = async (PARENTID, list) => {
  const resData = await getUserScheduling({ PARENTID });
  data[list] = resData.varList;
  searchForm.value.SHIFTDUTYTWO = "";
};
fnGetUserScheduling("0", "schedulingList");
const fnGetDataTransfer = () => {
  fnGetData({
    DEPARTMENT_ID: DEPARTMENT_ID.value,
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPARTMENT_ID: DEPARTMENT_ID.value,
  });
};
onBeforeRouteUpdate((to) => {
  DEPARTMENT_ID.value = to.query.DEPARTMENT_ID || departmentIdDefault;
  fnResetPaginationTransfer();
});
const fnResetPassword = debounce(
  1000,
  async (USER_ID, NAME) => {
    await ElMessageBox.confirm(`是否将【${NAME}】重置密码为Aa@123456？`, {
      type: "warning",
    });
    await setUserResetPassword({ USER_ID });
    ElMessage.success("重置成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const fnDelete = debounce(
  1000,
  async (USER_ID, NAME) => {
    await ElMessageBox.confirm(`确定要删除【${NAME}】吗？`, {
      type: "warning",
    });
    await setUserDelete({ USER_ID });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const fnImportDialogChangeShow = () => {
  data.importDialogVisible = !data.importDialogVisible;
};
const fnSubmitImport = async (formData) => {
  const resData = await setUserImport(formData);
  ElMessage.success(resData.msg);
  fnImportDialogChangeShow();
  fnResetPaginationTransfer();
};
const fnImportLearnersDialogChangeShow = () => {
  data.importLearnersDialogVisible = !data.importLearnersDialogVisible;
};
const fnSubmitLearnersImport = async (formData) => {
  const resData = await setUserLearnersImport(formData);
  ElMessage.success(resData.msg);
  fnImportLearnersDialogChangeShow();
  fnResetPaginationTransfer();
};
const fnSchedule = (row) => {
  data.scheduleDialog.visible = true;
  data.scheduleDialog.info = row;
  data.scheduleDialog.id = row.SHIFTDUTYTWO;
};
</script>

<style scoped lang="scss"></style>
