<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"
            >{{ t("admin.table.publishDemand") }}
          </el-button>
        </router-link>
      </el-col>
    </el-row>

    <el-radio-group
      v-model="queryParams.status"
      size="small"
      @change="handleQuery"
    >
      <el-radio-button :label="0"
        >{{ t("admin.table.pendingReview") }}
      </el-radio-button>
      <el-radio-button :label="1"
        >{{ t("admin.table.published") }}
      </el-radio-button>
      <el-radio-button :label="2"
        >{{ t("admin.table.rejected") }}
      </el-radio-button>
      <el-radio-button :label="3"
        >{{ t("admin.table.draftBox") }}
      </el-radio-button>
      <el-radio-button :label="4">{{ t("dict.finished") }}</el-radio-button>
    </el-radio-group>

    <el-table v-loading="loading" :data="dataList" style="margin-top: 20px">
      <el-table-column
        :label="t('admin.common.dataNumber')"
        prop="id"
        align="center"
      />
      <el-table-column
        :label="t('admin.form.name', { type: t('admin.common.demand') })"
        prop="title"
        align="center"
      />
      <el-table-column
        :label="t('admin.form.industry')"
        prop="industryStr"
        align="center"
      />
      <el-table-column
        :label="t('admin.form.contact')"
        prop="name"
        align="center"
      />
      <el-table-column
        :label="t('admin.form.mobile')"
        prop="mobile"
        align="center"
      />
      <!-- <el-zh-column label="浏览量" prop="visit_count" align="center" /> -->
      <el-table-column
        :label="t('admin.table.publishTime')"
        prop="createTime"
        align="center"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('admin.table.deadline')"
        prop="deadline"
        align="center"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.deadline) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('admin.common.operation')"
        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"
              >{{ t("admin.common.edit") }}
            </el-button>
          </router-link>

          <el-button
            v-if="queryParams.status != 0"
            size="small"
            type="text"
            icon="Delete"
            @click="handleDelete(row.id)"
          >
            {{ t("admin.common.delete") }}
          </el-button>
          <el-button
            v-if="queryParams.status == 1"
            size="small"
            type="text"
            icon="View"
            @click="handleResults(row)"
            >{{ t("webSearch.viewMatchingResults") }}
          </el-button>
          <el-button
            v-if="queryParams.status == 0"
            size="small"
            type="text"
            icon="Close"
            @click="cancelPub(row.id)"
            >{{ t("admin.table.cancelPublish") }}
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="View"
            @click="complete(row.id)"
            >{{ t("common.complete") }}
          </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 {
  deleteTechnologyDemand,
  technologyDemandList,
  updateTechnologyDemand,
} from "@/api/admin/enterprise/demand";
import { ElMessage } from "element-plus";
import modal from "@/plugins/modal";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

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

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

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 1,
  },
});

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 handleDelete(id) {
  modal
    .confirm(
      t("admin.common.confirmAction", {
        action: t("admin.common.delete"),
        number: id,
        type: t("admin.common.demand"),
      })
    )
    .then(() => {
      console.log("delete");
      return deleteTechnologyDemand(id);
    })
    .then(() => {
      getList();
      ElMessage.success(t("admin.common.deleteSuccess"));
    })
    .catch((err) => {
      console.log(err);
    });
}

function handleShelf(row) {
  let text =
    row.shelf_status == 2 ? t("admin.table.putOn") : t("admin.table.putOff");
  modal
    // .confirm('确认要"' + text + '""' + row.id + '"的需求吗?')
    .confirm(
      t("admin.common.confirmAction", {
        action: text,
        number: row.id,
        type: t("admin.common.demand"),
      })
    )
    .then(function () {
      let status = row.shelf_status == 1 ? 2 : 1;
      return achievementShelf({ id: row.id, status });
    })
    .then(() => {
      getList();
      ElMessage.success(t("admin.common.success", { action: text }));
    })
    .catch(() => {});
}

function handleResults(row) {
  router.push({
    path: "./results",
    query: {
      keyword: row.title,
    },
  });
}

// 修改为完成状态
const complete = (id) => {
  modal.confirm(
    t("admin.common.confirmAction", {
      action: t("common.finish"),
      type: t("admin.common.item"),
      number: id,
    })
      .then(async () => {
        await updateTechnologyDemand({ id, status: 4 });
        getList();
        ElMessage.success(
          t("admin.common.success", {
            action: t("common.finish"),
          })
        );
      })
      .catch(() => {})
  );
};
const cancelPub = (id) => {
  modal
    // .confirm(`是否确认取消发布编号为${id}的数据项？`)
    .confirm(
      t("admin.common.confirmAction", {
        action: t("admin.table.cancelPublish"),
        number: id,
        type: t("admin.common.item"),
      })
    )
    .then(async () => {
      await updateTechnologyDemand({ id, status: 3 });
      getList();
      ElMessage.success(
        t("admin.common.success", { action: t("admin.table.cancelPublish") })
      );
    })
    .catch(() => {});
};
getList();
</script>
