<template>
  <div class="p-2">
    <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" :inline="true">
            <el-form-item label="项目编号" prop="projectNo">
              <el-input v-model="queryParams.projectNo" placeholder="请输入项目编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="提交时间" prop="createTime">
              <el-date-picker
                v-model="dateRange"
                type="daterange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['customer:projectInfo:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['customer:projectInfo:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['customer:projectInfo:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['customer:projectInfo:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="projectInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="项目编号" align="center" prop="projectNo" />
        <el-table-column label="项目logo" align="center" prop="projectLogo" width="200">
          <template #default="scope">
            <el-image v-if="scope.row.projectLogo" :src="scope.row.projectLogo" style="width: 60px; height: 60px" fit="cover" />
            <span v-else>暂无图片</span>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <!-- <el-table-column label="项目类型" align="center" prop="projectType" /> -->
        <el-table-column label="负责人" align="center" prop="leaderName" />
        <!-- <el-table-column label="状态" align="center" prop="status" /> -->
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['customer:projectInfo:edit']">编辑</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['customer:projectInfo: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-card>
    <!-- 添加或修改项目信息抽屉 -->
    <AddEditDrawer v-model="drawerVisible" :title="drawerTitle" :form-data="currentFormData" @success="handleDrawerSuccess" />
  </div>
</template>

<script setup name="ProjectInfo" lang="ts">
import { listProjectInfo, getProjectInfo, delProjectInfo } from '@/api/project/projectInfo';
import AddEditDrawer from './addEditDrawer.vue';
import { ProjectInfoVO, ProjectInfoQuery, ProjectInfoForm } from '@/api/project/projectInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const projectInfoList = ref<ProjectInfoVO[]>([]);
const drawerVisible = ref(false);
const drawerTitle = ref('');
const currentFormData = ref<ProjectInfoForm>();
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 data = reactive<PageData<ProjectInfoForm, ProjectInfoQuery>>({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectNo: undefined,
    projectLogo: undefined,
    projectName: undefined,
    projectType: undefined,
    leaderId: undefined,
    leaderName: undefined,
    status: undefined,
    platformCode: undefined,
    params: {}
  },
  rules: {}
});

const { queryParams } = toRefs(data);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
/** 查询项目信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listProjectInfo(proxy?.addDateRange(queryParams.value, dateRange.value));
  projectInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 抽屉成功回调 */
const handleDrawerSuccess = () => {
  getList();
};

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

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

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

/** 新增按钮操作 */
const handleAdd = () => {
  currentFormData.value = undefined;
  drawerTitle.value = '添加项目信息';
  drawerVisible.value = true;
};

/** 修改按钮操作 */
const handleUpdate = async (row?: ProjectInfoVO) => {
  const _id = row?.id || ids.value[0];
  const res = await getProjectInfo(_id);
  currentFormData.value = res.data;
  drawerTitle.value = '修改项目信息';
  drawerVisible.value = true;
};

/** 删除按钮操作 */
const handleDelete = async (row?: ProjectInfoVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除项目信息编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delProjectInfo(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'customer/projectInfo/export',
    {
      ...queryParams.value
    },
    `projectInfo_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
