<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="作业申请分厂" prop="APPLY_DEPARTMENT_ID">
              <el-select
                v-model="searchForm.APPLY_DEPARTMENT_ID"
                @change="fnGetApplyDept"
              >
                <el-option
                  v-for="item in applyDeptList"
                  :key="item.department_ID"
                  :label="item.name"
                  :value="item.department_ID"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <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-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        row-key="UNITS_ID"
        @get-data="fnGetData"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="APPLY_DEPARTMENT_NAME" label="申请单位" />
        <el-table-column label="作业等级" width="200">
          <template #default="{ row }">
            <span v-if="row.WORK_LEVEL === '1'">一级作业级别</span>
            <span v-else-if="row.WORK_LEVEL === '2'">二级作业级别</span>
            <span v-else-if="row.WORK_LEVEL === '3'">三级作业级别</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="WORKSHOP_DEPARTMENT_NAME"
          label="工段部门"
          width="150"
        />
        <el-table-column
          prop="WORKSHOP_USER_NAME"
          label="工段负责人"
          width="150"
        />
        <el-table-column
          prop="LEADER_DEPARTMENT_NAME"
          label="分厂贯标中心"
          width="150"
        />
        <el-table-column
          prop="LEADER_USER_NAME"
          label="分厂贯标中心负责人"
          width="150"
        />
        <el-table-column
          prop="AUDIT_DEPARTMENT_NAME"
          label="分厂部门"
          width="150"
        />
        <el-table-column
          prop="AUDIT_USER_NAME"
          label="分厂部门负责人"
          width="150"
        />
        <el-table-column
          prop="APPROVE_DEPARTMENT_NAME"
          label="安全处"
          width="150"
        />
        <el-table-column
          prop="APPROVE_USER_NAME"
          label="安全处负责人"
          width="150"
        />
        <el-table-column
          prop="SAFETY_DEPARTMENT_NAME"
          label="安全部"
          width="100"
        />
        <el-table-column
          prop="SAFETY_USER_NAME"
          label="安全部负责人"
          width="100"
        />

        <el-table-column label="操作" align="center" width="240">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="fnView(row.CONFIGURATION_ID)"
            >
              查看
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="fnEdit(row.CONFIGURATION_ID)"
            >
              编辑
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="handleDelete(row.CONFIGURATION_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button type="primary" @click="fnAdd(row)"> 新增 </el-button>
          <el-button type="danger" @click="batchDel()"> 删除 </el-button>
        </template>
      </layout-table>
    </layout-card>
    <edit
      v-model:visible="data.editDialog.visible"
      v-model:form="data.editDialog.form"
      :type="data.editDialog.type"
      :title="data.editDialog.title"
      @get-data="fnResetPaginationTransfer"
    />
  </div>
</template>

<script setup>
import useListData from "@/assets/js/useListData";
import { serialNumber } from "@/assets/js/utils";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref } from "vue";
import Edit from "./components/edit.vue";
import {
  getApplyDept,
  getHoistingConfigurationUserView,
  setHoistingConfigurationUserDelete,
  setHoistingConfigurationUserDeleteAll,
  setHoistingConfigurationUserList
} from "@/request/eight_work.js";

const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(setHoistingConfigurationUserList);
const data = reactive({
  type: "",
  title: "",
  editDialog: {
    visible: false,
    form: {
      CONFIGURATION_ID: "",
      APPLY_DEPARTMENT_ID: "",
      WORK_LEVEL: "",
      LEADER_DEPARTMENT_ID: "",
      LEADER_USER_ID: "",
      AUDIT_DEPARTMENT_ID: "",
      AUDIT_USER_ID: "",
      APPROVE_DEPARTMENT_ID: "",
      APPROVE_USER_ID: "",
      SAFETY_DEPARTMENT_ID: "",
      SAFETY_USER_ID: "",
      WORKSHOP_DEPARTMENT_ID: "",
      WORKSHOP_USER_ID: "",
    },
  },
});
const applyDeptList = ref([]);
const fnGetApplyDept = () => {
  data.APPLY_WORKSHOP_DEPARTMENT_ID = "";
  data.applyWorkshopDeptList = [];
};

const getApplyDeptName = async () => {
  const resData = await getApplyDept({
    PARENT_ID: "551edf3de6bc447c8a03ec38dfb4cf75",
  });
  applyDeptList.value = resData.varList;
};
getApplyDeptName();
const handleDelete = async (CONFIGURATION_ID) => {
  await ElMessageBox.confirm("确定要删除吗？", {
    type: "warning",
  });
  await setHoistingConfigurationUserDelete({ CONFIGURATION_ID });
  fnGetData();
  ElMessage.success("删除成功");
};

const batchDel = async () => {
  const selectionData = tableRef.value.getSelectionRows();
  if (selectionData.length === 0) {
    ElMessage.warning("请选中要删除的项");
    return;
  }
  await ElMessageBox.confirm("确定要删除吗？", {
    type: "warning",
  });
  const ids = selectionData
    .map((item) => {
      return item.CONFIGURATION_ID;
    })
    .join(",");
  await setHoistingConfigurationUserDeleteAll({ DATA_IDS: ids });
  fnGetData();
  ElMessage.success("删除成功");
  tableRef.value.clearSelection();
};

const fnGoEdit = async (id) => {
  const resData = await getHoistingConfigurationUserView({
    CONFIGURATION_ID: id,
  });
  data.editDialog.form = resData.pd;
};

const fnResetPaginationTransfer = () => {
  fnGetData(searchForm);
  tableRef.value.clearSelection();
};

const fnAdd = () => {
  data.editDialog.form = {};
  data.editDialog.visible = true;
  data.editDialog.type = "add";
  data.editDialog.title = "新增";
};

const fnEdit = async (id) => {
  data.editDialog.form = {};
  data.editDialog.visible = true;
  data.editDialog.type = "edit";
  data.editDialog.title = "编辑";
  fnGoEdit(id);
};

const fnView = async (id) => {
  data.editDialog.form = {};
  data.editDialog.visible = true;
  data.editDialog.type = "view";
  data.editDialog.title = "查看";
  fnGoEdit(id);
};
</script>

<style scoped></style>
