<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form class="mb-[-16px]" :model="queryParams" inline>
        <el-form-item label="任务名称" prop="name">
          <el-input
            class="w-[280px]"
            v-model="queryParams.name"
            clearable
            placeholder="请输入任务名称"
          />
        </el-form-item>
        <el-form-item label="审核状态" prop="name">
          <el-select
            v-model="queryParams.check_status"
            placeholder="请选择审核状态"
            clearable
            class="w-[280px]"
          >
            <el-option
              v-for="(item, index) in checkStatusList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <el-table-column label="ID" prop="id" />
          <el-table-column
            label="任务名称"
            prop="task.title"
            show-overflow-tooltip
          />
          <el-table-column
            label="任务描述"
            prop="task.content"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="任务金额"
            prop="task.money"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="任务提交时间"
            prop="task.update_time"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="公司名称"
            prop="company_name"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="片区经理"
            prop="area_manager"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="审核状态"
            prop="check_status"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              <span v-if="row.check_status == 3" style="color: #f56c6c"
                >未通过</span
              >
              <span v-if="row.check_status == 2" style="color: #67c23a"
                >已通过</span
              >
              <span v-if="row.check_status == 1" style="color: #e6a23c"
                >待审核</span
              >
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button
                v-if="row.check_status <= 1"
                v-perms="['flow/edit']"
                type="primary"
                link
                @click="handleEdit(row, 'edit')"
              >
                审核
              </el-button>
              <el-button
                v-else
                type="primary"
                link
                @click="handleEdit(row, 'details')"
              >
                详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>
  
<script lang="ts" setup name="flowLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { apiApproveList } from "@/api/examined";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import EditPopup from "./index_list_popup.vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 是否显示编辑框
const showEdit = ref(false);

// 查询条件
const queryParams = reactive({
  name: "",
  check_status: "",
  type: 1,
});

const checkStatusList = ref([
  {
    id: 1,
    name: "待审核",
  },
  {
    id: 2,
    name: "已通过",
  },
  {
    id: 3,
    name: "未通过",
  },
]);

// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData("");

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiApproveList,
  params: queryParams,
});

// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

// 编辑
const handleEdit = async (data: any, type = "edit") => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open(type);
  editRef.value?.setFormData(data);
};

// 删除
// const handleDelete = async (id: number | any[]) => {
//   await feedback.confirm("确定要删除？");
//   await apiFlowDelete({ id });
//   getLists();
// };

getLists();
</script>
  