<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="论文题目" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入论文题目"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-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="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="数据编号" prop="id" align="center" />
      <el-table-column label="论文题目" prop="title" align="center" />
      <el-table-column label="刊物" prop="apply_code" align="center" />
      <el-table-column label="作者" prop="apply_code" align="center" />
      <el-table-column label="关键词" prop="apply_code" align="center" />
      <el-table-column label="出版日期" prop="apply_name" align="center" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page_num"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px">
      <el-form ref="form" :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-row>
          <el-col :span="24">
            <el-form-item label="出版日期:" prop="apply_at">
              <el-date-picker
                value-format="yyyyMMdd"
                v-model.trim="form.apply_at"
                type="date"
                placeholder="请选择出版日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <InputBoxAdd
          :labelWidth="100"
          v-model="form"
          title="关键词"
          placeholder="请输入关键词"
          fieldKey="key_words"
          ref="keyWordsForm"
        />

        <el-form-item label="摘要:" prop="description">
          <el-input
            v-model.number="form.description"
            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>
  </div>
</template>
<script>
import {
  patentList,
  patentAdd,
  patentDetail,
  patentEdit,
  patentDelete,
  patentShelf,
} from "@/api/dataList/patent";
import InputBoxAdd from "@/views/components/InputBoxAdd";
export default {
  components: {
    InputBoxAdd,
  },
  data() {
    return {
      loading: true,
      showSearch: true,
      queryParams: {
        title: undefined,
        page_num: 1,
        page_size: 10,
      },
      total: 0,
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      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" },
        ],
      },
      openDialog: false,
      claimLoading: false,
      claimQueryParams: {
        apply_name: "",
        inventor: "",
        page_num: 1,
        page_size: 100,
      },
      claimList: [],
      ids: [],
      formClaim: {},
    };
  },
  methods: {
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.page_num = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      patentList(this.queryParams).then((res) => {
        // this.dataList = res.data.data;
        // this.total = res.data.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        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,
        // shelf_status: 1,
        principal_claim: undefined,
      };
      this.resetForm("form");
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增论文";
    },
    handleUpdate(row) {
      this.reset();
      patentDetail({ id: row.id }).then((res) => {
        this.form = res.data;
        this.open = true;
        this.title = "修改论文";
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.inventor = this.form.inventor.replace(/，/g, ",");
          this.form.apply_name = this.form.apply_name.replace(/，/g, ",");
          if (this.form.id != undefined) {
            patentEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            patentAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除编号为"' + row.id + '"的数据项？')
        .then(function () {
          return patentDelete({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
  created() {
    this.getList();
  },
};
</script>