<template>
  <div class="flex flex-col h-full 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 label-width="120px" ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="申请人姓名" prop="applyName">
              <el-input v-model="queryParams.applyName" placeholder="请输入申请人姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="装修验收结果" prop="inspectionResult">
              <el-select v-model="queryParams.inspectionResult" placeholder="请选择装修验收结果" style="width: 240px">
                <el-option v-for="item in com_decoration_status" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </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 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="['decoration:decoration:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['decoration:decoration:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['decoration:decoration:remove']"
              >删除</el-button
            >
          </el-col>

          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="decorationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="小区名称" align="center" prop="villageName" />
        <el-table-column label="房屋号" align="center" prop="houseName" />
        <el-table-column label="申请人姓名" align="center" prop="applyName" />
        <el-table-column label="装修公司" align="center" prop="decorationCompany" />
        <el-table-column label="装修内容" align="center" prop="decorationContent" />
        <el-table-column label="装修押金" align="center" prop="decorationDeposit" />
        <el-table-column label="验收结果" align="center" prop="inspectionResult">
          <template #default="scope">
            <dict-tag :options="com_decoration_status" :value="Number(scope.row.inspectionResult)"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center" prop="createTime" />
        <el-table-column label="验收人" align="center" prop="inspectionName" />
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button
              link
              type="primary"
              v-if="scope.row.inspectionResult !== '1'"
              @click="handleInspection(scope.row)"
              v-hasPermi="['decoration:decoration:edit']"
              >验收</el-button
            >
            <el-button link type="primary" v-else @click="handleUpdate(scope.row)" v-hasPermi="['decoration:decoration:edit']">详情</el-button>
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['decoration:decoration:edit']">修改</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['decoration:decoration: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>

    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="decorationFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="验收人姓名" prop="inspectionName">
          <el-input v-model="form.inspectionName" placeholder="请输入验收人姓名" />
        </el-form-item>
        <el-form-item label="验收结果" prop="inspectionResult">
          <el-select v-model="form.inspectionResult" placeholder="请选择装修验收结果">
            <el-option
              v-for="item in com_decoration_status.filter((item) => item.value !== '0')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入" :rows="5"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Decoration" lang="ts">
import { listDecoration, delDecoration, updateDecoration } from '@/api/system/Decoration/index';
import { DecorationVO, DecorationQuery, DecorationForm } from '@/api/system/Decoration/type';

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

const decorationList = ref<DecorationVO[]>([]);
const buttonLoading = ref(false);
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 decorationFormRef = ref<ElFormInstance>();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: DecorationForm = {
  id: undefined,
  villageId: undefined,
  houseId: undefined,
  applyName: undefined,
  decorationCompany: undefined,
  decorationContent: undefined,
  decorationDeposit: undefined,
  inspectionResult: undefined,
  remark: undefined,
  inspectionName: undefined
};
const data = reactive<PageData<DecorationForm, DecorationQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    houseId: undefined,
    applyName: undefined,
    decorationCompany: undefined,
    decorationContent: undefined,
    decorationDeposit: undefined,
    inspectionResult: undefined,
    inspectionName: undefined,
    params: {}
  },
  rules: {
    inspectionName: [{ required: true, message: '验收人姓名不能为空', trigger: 'blur' }],
    inspectionResult: [{ required: true, message: '验收结果不能为空', trigger: 'blur' }]
  }
});

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

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

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  decorationFormRef.value?.resetFields();
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

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

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

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

/** 修改按钮操作 */
const handleUpdate = async (row?: DecorationVO) => {
  router.push({
    path: '/repair/addDecoration',
    query: {
      id: row.id
    }
  });
};

/**  验收 */
const handleInspection = (row: DecorationVO) => {
  form.value = {
    ...form.value,
    ...row
  };
  dialog.visible = true;
  dialog.title = '验收';
};

/** 提交按钮 */
const submitForm = () => {
  decorationFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateDecoration(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

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

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

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