<template>
  <div class="h-full flex flex-col p-2">
    <pageheader></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" label-width="100px" @submit.prevent="handleQuery" :model="queryParams" :inline="true">
            <el-form-item label="执行状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择执行状态" clearable>
                <el-option v-for="(item, index) in com_inspection_task_status" :key="index" :value="item.value" :label="item.label"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="巡检任务名称" prop="taskName">
              <el-input maxlength="20" v-model="queryParams.taskName" placeholder="请输入巡检任务名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="实际开始时间" prop="taskTime">
              <el-date-picker
                clearable
                v-model="queryParams.taskTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择实际开始时间"
              />
            </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(['system:task:add'])"
            :show-edit="checkPermi(['system:task:edit'])"
            :show-delete="checkPermi(['system:task:remove'])"
            @update:add="handleAdd"
            @update:edit="handleUpdate()"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="taskList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="巡检任务名称" align="center" prop="taskName" />
        <el-table-column label="巡检人员" align="center" prop="inspectorName" />
        <el-table-column label="起止日期" align="center" prop="taskTime" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="com_inspection_task_status" :value="scope.row.status"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="提醒时间" width="160" align="center" prop="remindTime">
          <template #default="scope">
            <dict-tag :options="com_inspection_remind_tiime" :value="scope.row.remindTime"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="关联巡检计划" align="center" prop="planName" />
        <el-table-column label="操作" 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="['system:task:edit']">编辑</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-has-permi="['system:task: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>
  </div>
</template>

<script setup name="Task" lang="ts">
import { listTask, delTask } from '@/api/system/InspectionTask';
import { TaskVO, TaskQuery, TaskForm } from '@/api/system/InspectionTask/type';
import { checkPermi } from '@/utils/permission';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_inspection_task_status } = toRefs<any>(proxy?.useDict('com_inspection_task_status'));
const { com_inspection_remind_tiime } = toRefs<any>(proxy?.useDict('com_inspection_remind_tiime'));
const router = useRouter();
const taskList = ref<TaskVO[]>([]);
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 initFormData: TaskForm = {
  id: undefined,
  taskName: undefined,
  status: undefined
};
const data = reactive<PageData<TaskForm, TaskQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskName: undefined,
    status: undefined
  },
  rules: {
    id: [{ required: true, message: '主键ID不能为空', trigger: 'blur' }],
    taskName: [{ required: true, message: '巡检任务名称不能为空', trigger: 'blur' }]
  }
});

const { queryParams } = toRefs(data);

/** 查询巡检任务列表 */
const getList = async () => {
  loading.value = true;
  listTask(queryParams.value)
    .then((res) => {
      taskList.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: TaskVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

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

/** 修改按钮操作 */
const handleUpdate = (row?: TaskVO) => {
  const _ids = row?.id || ids.value[0];
  router.push({
    path: '/inspection/editTask',
    query: {
      id: _ids
    }
  });
};

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

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

<style lang="scss" scoped>
.el-table {
  height: calc(100% - 80px);
}
</style>
