<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="./releaseService">
          <el-button type="primary" size="small"
            >{{ t("admin.table.publishDemand") }}
          </el-button>
        </router-link>
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-radio-group
      v-model="queryParams.status"
      size="small"
      @change="handleQuery"
    >
      <el-radio-button :label="0"
        ><!--待受理-->
        {{ t("dict.pendingReview") }}
      </el-radio-button>
      <el-radio-button :label="1"
        ><!--已受理-->
        {{ t("dict.accepted") }}
      </el-radio-button>
      <el-radio-button :label="2">{{ t("dict.rejected") }} </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-zh-column :label="t('admin.form.name', {type: t('admin.common.demand')})" align="center" prop="title" /> -->
      <el-table-column
        :label="t('admin.form.category', { type: t('admin.common.demand') })"
        align="center"
        prop="kind"
      />
      <!-- <el-zh-column :label="t('common.status')" align="center" prop="status" /> -->
      <el-table-column
        :label="t('admin.form.contact')"
        align="center"
        prop="name"
      />
      <el-table-column
        :label="t('admin.form.mobile')"
        align="center"
        prop="mobile"
      />
      <el-table-column
        :label="t('admin.table.publishTime')"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('admin.common.operation')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            v-if="queryParams.type == 2"
            size="small"
            type="text"
            icon="Delete"
            @click="handleDelete(row.id)"
          >
            {{ t("admin.common.delete") }}
          </el-button>
          <el-button
            v-if="queryParams.type == 0"
            size="small"
            type="text"
            icon="Close"
            @click="handleDelete(row.id)"
            >{{ t("admin.table.cancelPublish") }}
          </el-button>
          <el-button
            v-if="queryParams.type != 2"
            size="small"
            type="text"
            icon="View"
            @click="handleEdit(row.id)"
            ><!--查看-->
            {{ t("admin.common.view") }}
          </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>
import { demandList } from "@/api/admin/enterprise";
import { updateDemand } from "@/api/admin/enterprise/demand";
import { ElMessage } from "element-plus";
import modal from "@/plugins/modal";
import { getCurrentInstance, onActivated, reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
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,
  },
});

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

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const resp = await demandList(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) {
  modal
    .confirm(
      t("admin.common.confirmAction", {
        action: t("admin.common.delete"),
        type: t("admin.form.order"),
        number: id,
      })
    )
    .then(function () {
      return demandDelete(postIds);
    })
    .then(() => {
      getList();
      ElMessage.success(t("admin.common.deleteSuccess"));
    })
    .catch(() => {});
}

function handleEdit(id) {
  router.push({ path: "./releaseService", query: { id } });
}

// 修改为完成状态
const complete = (id) => {
  modal.confirm(
    t("admin.common.confirmAction", {
      action: t("common.finish"),
      type: t("admin.common.item"),
      number: id,
    })
      .then(async () => {
        await updateDemand({ id, status: 4 });
        getList();
        ElMessage.success(t("admin.common.deleteSuccess"));
      })
      .catch(() => {})
  );
};

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