<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item
        :label="t('admin.form.name', { type: t('admin.common.patent') })"
        prop="roleName"
      >
        <el-input
          v-model="queryParams.roleName"
          :placeholder="
            t('admin.form.placeholder', { type: t('admin.common.patent') })
          "
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="申请人" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入申请人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('admin.form.inventor')" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入发明人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="Search"
          size="small"
          @click="handleQuery"
        >
          {{ t("admin.common.search") }}
        </el-button>
        <el-button icon="Refresh" size="small" @click="resetQuery">
          {{ t("admin.common.reset") }}
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column :label="t('admin.common.dataNumber')" prop="id" />
      <el-table-column
        :label="t('admin.form.name', { type: t('admin.common.patent') })"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="t('webSearch.applicationNumber')"
        prop="applyCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column :label="t('admin.form.inventor')" prop="inventor" />
      <el-table-column :label="t('webSearch.applicant')" prop="applyName" />
      <el-table-column
        :label="t('admin.form.applicationDate')"
        align="center"
        prop="applyAt"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyAt) }}</span>
        </template>
      </el-table-column>
      <!-- <el-zh-column :label="t('common.status')" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.is_bind">已认领</el-tag>
          <el-tag type="danger" v-else>未认领</el-tag>
        </template>
      </el-zh-column> -->
      <el-table-column
        :label="t('admin.common.operation')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <!-- v-if="!scope.row.is_bind" -->
          <el-button
            size="small"
            type="text"
            icon="el-icon-plus"
            @click="handleBind(scope.row.id)"
            >{{ t("admin.table.claim") }}
          </el-button>
          <!-- <el-button
            v-else
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="handleUnbind(scope.row.id)"
            >{{ t("admin.common.cancel") }}认领</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 { delRole } from "@/api/system/role";
import { bindPatent, getNoBindPatentList } from "@/api/admin/expert/technology";
import { getInfo } from "@/api/admin/expert/account";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

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

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  queryParams: {
    // pageNum: 1,
    // pageSize: 10,
    // roleName: undefined,
    // author:
  },
});

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

/** 查询数据列表 */
const getList = async () => {
  loading.value = true;
  getInfo()
    .then((resp) => {
      return resp.data.casExpert;
    })
    .then((casExpert) => {
      queryParams.value.author = casExpert.name;
      queryParams.value.expertId = casExpert.id;
      getNoBindPatentList(queryParams.value).then((resp) => {
        // total.value = resp.;
        dataList.value = resp.data;
        loading.value = false;
      });
    })
    .catch((err) => {
      console.log(err);
      loading.value = false;
    });
  // listRole(queryParams.value).then(
  //   (response) => {
  //     dataList.value = response.rows;
  //     total.value = response.total;
  //     loading.value = false;
  //   }
  // );
};

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm(
      t("admin.common.confirmAction", {
        type: t("admin.common.item"),
        number: row.id,
        action: t("admin.common.delete"),
      })
    )
    .then(function () {
      return delRole(roleIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess(t("admin.common.deleteSuccess"));
    })
    .catch(() => {});
}

function handleBind(id) {
  getInfo()
    .then((resp) => {
      return resp.data.casExpert;
    })
    .then((casExpert) => {
      bindPatent({
        patentId: id,
        expertId: casExpert.id,
      }).then(() => {
        getList();
        ElMessage.success(t("tips.claimPatentSuccess"));
      });
    });
  // expertPatentBind({ id: this.queryParams.id, patent_ids: [id] }).then(
  //   (res) => {
  //     this.$modal.msgSuccess(t('admin.common.editSuccess'));
  //     this.getList();
  //   }
  // );
}

function handleUnbind(id) {
  // expertPatentUnbind({ id: this.queryParams.id, patent_ids: [id] }).then(
  //   (res) => {
  //     this.$modal.msgSuccess(t('admin.common.editSuccess'));
  //     this.getList();
  //   }
  // );
}

getList();
</script>
