<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="./release">
          <el-button size="small" type="primary"
            >{{ 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
        v-for="option in serviceDemandStatusDict"
        :key="option.value"
        :label="option.value"
        >{{ option.i18n ? t(`dict.${option.i18n}`) : option.label }}
      </el-radio-button>
    </el-radio-group>

    <el-table v-loading="loading" :data="dataList" style="margin-top: 20px">
      <el-table-column
        align="center"
        :label="t('admin.form.name', { type: t('admin.common.demand') })"
        prop="title"
      />
      <el-table-column
        align="center"
        :label="t('admin.form.category', { type: t('admin.common.demand') })"
        prop="kind"
      />
      <el-table-column align="center" :label="t('common.status')">
        <template #default="{ row }">
          <dict-tag :options="serviceDemandStatusDict" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="t('admin.form.contact')"
        prop="name"
      />
      <el-table-column
        align="center"
        :label="t('admin.form.mobile')"
        prop="mobile"
      />
      <el-table-column
        align="center"
        :label="t('admin.table.publishTime')"
        prop="createTime"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        class-name="small-padding fixed-width"
        :label="t('admin.common.operation')"
      >
        <template #default="{ row }">
          <el-button
            v-if="queryParams.type == 3"
            icon="Delete"
            size="small"
            type="text"
            @click="handleDelete(row.id)"
          >
            {{ t("admin.common.delete") }}
          </el-button>
          <el-button
            v-if="queryParams.type == 1"
            icon="Close"
            size="small"
            type="text"
            @click="handleDelete(row.id)"
            >{{ t("admin.table.cancelPublish") }}
          </el-button>
          <el-button
            v-if="queryParams.type != 3"
            icon="View"
            size="small"
            type="text"
            @click="handleEdit(row.id)"
            >{{ t("admin.common.view") }}
          </el-button>
          <el-button size="small" type="text" @click="complete(row.id)"
            >{{ t("common.complete") }}
          </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"
    />
  </div>
</template>

<script setup>
import { demandList } from "@/api/admin/expert/demand";
import { getCurrentInstance, onActivated, reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import DictTag from "@/components/DictTag/index.vue";
import { serviceDemandStatusDict } from "@/constant/dict";
import { useI18n } from "vue-i18n";
import { parseTime } from "@/utils/ruoyi";
import { ElMessage } from "element-plus";

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({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: "1",
  },
});

const { queryParams } = 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) {
  proxy.$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();
      proxy.$modal.msgSuccess(t("admin.common.deleteSuccess"));
    })
    .catch(() => {});
}

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

// 修改为完成状态
const complete = (id) => {
  modal
    .confirm(
      /*`是否确认结束编号为${id}的数据项？`*/
      t("admin.common.confirmAction", {
        action: t("common.finish"),
        type: t("admin.common.item"),
        number: id,
      })
    )
    .then(async () => {
      await updateCasDemand({ id, status: 4 });
      getList();
      ElMessage.success(t("admin.common.deleteSuccess"));
    })
    .catch(() => {});
};

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