<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="name">
              <el-input v-model="queryParams.name" placeholder="请输入班组名称" clearable @keyup.enter="handleQuery" />
            </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="hover">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
          </el-col>
          <right-toolbar v-model:show-search="showSearch" @query-table="getList" />
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="teamList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" prop="id" min-width="160" />
        <el-table-column label="名称" prop="name" min-width="140" />
        <el-table-column label="开始时间" prop="startTime" align="center" width="120" />
        <el-table-column label="结束时间" prop="endTime" align="center" width="120" />
        <el-table-column label="预留1" prop="reserve1" min-width="120" />
        <el-table-column label="预留2" prop="reserve2" min-width="120" />
        <el-table-column label="预留3" prop="reserve3" min-width="120" />
        <el-table-column label="预留4" prop="reserve4" min-width="120" />
        <el-table-column label="预留5" prop="reserve5" min-width="120" />
        <el-table-column label="更新时间" prop="updateTime" align="center" width="170" />
        <el-table-column fixed="right" label="操作" align="center" width="120">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <el-dialog v-model="dialog.visible" :title="dialog.title" append-to-body width="560px">
      <el-form ref="teamFormRef" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入班组名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-time-picker v-model="form.startTime" value-format="HH:mm:ss" placeholder="请选择开始时间" />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-time-picker v-model="form.endTime" value-format="HH:mm:ss" placeholder="请选择结束时间" />
        </el-form-item>
        <el-form-item label="预留1" prop="reserve1">
          <el-input v-model="form.reserve1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留2" prop="reserve2">
          <el-input v-model="form.reserve2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留3" prop="reserve3">
          <el-input v-model="form.reserve3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留4" prop="reserve4">
          <el-input v-model="form.reserve4" placeholder="请输入预留字段4" />
        </el-form-item>
        <el-form-item label="预留5" prop="reserve5">
          <el-input v-model="form.reserve5" placeholder="请输入预留字段5" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ShiftTeam" lang="ts">
import { addShiftTeam, delShiftTeam, getShiftTeam, listShiftTeam, updateShiftTeam } from '@/api/ems/shiftTeam';
import { ShiftTeamForm, ShiftTeamQuery, ShiftTeamVO } from '@/api/ems/shiftTeam/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

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

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

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ShiftTeamForm = {
  id: undefined,
  name: '',
  startTime: '',
  endTime: '',
  reserve1: '',
  reserve2: '',
  reserve3: '',
  reserve4: '',
  reserve5: ''
};

const data = reactive<PageData<ShiftTeamForm, ShiftTeamQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: ''
  },
  rules: {
    name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
    startTime: [{ required: true, message: '开始时间不能为空', trigger: 'change' }],
    endTime: [{ required: true, message: '结束时间不能为空', trigger: 'change' }]
  }
});

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

const getList = async () => {
  loading.value = true;
  try {
    const res = await listShiftTeam(queryParams.value);
    teamList.value = res.rows || [];
    total.value = res.total || 0;
  } finally {
    loading.value = false;
  }
};

const reset = () => {
  form.value = { ...initFormData };
  teamFormRef.value?.resetFields();
};

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

const handleSelectionChange = (selection: ShiftTeamVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '新增班组';
};

const handleUpdate = async (row?: ShiftTeamVO) => {
  reset();
  const id = row?.id || ids.value[0];
  const res = await getShiftTeam(id);
  form.value = { ...res.data };
  dialog.visible = true;
  dialog.title = '修改班组';
};

const submitForm = () => {
  teamFormRef.value?.validate(async (valid: boolean) => {
    if (!valid) {
      return;
    }
    if (form.value.id) {
      await updateShiftTeam(form.value);
    } else {
      await addShiftTeam(form.value);
    }
    proxy?.$modal.msgSuccess('操作成功');
    dialog.visible = false;
    await getList();
  });
};

const handleDelete = async (row?: ShiftTeamVO) => {
  const deleteIds = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除班组编号为"' + deleteIds + '"的数据项？');
  await delShiftTeam(deleteIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

const cancel = () => {
  dialog.visible = false;
  reset();
};

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