<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryFormRef"
      v-show="showSearch"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="专利名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入专利名称"
          clearable
          size="default"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="search"
          size="default"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" size="default" @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="default"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="!ids.length"
          type="danger"
          icon="delete"
          @click="handleDelete()"
          >批量删除</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="数据编号" prop="id" align="center" />
      <el-table-column label="专利名称" prop="title" align="center" />
      <el-table-column label="申请号" prop="applyCode" align="center" />
      <el-table-column label="发明人" prop="inventor" align="center" />
      <el-table-column
        label="申请（专利权）人"
        prop="applyName"
        align="center"
      />
      <!-- <el-table-column label="状态" align="center">
        <template #default={row}>
          <el-switch
            v-model="row.shelf_status"
            :active-value="1"
            :inactive-value="2"
            @change="handleStatusChange(row)"
          ></el-switch>
        </template>
      </el-table-column> -->
      <el-table-column label="申请日期" prop="applyAt" align="center">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            size="small"
            type="text"
            icon="edit"
            @click="handleUpdate(row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="text"
            icon="delete"
            @click="handleDelete(row.id)"
            >删除</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="showEditDialog" width="700px">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="申请人:" prop="applyName">
          <el-input
            v-model="form.applyName"
            placeholder="请输入申请（专利权）人（多个请以 , 分割）"
          />
        </el-form-item>
        <el-form-item label="申请人地址:" prop="applyAddress">
          <el-input
            v-model="form.applyAddress"
            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="applyCode">
          <el-input v-model="form.applyCode" placeholder="请输入申请号" />
        </el-form-item>
        <el-form-item label="公开号:" prop="openCode">
          <el-input v-model="form.openCode" placeholder="请输入公开号" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请日期:" prop="applyAt">
              <el-date-picker
                value-format="YYYYMMDD"
                v-model.trim="form.applyAt"
                type="date"
                placeholder="请选择申请日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公开日:" prop="openAt">
              <el-date-picker
                style="width: 100%"
                value-format="YYYYMMDD"
                v-model.trim="form.openAt"
                type="date"
                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="请选择">
                <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="ipcCode">
              <el-input v-model="form.ipcCode" placeholder="请输入IPC分类号" />
            </el-form-item>
            <!-- <el-form-item label="状态:" prop="shelf_status">
              <el-radio-group v-model="form.shelf_status">
                <el-radio :label="1">上架</el-radio>
                <el-radio :label="2">下架</el-radio>
              </el-radio-group>
            </el-form-item> -->
          </el-col>
        </el-row>

        <el-form-item label="摘要:" prop="description">
          <!-- <Editor v-model="form.description" :minHeight="150" /> -->
          <el-input
            v-model.number="form.description"
            placeholder="请输入摘要"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8 }"
          />
        </el-form-item>
        <el-form-item label="主权项:" prop="principalClaim">
          <!-- <Editor v-model="form.principalClaim" :minHeight="150" /> -->
          <el-input
            v-model.number="form.principalClaim"
            placeholder="请输入主权项"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8 }"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 认领专利 -->
    <!-- <el-dialog title="认领专利" :visible.sync="openDialog" width="900px">
      <el-form :model="claimQueryParams" ref="formClaim" :inline="true">
        <el-form-item label="申请（专利权）人" prop="applyName">
          <el-input
            v-model.trim="claimQueryParams.applyName"
            placeholder="请输入申请（专利权）人"
            clearable
            size="default"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="发明人" prop="inventor">
          <el-input
            v-model.trim="claimQueryParams.inventor"
            placeholder="请输入发明人"
            clearable
            size="default"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="search"
            size="default"
            @click="handleQueryClaim"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="claimLoading"
        max-height="500"
        ref="claimTable"
        :data="claimList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="数据编号" prop="id" align="center" />
        <el-table-column label="申请人" prop="applyName" align="center" />
        <el-table-column label="发明人" prop="inventor" align="center" />
        <el-table-column label="申请日期" prop="applyAt" align="center" />
        <el-table-column label="专利名称" prop="title" align="center" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitClaimForm">确 定</el-button>
        <el-button @click="openDialog = false">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script setup name="Patent">
