<template>
  <div class="app-container">
    <!-- 顶部区域 -->
    <el-form inline ref="queryFormRef" :model="queryParams">
      <el-row>
        <el-col>
          <el-form-item>
            <el-radio-group
                v-model="queryParams.status"
                size="small"
                @change="handleQuery"
            >
              <!-- <el-radio-button :label="1">已发布</el-radio-button> -->
              <el-radio-button :label="0">待审核</el-radio-button>
              <el-radio-button :label="1">已通过</el-radio-button>
              <el-radio-button :label="2">已驳回</el-radio-button>
              <el-radio-button :label="4">已结束</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="需求类型" prop="category">
            <!-- <SiteOptions v-model="queryParams.tenantId" :site-list="siteList" /> -->
            <el-select v-model="queryParams.category">
              <el-option label="企业" value="1"/>
              <el-option label="专家" value="2"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button
              type="primary"
              icon="search"
              size="default"
              @click="handleQuery"
          >搜索
          </el-button
          >
          <el-button icon="refresh" size="default" @click="resetQuery"
          >重置
          </el-button
          >
        </el-col>
      </el-row>
    </el-form>
    <el-row class="mb8">
      <el-button
          v-if="queryParams.status == 0"
          size="small"
          type="primary"
          :disabled="!selectedIds.length"
          @click="handleBatchApproval"
      >批量审核
      </el-button
      >
      <el-button
          size="small"
          type="danger"
          :disabled="!selectedIds.length"
          @click="handleDelete(...selectedIds)"
      >批量删除
      </el-button
      >
      <el-button
          size="small"
          type="primary"
          @click="userStore.resetUnApproval('service', 'unApprovedService')"
      >未读归零
      </el-button
      >
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80"></el-table-column>
      <el-table-column label="编号" align="center" prop="id"></el-table-column>
      <!-- <el-table-column
        label="需求名称"
        align="center"
        prop="title"
      ></el-table-column> -->
      <el-table-column label="需求类型" align="center">
        <template #default="{ row }">
          {{ categoryMap[row.category] }}需求
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="所属领域"
        align="center"
        prop="industryStr"
      ></el-table-column> -->
      <el-table-column
          label="联系人"
          align="center"
          prop="name"
      ></el-table-column>
      <el-table-column
          label="手机"
          align="center"
          prop="mobile"
      ></el-table-column>
      <!-- <el-table-column
        label="所属经纪人"
        align="center"
        prop=""
      ></el-table-column>
      <el-table-column
        label="经纪人电话"
        align="center"
        prop=""
      ></el-table-column> -->
      <!-- <el-table-column label="站点" align="center">
        <template #default="{ row }">
          {{
            siteList.find((el) => el.id === row.tenantId)?.name ?? "无"
          }}</template
        >
      </el-table-column> -->
      <el-table-column
          label="发布时间"
          align="center"
          prop="createTime"
      ></el-table-column>
      <!-- <el-table-column
        label="截止日期"
        align="center"
        prop="deadline"
      ></el-table-column> -->
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" size="small" @click="approval(row.id)"
          >审批
          </el-button
          >
          <el-button type="text" size="small" @click="complete(row.id)"
          >完成
          </el-button
          >
          <el-button
              type="text"
              size="small"
              icon="delete3"
              @click="handleDelete(row.id)"
          >删除
          </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"
    />
  </div>
</template>
<script setup>
// import { businessList } from "@/api/Businessneeds/index.js";
import {
  listCasDemand,
  deleteCasDemand,
  batchCasDemand,
  updateCasDemand,
} from "@/api/dataApproval/enterpriseServiceDemand";

// import Edialog from "@/views/components/Businessneeds/Approvaldetails.vue";
// import SiteOptions from "@/views/components/SiteOptions";
import {tenantSelect} from "@/api/subPlatform/tenant";
import {ref, toRefs} from "vue";
// import { cloneDeep } from "lodash";
import {useRouter} from "vue-router";
import useUserStore from "@/store/modules/user";
import {ElMessage, ElMessageBox} from "element-plus";

const router = useRouter();
const userStore = useUserStore();
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 0,
  },
  demandForm: {},
});

const categoryMap = {
  1: "企业",
  2: "专家",
};

const siteList = ref([]);
const dataList = ref([]);
const total = ref(0);
const isShow = ref(false);
const {queryParams, demandForm} = toRefs(data);
const queryFormRef = ref();
const getList = async () => {
  const resp = await listCasDemand(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  // userStore.serviceDemandTotal = resp.total;
};
const approval = (id) => {
  //   demandForm.value = cloneDeep(row);
  //   if (!row.kinds) {
  //     demandForm.value.kinds = row.kind?.split(",") ?? [];
  //   }
  //   if (!row.wants) {
  //     demandForm.value.wants = row.want?.split(",") ?? [];
  //   }
  //   // if (!row.industrys) {
  //   //   demandForm.value.industrys = row.;
  //   // }
  //   isShow.value = true;
  router.push({
    path: "/auditing/serviceDemand/approval",
    query: {
      id,
    },
  });
};

// 获取站点列表
const getSiteList = async () => {
  const resp = await tenantSelect();
  siteList.value = resp.rows;
};

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

const selectedIds = ref([]);
// 选中状态改变
const handleSelectionChange = (val) => {
  selectedIds.value = val.map((el) => el.id);
};

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

const handleDelete = async (...ids) => {
  ElMessageBox.confirm(`是否确认删除编号为${ids.join(",")}的数据项？`)
      .then(async () => {
        await deleteCasDemand(ids);
        userStore.getServiceDemandTotal();
        userStore.getUnApprovedService();
        getList();
        ElMessage.success("删除成功");
      })
      .catch(() => {
      });
};
const handleBatchApproval = async () => {
  ElMessageBox.confirm(
      `是否确认批量审核通过编号为${selectedIds.value.join(",")}的数据项？`
  )
      .then(async () => {
        await batchCasDemand(selectedIds.value.join(","), 1);
        userStore.getServiceDemandTotal();
        userStore.getUnApprovedService();
        getList();
        ElMessage.success("批量审核成功");
      })
      .catch(() => {
      });
};
// 修改为完成状态
const complete = (id) => {
  ElMessageBox.confirm(`是否确认结束编号为${id}的数据项？`)
      .then(async () => {
        await updateCasDemand({id, status: 4});
        userStore.getServiceDemandTotal();
        userStore.getUnApprovedService();
        getList();
        ElMessage.success("删除成功");
      })
      .catch(() => {
      });
};
getSiteList();
getList();
userStore.getServiceDemandTotal();
userStore.getUnApprovedService();
</script>
<style></style>
