<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="./release">
          <el-button type="primary" size="small"
            >{{ t("routes.company.publishProduct") }}
          </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-group>

    <el-table v-loading="loading" :data="dataList" style="margin-top: 20px">
      <el-table-column :label="t('admin.common.dataNumber')" prop="id" />
      <el-table-column
        :label="t('admin.form.name', { type: t('admin.form.product') })"
        prop="title"
      />
      <el-table-column
        :label="t('admin.form.productField')"
        prop="industryStr"
      />
      <!-- <el-zh-column label="浏览量" prop="visit_count" /> -->
      <el-table-column
        :label="t('admin.table.publishTime')"
        prop="createTime"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('admin.common.operation')"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <router-link
            :to="{ path: './release', 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 == 1"-->
          <!--            size="small"-->
          <!--            type="text"-->
          <!--            :icon="row.shelfStatus == 2 ? 'Upload' : 'Download'"-->
          <!--            @click="handleShelf(row)"-->
          <!--            >{{-->
          <!--              row.shelfStatus == 2-->
          <!--                ? t("admin.table.putOn")-->
          <!--                : t("admin.table.putOff")-->
          <!--            }}-->
          <!--          </el-button>-->
          <el-button
            v-if="queryParams.status != 1"
            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 == 1"-->
          <!--            disabled-->
          <!--            size="small"-->
          <!--            type="text"-->
          <!--            icon="View"-->
          <!--            @click="checkEnterpriseInfo(row.id)"-->
          <!--            >{{ t("tips.browseEnterpriseInformation") }}-->
          <!--          </el-button>-->
<!--    TODO:      <el-button
            v-if="queryParams.status == 0"
            size="small"
            type="text"
            icon="Close"
            @click="releaseCancel(row.id)"
            >{{ t("tips.moveToDraftBox") }}
          </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="Product">
import {
  deleteEnterpriseProduct,
  getEnterpriseProduct,
  updateEnterpriseProduct,
} from "@/api/admin/enterprise/product";
import { ElMessage } from "element-plus";
import modal from "@/plugins/modal";
import { onActivated, reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const router = useRouter();
const dataList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 1, // 状态(0-待审核1-通过2-驳回3-草稿)
  },
});

const { queryParams } = toRefs(data);

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const resp = await getEnterpriseProduct(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};

// 处理删除
const handleDelete = (id) => {
  modal
    .confirm(
      t("admin.common.confirmAction", {
        action: t("admin.common.delete"),
        number: id,
        type: t("admin.form.product"),
      })
    )
    .then(async () => {
      await deleteEnterpriseProduct(id);
      ElMessage.success(t("admin.common.deleteSuccess"));
      getList();
    })
    .catch((error) => {
      console.log("Cancelled");
    });
};

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 取消发布
const releaseCancel = (id) => {
  modal
    // .confirm('确认要取消发布id为"' + id + '"的产品吗?')
    .confirm(
      t("admin.common.confirmAction", {
        action: t("admin.table.cancelPublish"),
        number: id,
        type: t("admin.form.product"),
      })
    )
    .then(async () => {
      await updateEnterpriseProduct({ id, status: 3 });
      ElMessage.success(t("tips.cancelReleaseSuccess"));
      getList();
    })
    .catch((err) => {
      console.log(err);
    });
};
// 下架和上架
const handleShelf = (row) => {
  let text =
    row.shelfStatus == 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.form.product"),
      })
    )
    .then(function () {
      let status = row.shelfStatus == 1 ? 2 : 1;
      return updateEnterpriseProduct({ id: row.id, shelfStatus: status });
    })
    .then(() => {
      getList();
      ElMessage.success(t("admin.common.success", { action: text }));
    })
    .catch(() => {});
};
const handleResults = (row) => {
  router.push({ path: "./results", query: { keyword: row.title } });
};

const checkEnterpriseInfo = (id) => {
  router.push({ path: "./business" });
};

onActivated(() => {
  getList();
});
</script>
