<template>
  <div class="h-full flex flex-col p-2">
    <Pageheader title="报修管理"></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" :model="queryParams" @submit.prevent="handleQuery" :inline="true">
            <el-form-item label="报修状态" prop="problemStatus">
              <el-select v-model="queryParams.problemStatus" placeholder="请选择" clearable>
                <el-option v-for="dict in com_repair_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="预约日期" prop="maintenanceItemId">
              <el-date-picker
                clearable
                v-model="queryParams.orderDate"
                placeholder="请选择"
                type="date"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
              />
            </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">
          <!-- <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['repair:add:repair']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['repair:edit:repair']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['repair:repair:remove']"
              >删除</el-button
            >
          </el-col> -->
          <right-toolbar
            :disable-delete="multiple"
            :disable-edit="single"
            :show-add="checkPermi(['repair:repair:add'])"
            :show-edit="checkPermi(['repair:repair:edit'])"
            :show-delete="checkPermi(['repair:repair:remove'])"
            @update:add="handleAdd"
            @update:edit="handleUpdate()"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="repairList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="报修房屋" align="center" prop="houseName" />
        <el-table-column label="维修项目" align="center" prop="maintenanceItemName" />
        <el-table-column label="问题描述" align="center" prop="problem" />
        <el-table-column label="报修人" align="center" prop="residentName" />
        <el-table-column label="预约日期" align="center" prop="orderDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="维修状态" align="center" prop="problemStatus">
          <template #default="scope">
            <dict-tag :options="com_repair_status" :value="scope.row.problemStatus || ''"></dict-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleShare(scope.row)" v-hasPermi="['repair:repair:query']">{{
              scope.row.problemStatus === '0' ? '分配' : '详情'
            }}</el-button>
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['repair:repair:edit']">编辑</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['repair:repair: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="Repair" lang="ts">
import { listRepair, delRepair } from '@/api/system/Repair/index';
import { RepairVO, RepairQuery, RepairForm } from '@/api/system/Repair/type';
import { listRepairProject } from '@/api/system/RepairProject';
import { listResident } from '@/api/system/resident';
import { checkPermi } from '@/utils/permission';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_repair_status } = toRefs<any>(proxy?.useDict('com_repair_status'));

interface repairlistType extends RepairVO {
  maintenanceItemPath?: string;
  residentName?: string;
}
const repairList = ref<repairlistType[]>([]);
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 router = useRouter();

const queryFormRef = ref<ElFormInstance>();

const initFormData: RepairForm = {
  id: undefined,
  houseId: undefined,
  maintenanceItemId: undefined,
  problem: undefined,
  residentId: undefined,
  phone: undefined,
  orderDate: undefined,
  problemImageUrl: undefined,
  createBy: undefined,
  createTime: undefined,
  updateBy: undefined,
  updateTime: undefined,
  problemStatus: undefined,
  repairDes: undefined,
  repairImageUrl: undefined,
  repairId: undefined
};
const data = reactive<PageData<RepairForm, RepairQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    houseId: undefined,
    maintenanceItemId: undefined,
    problem: undefined,
    residentId: undefined,
    phone: undefined,
    orderDate: undefined,
    problemImageUrl: undefined,
    problemStatus: undefined,
    repairDes: undefined,
    repairImageUrl: undefined,
    repairId: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '报修管理表id不能为空', trigger: 'blur' }]
  }
});

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

// != 维修项目u ===========================================================================

/** 查询报修管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listRepair(queryParams.value);
  repairList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

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

/** 修改按钮操作 */
const handleUpdate = async (row?: RepairVO) => {
  const _id = row.id || ids.value[0];
  router.push({
    path: '/repair/editRepair',
    query: {
      id: _id
    }
  });
};
/** 分配人员 */
const handleShare = (row) => {
  const _id = row.id || ids.value[0];
  router.push({
    path: '/repair/repairMain',
    query: {
      id: _id
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: RepairVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('该信息删除后无法恢复，确定要删除吗？').finally(() => (loading.value = false));
  await delRepair(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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