<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="50px"
        @submit.prevent="fnResetPagination"
      >
        <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 v-if="entrance === 'job_data'" :span="6">
            <el-form-item label="状态" prop="STEP_ID">
              <el-select v-model="searchForm.STEP_ID">
                <el-option
                  v-for="item in stepList"
                  :key="item.STEP_ID"
                  :label="item.STEP_NAME"
                  :value="item.STEP_ID"
                />
              </el-select>
            </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="fnResetPagination">
                重置
              </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="填写人" />
        <el-table-column
          prop="CONFIRM_USER_NAME"
          label="作业负责人"
          show-overflow-tooltip
        />
        <el-table-column label="所在单位负责人" show-overflow-tooltip>
          <template #default="{ row }">
            {{ row.LEADER_USER_NAME || " 已跳过" }}
          </template>
        </el-table-column>
        <el-table-column
          label="消防、安全管理部门负责人"
          show-overflow-tooltip
          width="200"
        >
          <template #default="{ row }">
            {{ row.AUDIT_DEPARTMENT_NAME || " 已跳过" }}
          </template>
        </el-table-column>
        <el-table-column label="审批部门负责人" show-overflow-tooltip>
          <template #default="{ row }">
            {{ row.APPROVE_USER_NAME || " 已跳过" }}
          </template>
        </el-table-column>
        <el-table-column
          prop="ACCEPT_USER_NAME"
          label="验收部门负责人"
          show-overflow-tooltip
        />
        <el-table-column label="审核状态">
          <template #default="{ row }">
            <span v-if="row.STEP_ID === 0">暂存</span>
            <span v-else-if="row.STEP_ID !== -1">{{ row.NEXT_STEP_NAME }}</span>
            <span v-else>{{ row.STEP_NAME }}打回</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template #default="{ row }">
            <el-button
              v-if="
                entrance === 'job_data' &&
                row.STATUS > -1 &&
                row.NEXT_STEP_ID !== 99
              "
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/open_circuit/job_data/add_video',
                  query: {
                    CUTROAD_ID: row.CUTROAD_ID,
                    CHECK_NO: row.CHECK_NO,
                  },
                })
              "
            >
              添加监控
            </el-button>
            <el-button
              v-if="
                entrance === 'archive' &&
                row.NEXT_STEP_ID === 99 &&
                row.VIDEO_PATH
              "
              type="primary"
              text
              link
              @click="fnPreviewVideo(row.VIDEO_PATH)"
            >
              查看验收视频
            </el-button>
            <el-button
              v-if="entrance === 'job_data'"
              type="primary"
              text
              link
              @click="fnFlowChart(row.CUTROAD_ID)"
            >
              流程图
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path:
                    props.entrance === 'job_data'
                      ? '/open_circuit/job_data/view'
                      : '/open_circuit/archive/view',
                  query: { CUTROAD_ID: row.CUTROAD_ID, entrance },
                })
              "
            >
              审批表详情
            </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"
    />
    <layout-video
      v-model:visible="data.videoDialog.visible"
      :src="data.videoDialog.src"
      append-to-body
    />
  </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, ref } from "vue";
import FlowChart from "./components/flow_chart.vue";
import {
  getOpenCircuitFlow,
  getOpenCircuitList,
  getTaskSetps,
} from "@/request/eight_work.js";
import LayoutVideo from "@/components/video/index.vue";

const props = defineProps({
  entrance: {
    type: String,
    default: "job_data",
  },
});
const stepList = ref([]);
const getStepList = async () => {
  const { list } = await getTaskSetps({ TASK_ID: 1 });
  stepList.value = [
    { STEP_NAME: "请选择", STEP_ID: "" },
    ...list,
    { STEP_NAME: "验收归档", STEP_ID: "99" },
  ];
};
const fnPreviewVideo = (VIDEO_PATH) => {
  data.videoDialog.visible = true;
  data.videoDialog.src = VIDEO_PATH;
};

getStepList();
const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getOpenCircuitList, {
    otherParams: props.entrance === "archive" ? { STEP_ID: "99" } : {},
  });
const data = reactive({
  flowChartDialog: {
    visible: false,
    flowList: {},
    flowing: {},
  },
  videoDialog: {
    visible: false,
    src: "",
  },
});
const fnFlowChart = async (CUTROAD_ID) => {
  const { flowList, flowingWork } = await getOpenCircuitFlow({ CUTROAD_ID });
  data.flowChartDialog.visible = true;
  data.flowChartDialog.flowList = flowList;
  data.flowChartDialog.flowing = flowingWork;
};
</script>
<style scoped></style>
