<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="编号" prop="KEYWORDS">
              <el-input
                v-model="searchForm.KEYWORDS"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <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
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetData"
      >
        <el-table-column label="序号" width="70">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="CHECK_NO" label="编号" show-overflow-tooltip />
        <el-table-column prop="APPLY_USER_NAME" label="申请人" width="100" />
        <el-table-column prop="APPLY_DEPARTMENT_NAME" label="申请部门" />
        <el-table-column
          prop="SAFETY_USER_NAME"
          label="水、电、汽、工艺、设备、消防、 安全管理等动土所在单位负责人"
          show-overflow-tooltip
          width="150"
        />
        <el-table-column
          prop="AUDIT_USER_NAME"
          label="分厂负责人"
          show-overflow-tooltip
        />
        <el-table-column
          prop="ACCEPT_USER_NAME"
          label="验收部门负责人"
          show-overflow-tooltip
        />
        <el-table-column label="审核状态" width="170">
          <template #default="{ row }">
            <span v-if="row.STATUS === -1 || row.STATUS === -2"
              >{{ row.STEP_NAME }}时作废本作业票</span
            >
            <span v-else>验收归档后作废本作业票</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="STEP_REASON"
          label="作废原因"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="250">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="fnFlowChart(row.BREAKGROUND_ID)"
            >
              流程图
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/groundbreaking/job_data/view',
                  query: {
                    BREAKGROUND_ID: row.BREAKGROUND_ID,
                    entrance: 'archive',
                  },
                })
              "
            >
              审批表详情
            </el-button>
            <el-button
              v-if="ISMAIN === '1'"
              type="danger"
              text
              link
              @click="
                fnRemoveWorkTicket(
                  row.BREAKGROUND_ID,
                  'groundbreaking',
                  row.CHECK_NO
                )
              "
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
    <flow-chart
      v-model:visible="data.flowChartDialog.visible"
      :flow-list="data.flowChartDialog.flowList"
      :flowing="data.flowChartDialog.flowing"
    />
    <check-list
      v-if="data.checkListDialog.visible"
      v-model:visible="data.checkListDialog.visible"
      v-model:foreign-key="data.checkListDialog.FOREIGN_KEY"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import { useRouter } from "vue-router";
import useListData from "@/assets/js/useListData.js";
import { reactive } from "vue";
import FlowChart from "./components/flow_chart.vue";
import CheckList from "@/components/safety_briefing_checklist/index.vue";
import {
  getGroundbreakingFlow,
  getGroundbreakingList,
  setEightWorkRemove,
} from "@/request/eight_work.js";
import { useUserStore } from "@/pinia/user.js";
import { ElMessage, ElMessageBox } from "element-plus";
const userStore = useUserStore();
const ISMAIN = userStore.getUserInfo.ISMAIN;
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getGroundbreakingList, {
    otherParams: { WORK_STATE: "-1" },
  });
const router = useRouter();

const data = reactive({
  flowChartDialog: {
    visible: false,
    flowList: {},
    flowing: {},
  },

  checkListDialog: {
    visible: false,
    FOREIGN_KEY: "",
  },
});

const fnResetPaginationTransfer = () => {
  fnResetPagination({
    APPLY_DEPARTMENT_ID: searchForm.value.APPLY_DEPARTMENT_ID,
    CONFIRM_DEPARTMENT_ID: searchForm.value.CONFIRM_DEPARTMENT_ID,
    WORK_START_DATE: searchForm.value.WORK_START_DATE,
  });
};

const fnFlowChart = async (BREAKGROUND_ID) => {
  const { flowList, flowingWork } = await getGroundbreakingFlow({
    BREAKGROUND_ID,
  });
  data.flowChartDialog.visible = true;
  data.flowChartDialog.flowList = flowList;
  data.flowChartDialog.flowing = flowingWork;
};
const fnRemoveWorkTicket = async (id, type, CHECK_NO) => {
  await ElMessageBox.confirm("确定要删除吗？", {
    type: "warning",
  });
  await setEightWorkRemove({
    WORK_TYPE: type,
    WORK_ID: id,
    CHECK_NO,
  });

  await fnResetPaginationTransfer();
  ElMessage({
    message: "删除成功",
    type: "success",
  });
};
</script>

<style scoped></style>
