<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="./releaseTechnology">
          <el-button type="primary" size="small">发布需求</el-button>
        </router-link>
      </el-col>
    </el-row>

    <el-radio-group
      v-model="queryParams.status"
      size="small"
      @change="handleQuery"
    >
      <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="3">草稿箱</el-radio-button>
      <el-radio-button :label="4">已结束</el-radio-button>
    </el-radio-group>

    <el-table v-loading="loading" :data="dataList" style="margin-top: 20px">
      <el-table-column label="数据编号" prop="id" align="center" />
      <el-table-column label="需求名称" prop="title" align="center" />
      <el-table-column label="所属领域" prop="industryStr" align="center" />
      <el-table-column label="联系人" prop="name" align="center" />
      <el-table-column label="手机号" prop="mobile" align="center" />
      <!-- <el-table-column label="浏览量" prop="visit_count" align="center" /> -->
      <el-table-column
        label="发布时间"
        prop="createTime"
        align="center"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="截止时间"
        prop="deadline"
        align="center"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.deadline) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        class-name="small-padding fixed-width"
        align="center"
      >
        <template #default="{ row }">
          <router-link
            :to="{ path: './releaseTechnology', query: { id: row.id } }"
            v-if="queryParams.status == 3"
          >
            <el-button size="small" type="text" icon="Edit">编辑</el-button>
          </router-link>

          <el-button
            v-if="queryParams.status != 0"
            size="small"
            type="text"
            icon="Delete"
            @click="handleDelete(row.id)"
            >删除</el-button
          >
          <el-button
            v-if="queryParams.status == 1"
            size="small"
            type="text"
            icon="View"
            @click="handleResults(row)"
            >查看匹配结果</el-button
          >
          <el-button
            v-if="queryParams.status == 0"
            size="small"
            type="text"
            icon="Close"
            @click="cancelPub(row.id)"
            >取消发布</el-button
          >
          <el-button
            size="small"
            type="text"
            icon="View"
            @click="complete(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 name="technologyDemand">
import {
  technologyDemandList,
  deleteTechnologyDemand,
  updateTechnologyDemand,
} from "@/api/admin/enterprise/demand";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";

const { proxy } = getCurrentInstance();
const router = useRouter();

const dataList = ref([]); // 企业技术需求列表
const loading = ref(true);
// const showSearch = ref(true);
const total = ref(0);
// const dateRange = ref([]);

const data = reactive({
  // form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 1,
  },
  // rules: {
  //   name: [{ required: true, message: "公司名称不能为空", trigger: "blur" }],
  //   dutyParagraph: [
  //     { required: true, message: "公司税号不能为空", trigger: "blur" },
  //   ],
  //   bank: [{ required: true, message: "开户行不能为空", trigger: "blur" }],
  //   bankAccount: [
  //     { required: true, message: "开户行账号不能为空", trigger: "blur" },
  //   ],
  //   bankPhone: [
  //     { required: true, message: "开户行电话不能为空", trigger: "blur" },
  //   ],
  //   email: [{ required: true, message: "电子邮箱不能为空", trigger: "blur" }],
  //   phone: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
  //   address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
  //   username: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
  //   userPhone: [
  //     { required: true, message: "联系人电话不能为空", trigger: "blur" },
  //   ],
  //   userAddress: [
  //     { required: true, message: "邮寄地址不能为空", trigger: "blur" },
  //   ],
  // },
});

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

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const resp = await technologyDemandList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
// function resetQuery() {
//   dateRange.value = [];
//   proxy.resetForm("queryRef");
//   handleQuery();
// }
/** 删除按钮操作 */
function handleDelete(id) {
  ElMessageBox.confirm('是否确认删除数据编号为"' + id + '"的需求项？')
    .then(function () {
      console.log("delete");
      return deleteTechnologyDemand(id);
    })
    .then(() => {
      getList();
      ElMessage.success("删除成功");
    })
    .catch((err) => {
      console.log(err);
    });
}
function handleShelf(row) {
  let text = row.shelf_status == 2 ? "上架" : "下架";
  ElMessageBox.confirm('确认要"' + text + '""' + row.id + '"的需求吗?')
    .then(function () {
      let status = row.shelf_status == 1 ? 2 : 1;
      return achievementShelf({ id: row.id, status });
    })
    .then(() => {
      getList();
      ElMessage.success(text + "成功");
    })
    .catch(() => {});
}
function handleResults(row) {
  router.push({
    path: "./results",
    query: {
      keyword: row.title,
    },
  });
}

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