<template>
  <div class="safety-platform-container">
    <header class="safety-platform-container__header">
      <div class="breadcrumb-title"> 安全责任通知管理 (部门侧)</div>
      <!-- <el-tabs v-model="activeTab">
        <el-tab-pane label="全部" name="" />
        <el-tab-pane label="院领导" name="院领导" />
        <el-tab-pane label="部门" name="部门" />
        <el-tab-pane label="科室" name="科室" />
        <el-tab-pane label="员工" name="员工" />
        <el-tab-pane label="常驻供应商" name="常驻供应商" />
      </el-tabs> -->
    </header>
    <main class="safety-platform-container__main">
      <div class="search-form">
        <el-form :inline="true">
          <el-form-item label="责任通知">
            <el-input
              v-model="queryParams.queryParam.responsibilityName"
              placeholder="搜索责任通知名称"
              style="width: 170px"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="queryParams.queryParam.status" clearable placeholder="状态" style="width: 170px">
              <!-- <el-option :value="1" label="未下发" /> -->
              <el-option :value="2" label="待反馈" />
              <el-option :value="3" label="待审核" />
              <el-option :value="4" label="已完成" />
              <el-option :value="5" label="已作废" />
            </el-select>
          </el-form-item>
          <el-form-item v-if="activeTab" label="类别名称">
            <el-select
              v-model="queryParams.queryParam.departmentName"
              clearable
              placeholder="类别名称"
              style="width: 170px"
            >
              <el-option value="院领导">院领导</el-option>
              <el-option value="部门">部门</el-option>
              <el-option value="科室">科室</el-option>
              <el-option value="员工">员工</el-option>
              <el-option value="常驻供应商">常驻供应商</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计划日期">
            <el-date-picker
              v-model="queryParams.queryParam.date"
              clearable
              type="daterange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              style="width: 230px"
            />
          </el-form-item>
        </el-form>

        <div>
          <el-button type="primary" @click="queryTableList">查询</el-button>
          <el-button @click="handleRestParams">重置</el-button>
        </div>
      </div>

      <div class="table-content">
        <el-table :data="tableData.data">
          <el-table-column label="责任通知名称" prop="responsibilityName" />
          <el-table-column label="状态" prop="statusName" width="100" />
          <el-table-column label="类型" prop="safetyAreaName" width="130" />
          <!-- <el-table-column label="下发数" prop="issuedQuantity" width="120" />
          <el-table-column label="反馈人数" prop="signedQuantity" width="120" />
          <el-table-column label="反馈比例" prop="signedRatio" width="120" /> -->
          <!-- <el-table-column label="安全责任人" prop="responsibilityPersonName" /> -->
          <el-table-column label="计划开始时间" prop="planStartTime" />
          <el-table-column label="计划完成时间" prop="planEndTime" />
          <el-table-column fixed="right" label="操作">
            <template #default="scope">
              <el-button
                type="primary"
                v-if="scope.row.status === 2"
                link
                @click="
                  $router.push({
                    name: 'responsibilityNoticeFeedback',
                    query: {
                      id: scope.row.id,
                    },
                  })
                "
                >反馈</el-button
              >
              <el-button type="primary" link @click="handleDownloadLink(scope)">下载</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container" v-if="tableData.total > 0">
        <el-pagination
          background
          layout="prev, pager, next, jumper,sizes, total"
          :current-page="queryParams.pageNumber"
          :page-size="queryParams.pageSize"
          :total="tableData.total"
          :page-sizes="[10, 20, 50, 100]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </main>
  </div>
</template>
<script lang="ts" setup>
  import { onMounted, ref, reactive } from 'vue';
  import dayjs from 'dayjs';
  import { ElMessage } from 'element-plus';
  import { useRouter } from 'vue-router';
  import { safetyNoticeDeptQueryPage } from '@/api/production-safety/responsibility-implementation';
  import { omit } from 'lodash-es';

  import { unformatAttachment } from '@/components/UploadFiles/utils';
  import { downloadFile } from '@/views/disaster/utils';
  import { useUserInfoHook } from '@/views/disaster/hooks';

  const { id } = useUserInfoHook();
  const router = useRouter();
  const activeTab = ref('');
  const groupList = ref<any[]>([]);
  const queryParams = reactive<any>({
    pageNumber: 1,
    pageSize: 10,
    queryParam: {
      responsibilityName: '',
      status: '',
      date: '',
      responsibilityPersonId: id,
    },
  });

  const tableData = reactive({
    data: [],
    total: 0,
  });

  const handleSizeChange = (value) => {
    queryParams.pageSize = value;
    queryTableList();
  };
  const handleCurrentChange = (value) => {
    queryParams.pageNumber = value;
    queryTableList();
  };

  const handleDownloadLink = (scope) => {
    const attachment = unformatAttachment(scope.row.attachment);
    attachment?.forEach((item: any) => {
      downloadFile(item.fileUrl, item.fileName);
    });
  };

  const queryTableList = () => {
    safetyNoticeDeptQueryPage({
      ...queryParams,
      queryParam: {
        ...omit(queryParams.queryParam, 'date'),
        startTime: queryParams.queryParam.date?.[0]
          ? dayjs(queryParams.queryParam.date?.[0]).format('YYYY-MM-DD')
          : undefined,
        endTime: queryParams.queryParam.date?.[1]
          ? dayjs(queryParams.queryParam.date?.[1]).format('YYYY-MM-DD')
          : undefined,
      },
    }).then((res) => {
      tableData.data = res.records;
      tableData.total = res.totalRow;
    });
  };
  const handleRestParams = () => {
    Object.assign(queryParams, {
      pageNumber: 1,
      pageSize: 10,
      queryParam: {
        responsibilityName: '',
        status: '',
        date: '',
        responsibilityPersonId: '',
      },
    });
    queryTableList();
  };

  onMounted(async () => {
    queryTableList();
  });
</script>

<style lang="scss" scoped>
  @use '@/styles/page-details-layout.scss' as *;
  @use '@/styles/page-main-layout.scss' as *;
  @use '@/styles/basic-table-action.scss' as *;

  :deep(.el-tabs__header) {
    margin: 0;
  }

  :deep(.el-tabs__item) {
    font-size: 14px !important;
  }

  :deep(.flexContent) {
    display: flex;
  }

  :deep(.breadcrumb .title) {
    margin-left: 0;
  }

  :deep(.el-form) {
    flex: 1;
    display: flex;
    row-gap: 15px;
    flex-wrap: wrap;
  }

  :deep(.el-form-item) {
    margin-bottom: 0;
  }

  :deep(main) {
    display: flex;
    flex-direction: column;
  }

  .search-form {
    min-width: 800px;
    display: flex;

    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: 20px;
  }

  .button-content {
    margin-bottom: 20px;
  }

  .table-content {
    flex: 1;
    overflow: hidden;
    overflow-y: auto;
  }

  .page-content {
    display: flex;
    justify-content: flex-end;
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
</style>
