<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form class="mb-[-16px] formtabel" :model="queryParams" inline>
        <el-form-item label-width="100px" label="任务名称" prop="title">
          <el-input
            class="w-[280px]"
            v-model="queryParams.title"
            clearable
            placeholder="请输入任务名称"
          />
        </el-form-item>
        <el-form-item label-width="100px" label="创建人" prop="admin_id">
          <el-input
            class="w-[280px]"
            v-model="queryParams.admin_id"
            clearable
            placeholder="请输入创建人"
          />
        </el-form-item>
        <el-form-item label-width="100px" label="金额" prop="money">
          <el-input
            class="w-[280px]"
            v-model="queryParams.money"
            clearable
            placeholder="请输入金额"
          />
        </el-form-item>
        <el-form-item label-width="100px" label="任务类型" prop="type">
          <el-select
            v-model="queryParams.type"
            clearable
            placeholder="请输入任务类型"
          >
            <el-option
              v-for="item in datalist"
              :key="item.label"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label-width="100px" label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            clearable
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in statusdata"
              :key="item.label"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label-width="100px" label="任务描述" prop="content">
          <el-input
            class="w-[280px]"
            v-model="queryParams.content"
            clearable
            placeholder="请输入任务描述"
          />
        </el-form-item>
        <el-form-item label-width="100px" label="">
          <el-button class="el-btn" 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">
      <el-button
        v-perms="['task_template.task_template/add']"
        type="primary"
        @click="handleAdd"
      >
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button>
      <el-button
        v-perms="['task_template.task_template/delete']"
        :disabled="!selectData.length"
        @click="handleDelete(selectData)"
      >
        删除
      </el-button>
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column
            label="ID"
            width="80"
            prop="id"
            show-overflow-tooltip
          />
          <el-table-column
            label="任务名称"
            prop="title"
            show-overflow-tooltip
          />
          <el-table-column
            label="创建人"
            prop="admin_name"
            show-overflow-tooltip
          />
          <el-table-column label="金额" prop="money" show-overflow-tooltip />
          <el-table-column
            label="任务类型"
            prop="type_name"
            show-overflow-tooltip
          />
          <el-table-column label="状态" show-overflow-tooltip>
            <template #default="{ row }">
              <span>{{ row.status == 1 ? "显示" : "隐藏" }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="任务描述"
            prop="content"
            show-overflow-tooltip
          />
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button
                v-perms="['task_template.task_template/edit']"
                type="primary"
                link
                @click="handleEdit(row)"
              >
                编辑
              </el-button>
              <el-button
                v-perms="['task_template.task_template/delete']"
                type="danger"
                link
                @click="handleDelete(row.id)"
              >
                删除
              </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 && taskTypeId != 15"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
    <edit-admin-popup
      v-else-if="showEdit"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>

<script lang="ts" setup name="taskTemplateLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import {
  apiTaskTemplateLists,
  apiTaskTemplateDelete,
} from "@/api/task_template";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
import EditAdminPopup from "./edit_admin.vue";
import { dictDataLists } from "@/api/setting/dict";
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const datalist = ref([]);
// 是否显示编辑框
const showEdit = ref(false);

const route = useRoute();

// 查询条件
const queryParams = reactive({
  id: "", //任务公司排期id
  company_id: "",
  title: "",
  admin_id: "",
  money: "",
  type: "",
  status: "",
  content: "",
});
const taskTypeId = ref(10);
if (route.query?.id) queryParams.id = route.query.id;
if (route.query?.company_id) queryParams.company_id = route.query.company_id;
if (route.query?.company_type == 41) taskTypeId.value = 15;
const statusdata = reactive([
  { id: 1, name: "显示" },
  { id: 2, 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: apiTaskTemplateLists,
  params: queryParams,
});
// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
  editRef.value?.setFormData({ company_id: queryParams.company_id });
};

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

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm("确定要删除？");
  await apiTaskTemplateDelete({ id });
  getLists();
};
//任务类型接口
dictDataLists({ type_id: 10 }).then((res) => {
  datalist.value = res.lists;
});
getLists();
</script>
<style lang="scss" scoped>
.formtabel {
  .el-form-item {
    width: 20%;
    .el-btn {
      margin-left: 60px;
    }
  }
}
</style>

