<template>
  <div class="app-container">
    <!-- 顶部区域 -->
    <el-form inline ref="queryFormRef" :model="queryParams">
      <el-row>
        <el-col>
          <el-form-item prop="status">
            <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-button :label="2">已驳回</el-radio-button> -->
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属站点" prop="tenantId">
            <SiteOptions v-model="queryParams.tenantId" :site-list="siteList" />
          </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>
      <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-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"
        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" prop=""></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" round size="small" @click="approval(row)"
            >审批</el-button
          >
          <el-button type="text" round size="small" @click="complete(row.id)"
            >完成</el-button
          >
          <el-button
            type="text"
            round
            size="small"
            icon="delete"
            @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"
    />

    <!-- 审批详情 -->
    <Edialog
      v-model:isShow="isShow"
      v-model="demandForm"
      @update-list="handleQuery"
    ></Edialog>
  </div>
</template>
<script setup>
import { businessList } from "@/api/Businessneeds/index.js";
import Edialog from "@/views/components/Businessneeds/Approvaldetails.vue";
import SiteOptions from "@/views/components/SiteOptions";
import { tenantSelect } from "@/api/subPlatform/tenant";
import { ref, toRefs, defineComponent } from "vue";
import { cloneDeep } from "lodash";
import useUserStore from "@/store/modules/user";
import {
  batchTechnologyDemand,
  deleteTechnologyDemand,
  updateTechnologyDemand,
} from "@/api/dataApproval/enterpriseTechnologyDemand";
import { ElMessage, ElMessageBox } from "element-plus";
const userStore = useUserStore();
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 0,
  },
  demandForm: {},
});

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 businessList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
};
const approval = (row) => {
  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;
};

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

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

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

const selectedIds = ref([]);
// 选中状态改变
const handleSelectionChange = (val) => {
  selectedIds.value = val.map((el) => el.id);
};
const handleDelete = async (...ids) => {
  ElMessageBox.confirm(`是否确认删除编号为${ids.join(",")}的数据项？`)
    .then(async () => {
      await deleteTechnologyDemand(ids);
      userStore.getApprovalCount();
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};
const handleBatchApproval = async () => {
  ElMessageBox.confirm(
    `是否确认批量审核通过编号为${selectedIds.value.join(",")}的数据项？`
  )
    .then(async () => {
      await batchTechnologyDemand(selectedIds.value);
      userStore.getApprovalCount();
      getList();
      ElMessage.success("批量审核成功");
    })
    .catch(() => {});
};

// 修改为完成状态
const complete = (id) => {
  ElMessageBox.confirm(`是否确认结束编号为${id}的数据项？`)
    .then(async () => {
      await updateTechnologyDemand({ id, status: 4 });
      userStore.getApprovalCount();
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};
getSiteList();
getList();
</script>
<style></style>
