<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      @submit.prevent
    >
      <el-form-item label="专利名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入专利名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </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="handleQuery"
          />
        </el-form-item>
        <el-form-item label="发明人" prop="inventor">
          <el-input
            v-model="queryParams.inventor"
            placeholder="请输入发明人"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- 无接口  暂时注释 -->
      <!-- <el-col :span="1.5">
          <router-link to="./claimPatent">
            <el-button type="primary" plain icon="Plus" size="mini"
              >认领</el-button
            >
          </router-link>
        </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="数据编号" prop="id" />
      <el-table-column
        label="专利名称"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="申请号"
        prop="apply_code"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="发明人" prop="inventor" />
      <el-table-column label="申请（专利权）人" prop="apply_name" />
      <el-table-column label="申请日期" align="center" prop="apply_at">
        <template #default="scope">
          <span>{{ dayjs(scope.row.apply_at).format("YYYY-MM-DD") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row.id)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</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"
    />

    <!-- 添加或修改数据配置对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="patentRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="申请人:" prop="apply_name">
          <el-input
            v-model="form.apply_name"
            placeholder="请输入申请（专利权）人（多个请以 , 分割）"
          />
        </el-form-item>
        <el-form-item label="申请人地址:" prop="apply_address">
          <el-input
            v-model="form.apply_address"
            placeholder="请输入申请人地址"
          />
        </el-form-item>
        <el-form-item label="发明人:" prop="inventor">
          <el-input
            v-model="form.inventor"
            placeholder="请输入发明人（多个请以 , 分割）"
          />
        </el-form-item>
        <el-form-item label="专利名称:" prop="title">
          <el-input v-model="form.title" placeholder="请输入专利名称" />
        </el-form-item>
        <el-form-item label="申请号:" prop="apply_code">
          <el-input v-model="form.apply_code" placeholder="请输入申请号" />
        </el-form-item>
        <el-form-item label="公开号:" prop="open_code">
          <el-input v-model="form.open_code" placeholder="请输入公开号" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请日期:" prop="apply_at">
              <el-date-picker
                style="width: 100%"
                v-model="form.apply_at"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择申请日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公开日:" prop="open_at">
              <el-date-picker
                style="width: 100%"
                v-model="form.open_at"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择公开日"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="专利类型:" prop="kind">
              <el-select
                v-model="form.kind"
                clearable
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in patentOptions"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IPC分类号:" prop="ipc_code">
              <el-input v-model="form.ipc_code" placeholder="请输入IPC分类号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="摘要:" prop="description">
          <el-input
            v-model="form.description"
            placeholder="请输入摘要"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8 }"
          />
        </el-form-item>
        <el-form-item label="主权项:" prop="principal_claim">
          <el-input
            v-model="form.principal_claim"
            placeholder="请输入主权项"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8 }"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
//   import {
//     expertPatent,
//     patentAdd,
//     patentDetail,
//     patentEdit,
//     patentDelete,
//   } from "@/api/admin/expert/technology";
import dayjs from "dayjs";
import { useRouter } from "vue-router";
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({
  form: {},
  queryParams: {
    pageNum: 1,
    page_size: 10,
    inventor: undefined,
  },
  rules: {
    apply_name: [
      { required: true, message: "申请人不能为空", trigger: "blur" },
    ],
    apply_address: [
      { required: true, message: "申请人地址不能为空", trigger: "blur" },
    ],
    open_code: [{ required: true, message: "公开号不能为空", trigger: "blur" }],
    open_at: [{ required: true, message: "公开日不能为空", trigger: "blur" }],
    inventor: [{ required: true, message: "发明人不能为空", trigger: "blur" }],
    title: [{ required: true, message: "专利名称不能为空", trigger: "blur" }],
    apply_code: [
      { required: true, message: "申请号不能为空", trigger: "blur" },
    ],
    apply_at: [
      { required: true, message: "申请日期不能为空", trigger: "blur" },
    ],
    kind: [{ required: true, message: "请选择专利类型", trigger: "change" }],
    ipc_code: [
      { required: true, message: "IPC分类号不能为空", trigger: "blur" },
    ],
    description: [{ required: true, message: "摘要不能为空", trigger: "blur" }],
    principal_claim: [
      { required: true, message: "主权项不能为空", trigger: "blur" },
    ],
  },
});

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

/** 查询数据列表 */
const getList = async () => {
  loading.value = true;
  expertPatent(queryParams.value).then((response) => {
    dataList.value = response.data.data;
    total.value = response.data.count;
    loading.value = false;
  });
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除数据编号为"' + row.id + '"的专利项?')
    .then(function () {
      return patentDelete({ id: row.id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    id: undefined,
    apply_name: undefined,
    apply_address: undefined,
    inventor: undefined,
    title: undefined,
    apply_code: undefined,
    open_code: undefined,
    open_at: undefined,
    apply_at: undefined,
    kind: undefined,
    ipc_code: undefined,
    description: undefined,
    principal_claim: undefined,
  };
  proxy.resetForm("patentRef");
}
/** 添加数据 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加数据";
};
/** 修改数据 */
const handleUpdate = (id) => {
  reset();
  patentDetail({ id }).then((res) => {
    form.value = res.data;
    open.value = true;
    title.value = "修改数据";
  });
};
/** 提交按钮 */
function submitForm() {
  proxy.$refs["patentRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        patentEdit(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        patentAdd(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

// getList();
</script>
