<template>
  <div class="h-full p-2 flex flex-col">
    <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" label-width="120px" :model="queryParams" :inline="true">
            <el-form-item label="投诉类型" prop="typeId">
              <el-select v-model="queryParams.typeId" placeholder="请选择投诉类型" style="width: 240px">
                <el-option :disabled="item.status === '1'" v-for="item in complaintyTyplist" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="投诉人姓名" prop="complaintName">
              <el-input v-model="queryParams.complaintName" 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 class="flex-1" shadow="never">
      <el-table v-loading="loading" border :data="complaintList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="投诉类型" align="center" prop="typeName" />
        <el-table-column label="问题描述" align="center" prop="problemDes" />
        <el-table-column label="投诉人姓名" align="center" prop="complaintName" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="投诉时间" align="center" prop="createTime" />
        <el-table-column label="投诉状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="com_complaint_status" :value="scope.row.status"></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="handleUpdate(scope.row)" v-hasPermi="['complaint:complaint:edit']">查看详情</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>
<!-- complaint -->
<script setup name="Complaint" lang="ts">
import { listComplaint, getComplaint, delComplaint, addComplaint, updateComplaint } from '@/api/system/Complaint/index';
import { ComplaintVO, ComplaintQuery, ComplaintForm } from '@/api/system/Complaint/type';
import { listComplaintType } from '@/api/system/ComplainType';

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

const complaintList = ref<ComplaintVO[]>([]);
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 complaintFormRef = ref<ElFormInstance>();

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

const initFormData: ComplaintForm = {
  id: undefined,
  typeId: undefined,
  problemDes: undefined,
  complaintName: undefined,
  phone: undefined,
  problemIamgeUrl: undefined,
  status: undefined,
  handleId: undefined,
  handleName: undefined,
  handleContent: undefined
};
const data = reactive<PageData<ComplaintForm, ComplaintQuery>>({
  form: { ...initFormData },
  queryParams: {
    villageId: Number(localStorage.getItem('villageid')),
    pageNum: 1,
    pageSize: 10,
    typeId: undefined,
    problemDes: undefined,
    complaintName: undefined,
    phone: undefined,
    problemIamgeUrl: undefined,
    status: undefined,
    handleId: undefined,
    handleName: undefined,
    handleContent: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '投诉管理表id不能为空', trigger: 'blur' }]
  }
});

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

const complaintyTyplist = ref([]);
/** 查询投诉管理列表 */
const getList = async () => {
  loading.value = true;
  const res2 = await listComplaintType();
  complaintyTyplist.value = res2.rows;
  const res = await listComplaint(queryParams.value);
  complaintList.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: ComplaintVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

const router = useRouter();
/** 详情按钮操作 */
const handleUpdate = async (row?: ComplaintVO) => {
  router.push({
    path: '/repair/complainMain',
    query: {
      id: row.id
    }
  });
};

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

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

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'complaint/complaint/export',
    {
      ...queryParams.value
    },
    `complaint_${new Date().getTime()}.xlsx`
  );
};

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

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