<template>
  <div class="flex flex-col h-full p-2">
    <pageheader title="办事记录"></pageheader>
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" @submit.prevent="handleQuery" :inline="true">
            <el-form-item label="求助人" prop="seekName">
              <el-input v-model="queryParams.seekName" placeholder="请输入求助人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card class="flex-1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar
            :disable-delete="multiple"
            :disable-edit="single"
            :show-add="checkPermi(['handleLog:handleLog:add'])"
            :show-edit="checkPermi(['handleLog:handleLog:edit'])"
            :show-delete="checkPermi(['handleLog:handleLog:remove'])"
            @update:add="handleAdd"
            @update:edit="handleUpdate()"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="handleLogList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="办事内容" align="center" prop="content">
          <template #default="scope">
            <div class="color-blue" v-html="scope.row.content"></div>
          </template>
        </el-table-column>
        <el-table-column label="求助人" align="center" prop="seekName" width="120" />
        <el-table-column label="联系方式" align="center" prop="phone" width="120" />
        <el-table-column label="办事地点" align="center" prop="handlePosition" />
        <el-table-column label="办事时间" align="center" prop="handleTime" width="120">
          <template #default="scope">
            <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="附属材料" width="100" align="center" prop="handlePosition">
          <template #default="scope">
            <el-button type="primary" @click="openDiage(scope.row)" link>点击查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-has-permi="['handleLog:handleLog:edit']">编辑</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-has-permi="['handleLog:handleLog:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      <!-- 添加或修改办事记录对话框 -->
      <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
        <ul class="ulbox" v-if="dialog.files.length > 0">
          <li @click="handleDown(item)" class="files_item" v-for="(item, index) in dialog.files" :key="index">
            <div>
              {{ item.fileName }}
            </div>
            <span class="clickdown">点击下载</span>
          </li>
        </ul>
        <ul class="ulbox" v-else>
          <li class="files_item_no">
            <div>暂无数据</div>
          </li>
        </ul>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup name="HandleLog" lang="ts">
import { listHandleLog, delHandleLog } from '@/api/system/HandleLog/index';
import { HandleLogVO, HandleLogQuery, HandleLogForm } from '@/api/system/HandleLog/type';
import { checkPermi } from '@/utils/permission';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const handleLogList = ref<HandleLogVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const handleLogFormRef = ref<ElFormInstance>();

const initFormData: HandleLogForm = {
  id: undefined,
  content: undefined,
  seekName: undefined,
  phone: undefined,
  handleTime: undefined,
  handlePosition: undefined
};
const data = reactive<PageData<HandleLogForm, HandleLogQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    content: undefined,
    seekName: undefined,
    phone: undefined,
    handleTime: undefined,
    handlePosition: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '办事记录表id不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询办事记录列表 */
const getList = async () => {
  loading.value = true;
  listHandleLog(queryParams.value)
    .then((res) => {
      handleLogList.value = res.rows;
      total.value = res.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: HandleLogVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

const router = useRouter();
/** 新增按钮操作 */
const handleAdd = () => {
  router.push({
    path: '/repair/addHandleLog'
  });
};

/** 修改按钮操作 */
const handleUpdate = async (row?: HandleLogVO) => {
  const _id = row ? row.id : ids.value[0];
  router.push({
    path: '/repair/editHandleLog',
    query: {
      id: _id
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: HandleLogVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('该信息删除后无法恢复，确定要删除吗？').finally(() => (loading.value = false));
  await delHandleLog(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};
const dialog = reactive({
  visible: false,
  title: '',
  files: []
});

const openDiage = (row) => {
  dialog.visible = true;
  dialog.title = '查看附属材料';
  console.log(row);
  dialog.files = row.handleLogFileList ?? [];
};
const handleDown = (item) => {
  proxy?.$download.oss(item.ossId);
};
onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.el-table {
  height: calc(100% - 80px);
}
.ulbox {
  max-height: 300px;
  overflow-y: auto;
  overflow-x: hidden;
}
.files_item_no {
  text-align: center;
  color: #999;
  padding: 10px;
}
.files_item {
  height: 40px;
  line-height: 40px;
  box-sizing: border-box;
  padding: 0 10px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  &:hover {
    background-color: #f5f5f5;
    color: #409eff;
    .clickdown {
      color: #409eff;
    }
  }

  .clickdown {
    color: #cccc;
    font-size: small;
  }
}
</style>
