<template>
  <div class="app-container">
    <!-- 顶部区域 -->
    <el-form ref="queryFormRef" :model="queryParams" inline>
      <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-->
<!--              icon="search"-->
<!--              size="default"-->
<!--              type="primary"-->
<!--              @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"
          :disabled="!selectedIds.length"
          size="small"
          type="primary"
          @click="handleBatchApproval"
      >批量审核
      </el-button>
      <el-button
          :disabled="!selectedIds.length"
          size="small"
          type="danger"
          @click="handleDelete(...selectedIds)"
      >批量删除
      </el-button>
      <el-button
          size="small"
          type="primary"
          @click="userStore.resetUnApproval('technology', 'unApprovedTechnology')"
      >未读归零
      </el-button>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80"></el-table-column>
      <el-table-column align="center" label="编号" prop="id"></el-table-column>
      <el-table-column
          align="center"
          label="需求名称"
          prop="title"
      ></el-table-column>
      <el-table-column
          align="center"
          label="所属领域"
          prop="industryStr"
      ></el-table-column>
      <el-table-column
          align="center"
          label="联系人"
          prop="name"
      ></el-table-column>
      <el-table-column
          align="center"
          label="经纪人"
          prop="brokerName"
      ></el-table-column>
      <el-table-column
          align="center"
          label="手机"
          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
          align="center"
          label="发布时间"
          prop="createTime"
      ></el-table-column>
      <el-table-column
          align="center"
          label="截止日期"
          prop="deadline"
      ></el-table-column>
      <el-table-column align="center" label="操作" width="320" v-if="queryParams.status!==4">
        <template #default="{ row }">
          <el-button round size="small" type="text" @click="approval(row)"
          >审批
          </el-button>
          <el-button
              round
              size="small"
              type="text"
              @click="handleAppointBroker(row)"
          >指派经纪人
          </el-button>
          <el-button round size="small" type="text" @click="complete(row.id)"
          >完成
          </el-button>
          <el-button
              icon="delete"
              round
              size="small"
              type="text"
              @click="handleDelete(row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 审批详情 -->
    <Edialog
        v-model="demandForm"
        v-model:isShow="isShow"
        @update-list="handleQuery"
    ></Edialog>
    <appoint-broker
        v-model="brokerForm.brokerId"
        v-model:show="showAppointBroker"
        :default-label="appointBrokerName"
        @confirm="handleConfirmAppointBroker"
    />
  </div>
</template>
<script setup>
import AppointBroker from "@/views/components/appoint-broker.vue";
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 {reactive, ref, toRefs} from "vue";
import {cloneDeep} from "lodash";
import useUserStore from "@/store/modules/user";
import {
  batchTechnologyDemand,
  dataReviewAppointBroker,
  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: {},
  brokerForm: {},
});

const siteList = ref([]);
const dataList = ref([]);
const total = ref(0);
const isShow = ref(false);
const {queryParams, demandForm, brokerForm} = 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(",") ?? [];
  }
  isShow.value = true;
};

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

const handleQuery = () => {
  // userStore.getUnApprovedTechnology();
  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.getUnApprovedTechnology();
        getList();
        ElMessage.success("删除成功");
      })
      .catch(() => {
      });
};
const handleBatchApproval = async () => {
  ElMessageBox.confirm(
      `是否确认批量审核通过编号为${selectedIds.value.join(",")}的数据项？`
  )
      .then(async () => {
        await batchTechnologyDemand(selectedIds.value.join(","), 1);
        userStore.getUnApprovedTechnology();
        getList();
        ElMessage.success("批量审核成功");
      })
      .catch(() => {
      });
};

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

const showAppointBroker = ref(false);
const appointDemandId = ref();
const appointBrokerName = ref("");

const handleAppointBroker = (row) => {
  appointDemandId.value = row.id;
  appointBrokerName.value = row.brokerName;
  showAppointBroker.value = true;
};

/**
 * 指派经纪人
 * @param value
 * @return {Promise<void>}
 */
const handleConfirmAppointBroker = async (value) => {
  await dataReviewAppointBroker(
      appointDemandId.value,
      value.brokerId,
      value.brokerName
  );
  showAppointBroker.value = false;
  getList();
  ElMessage.success("指派经纪人成功");
};

getSiteList();
getList();
userStore.getUnApprovedTechnology();
userStore.getTechnologyTotal();
</script>
<style></style>
