<template>
  <div class="system-busWeeklySecurityReport-container">
    <el-card shadow="hover">
      <div class="system-busWeeklySecurityReport-search mb15">
        <el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
          <el-row>
            <el-col :span="1.5">
              <el-form-item label="周期范围" prop="scopeDate">
                <el-date-picker
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                  v-model="scopeDate"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </el-form-item>
            </el-col>
            <el-col :span="1.5">
              <el-form-item>
                <el-button type="primary" @click="onSearch"
                  ><el-icon><ele-Search /></el-icon>搜索</el-button
                >
                <el-button @click="resetQuery(queryRef)"
                  ><el-icon><ele-Refresh /></el-icon>重置</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" @click="handleAdd" v-auth="'api/v1/system/busWeeklySecurityReport/add'"
              ><el-icon><ele-Plus /></el-icon>新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" :disabled="multiple" @click="handleDelete(null)" v-auth="'api/v1/system/busWeeklySecurityReport/delete'"
              ><el-icon><ele-Delete /></el-icon>删除</el-button
            >
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="loading" :data="tableData.data" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" min-width="50px" />
        <el-table-column label="周" align="center" prop="week" min-width="100px" />
        <el-table-column label="周期范围" align="center" prop="scope" min-width="200px">
          <template #default="scope">
            <span>{{ scope.row.scope + '至' + scope.row.scopeEnd }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="文件位置" align="center" prop="path" min-width="100px" /> -->
        <el-table-column label="创建时间" align="center" prop="createdAt" min-width="100px">
          <template #default="scope">
            <span>{{ scope.row.createdAt }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="handleView(scope.row)"
              ><el-icon><ele-View /></el-icon>预览</el-button
            >
            <el-button type="success" link @click="handleUpdate(scope.row)" v-auth="'api/v1/system/busWeeklySecurityReport/edit'"
              ><el-icon><ele-EditPen /></el-icon>修改</el-button
            >
            <el-button type="danger" link @click="handleDelete(scope.row)" v-auth="'api/v1/system/busWeeklySecurityReport/delete'"
              ><el-icon><ele-DeleteFilled /></el-icon>删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="tableData.total > 0"
        :total="tableData.total"
        v-model:page="tableData.param.pageNum"
        v-model:limit="tableData.param.pageSize"
        @pagination="busWeeklySecurityReportList"
      />
    </el-card>
    <apiV1SystemBusWeeklySecurityReportAdd
      ref="addRef"
      @busWeeklySecurityReportList="busWeeklySecurityReportList"
    ></apiV1SystemBusWeeklySecurityReportAdd>
    <documentsEdit ref="documentsEditRef" v-if="documentsEditShow" @onClose="documentsEditShow = false"></documentsEdit>
    <documentsDetail ref="documentsDetailRef" v-if="documentsDetailShow" @onClose="documentsDetailShow = false"></documentsDetail>
  </div>
</template>
<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw, nextTick } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import {
  addSafetyWeeklyReport,
  delSafetyWeeklyReport,
  getSafetyWeeklyReport,
  listSafetyWeeklyReport,
  updateSafetyWeeklyReport
} from '@/api/safety/safetyWeeklyReport';
import apiV1SystemBusWeeklySecurityReportAdd from './component/add.vue';
// 预览文档
import documentsDetail from './component/documentsDetail/index.vue';
// 编辑文档
import documentsEdit from './component/documentsEdit/index.vue';
// 获取用户 store
import { useUserStoreHook } from '@/store/modules/user';
const userStore = useUserStoreHook();
// 从 store 中获取项目列表和当前选中的项目
const currentProject = computed(() => userStore.selectedProject);
export default defineComponent({
  name: 'index',
  components: {
    apiV1SystemBusWeeklySecurityReportAdd,
    documentsDetail,
    documentsEdit
  },
  setup() {
    const { proxy } = <any>getCurrentInstance();
    const loading = ref(false);
    const queryRef = ref();
    const addRef = ref();
    // 是否显示所有搜索选项
    const showAll = ref(false);
    const documentsDetailRef = ref(false);
    const documentsEditRef = ref(false);
    // 非单个禁用
    const single = ref(true);
    // 非多个禁用
    const multiple = ref(true);
    // 字典选项数据
    const state = reactive({
      ids: [],
      documentsDetailShow: false,
      documentsEditShow: false,
      tableData: {
        data: [],
        total: 0,
        loading: false,
        param: {
          pageNum: 1,
          pageSize: 10,
          id: undefined,
          projectId: currentProject.value?.goId,
          projectName: undefined,
          scope: undefined,
          scopeEnd: undefined,
          path: undefined,
          createBy: undefined,
          updateBy: undefined,
          createdAt: undefined,
          dateRange: [],
          scopeDate: ''
        }
      },
      projectList: [],
      scopeDate: [] //时间范围
    });
    // 页面加载时
    onMounted(() => {
      initTableData();
    });
    // 初始化表格数据
    const initTableData = () => {
      busWeeklySecurityReportList();
    };
    /** 重置按钮操作 */
    const resetQuery = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      state.scopeDate = [];
      state.tableData.param.scopeDate = '';
      formEl.resetFields();
      busWeeklySecurityReportList();
    };
    const onSearch = () => {
      state.tableData.param.scopeDate = state.scopeDate.join(',');
      busWeeklySecurityReportList();
    };
    // 获取列表数据
    const busWeeklySecurityReportList = () => {
      loading.value = true;
      listSafetyWeeklyReport(state.tableData.param).then((res: any) => {
        let list = res.data.list ?? [];
        state.tableData.data = list;
        state.tableData.total = res.data.total;
        loading.value = false;
      });
    };
    // 多选框选中数据
    const handleSelectionChange = (selection) => {
      state.ids = selection.map((item) => item.id);
      single.value = selection.length != 1;
      multiple.value = !selection.length;
    };
    const handleAdd = () => {
      addRef.value.openDialog();
    };
    const handleUpdate = (row) => {
      state.documentsEditShow = true;
      nextTick(() => {
        documentsEditRef.value.openDialog(row);
      });
    };
    const handleDelete = (row) => {
      let msg = '你确定要删除所选数据？';
      let id: number[] = [];
      if (row) {
        msg = `此操作将永久删除数据，是否继续?`;
        id = [row.id];
      } else {
        id = state.ids;
      }
      if (id.length === 0) {
        ElMessage.error('请选择要删除的数据。');
        return;
      }
      ElMessageBox.confirm(msg, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delSafetyWeeklyReport(id).then(() => {
            ElMessage.success('删除成功');
            busWeeklySecurityReportList();
          });
        })
        .catch(() => {});
    };
    const handleView = (row) => {
      state.documentsDetailShow = true;
      nextTick(() => {
        documentsDetailRef.value.openDialog(row);
      });
    };

    return {
      proxy,
      addRef,
      showAll,
      loading,
      single,
      multiple,
      documentsEditRef,
      documentsDetailRef,
      queryRef,
      resetQuery,
      onSearch,
      busWeeklySecurityReportList,
      handleSelectionChange,
      handleAdd,
      handleUpdate,
      handleDelete,
      handleView,
      ...toRefs(state)
    };
  }
});
</script>
<style lang="scss" scoped>
.colBlock {
  display: block;
}
.colNone {
  display: none;
}
</style>
