<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="专利名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入专利名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="apply_name">
        <el-input
          v-model="queryParams.apply_name"
          placeholder="请输入申请人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <!-- <el-form-item label="发明人" prop="inventor">
        <el-input
          v-model="queryParams.inventor"
          placeholder="请输入发明人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="getList"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getList"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="数据编号" align="center" prop="id" />
      <el-table-column label="专利名称" align="center" prop="title" />
      <el-table-column label="申请号" align="center" prop="apply_code" />
      <el-table-column
        label="申请（专利权）人"
        align="center"
        prop="apply_name"
      />
      <el-table-column label="发明人" align="center" prop="inventor" />
      <el-table-column label="申请日" align="center" prop="apply_at" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.is_bind">已认领</el-tag>
          <el-tag type="danger" v-else>未认领</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.is_bind"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleBind(scope.row.id)"
            >认领机构</el-button
          >
          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUnbind(scope.row.id)"
            >取消认领</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import {
  expertPatent,
  expertPatentBind,
  expertPatentUnbind,
} from "@/api/dataList/patent";
export default {
  data() {
    return {
      showSearch: true,
      loading: true,
      dataList: null,
      queryParams: {
        id: undefined,
        title: undefined,
        apply_name: undefined,
        // inventor: undefined,
      },
    };
  },
  methods: {
    getList() {
      this.loading = true;
      expertPatent(this.queryParams).then((response) => {
        this.dataList = response.data;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.getList();
    },
    handleBind(id) {
      expertPatentBind({ id: this.queryParams.id, patent_ids: [id] }).then(
        (res) => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        }
      );
    },
    handleUnbind(id) {
      expertPatentUnbind({ id: this.queryParams.id, patent_ids: [id] }).then(
        (res) => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        }
      );
    },
  },
  created() {
    let { id } = this.$route.query;
    if (!id) {
      this.$message.error("无ID");
      this.$router.go(-1);
      return;
    }
    console.log(id);
    this.queryParams.id = id;
    this.getList();
  },
};
</script>