import {
  patentList,
  patentAdd,
  patentDetail,
  patentEdit,
  patentDelete,
  patentShelf,
} from "@/api/dataList/patent";
import { patentOptions } from "@/utils/parameter";
import { reactive, toRefs } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
const loading = ref(true); // 是否正在加载
const showSearch = ref(true); // 是否显示搜索栏
const showEditDialog = ref(false);
const queryFormRef = ref(null);
const formRef = ref(null);

const total = ref(0);
const title = ref("");
const dataList = ref([]);
const ids = ref([]); // 批量选中的列表项id列表
const data = reactive({
  queryParams: {
    title: undefined,
    pageNum: 1,
    pageSize: 10,
  },
  form: {},
  rules: {
    applyName: [{ required: true, message: "申请人不能为空", trigger: "blur" }],
    applyAddress: [
      { required: true, message: "申请人地址不能为空", trigger: "blur" },
    ],
    openCode: [{ required: true, message: "公开号不能为空", trigger: "blur" }],
    openAt: [{ required: true, message: "公开日不能为空", trigger: "blur" }],
    inventor: [{ required: true, message: "发明人不能为空", trigger: "blur" }],
    title: [{ required: true, message: "专利名称不能为空", trigger: "blur" }],
    applyCode: [{ required: true, message: "申请号不能为空", trigger: "blur" }],
    applyAt: [{ required: true, message: "申请日期不能为空", trigger: "blur" }],
    kind: [{ required: true, message: "请选择专利类型", trigger: "change" }],
    ipcCode: [
      { required: true, message: "IPC分类号不能为空", trigger: "blur" },
    ],
    description: [{ required: true, message: "摘要不能为空", trigger: "blur" }],
    principalClaim: [
      { required: true, message: "主权项不能为空", trigger: "blur" },
    ],
  },
});
const { queryParams, form, rules } = toRefs(data);

const resetQuery = () => {
  if (queryFormRef.value) {
    queryFormRef.value.resetFields();
  }
  handleQuery();
};
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
};
const getList = async () => {
  loading.value = true;
  const resp = await patentList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};
// 取消按钮
const cancel = () => {
  showEditDialog.value = false;
  reset();
};
// 表单重置
const reset = () => {
  form.value = {
    id: undefined,
    applyName: undefined,
    applyAddress: undefined,
    inventor: undefined,
    title: undefined,
    applyCode: undefined,
    openCode: undefined,
    openAt: undefined,
    applyAt: undefined,
    kind: undefined,
    ipcCode: undefined,
    description: undefined,
    // shelf_status: 1,
    principalClaim: undefined,
  };
  if (formRef.value) {
    formRef.value.resetFields();
  }
};
const handleAdd = () => {
  reset();
  showEditDialog.value = true;
  title.value = "新增专利";
};
const handleUpdate = async (row) => {
  reset();
  const resp = await patentDetail(row.id);
  form.value = resp.data;
  showEditDialog.value = true;
  title.value = "修改专利";
};
const submitForm = async () => {
  await formRef.value.validate();
  form.value.inventor = form.value.inventor.replace(/，/g, ",");
  form.value.applyName = form.value.applyName.replace(/，/g, ",");
  if (form.value.id != undefined) {
    await patentEdit(form.value);
    ElMessage.success("修改成功");
    showEditDialog.value = false;
    getList();
  } else {
    await patentAdd(form.value);
    ElMessage.success("新增成功");
    showEditDialog.value = false;
    getList();
  }
};
/** 删除按钮操作 */
const handleDelete = (id) => {
  const delIds = id || ids.value.join(",");
  ElMessageBox.confirm(`是否确认删除编号为${delIds}的数据项？`)
    .then(async () => {
      await patentDelete(delIds);
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};
getList();
</script>
