<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      @submit.prevent
    >
      <el-form-item
        :label="t('admin.form.name', { type: t('admin.common.patent') })"
        prop="title"
      >
        <el-input
          v-model="queryParams.title"
          :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="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="t('admin.form.inventor')" 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="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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="small"
          @click="handleAdd"
        >
          {{ t("admin.common.add") }}
        </el-button>
      </el-col>
      <!-- 无接口  暂时注释 -->
      <!--      <el-col :span="1.5">-->
      <!--        <router-link to="./claimPatent">-->
      <!--          <el-button type="primary" plain icon="Plus" size="small"-->
      <!--            >{{ t("admin.table.claim") }}-->
      <!--          </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="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="{ row }">
          <span>{{ dayjs(row.applyAt).format("YYYY-MM-DD") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('admin.common.operation')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            size="small"
            type="text"
            icon="Edit"
            @click="handleUpdate(row)"
            >{{ t("admin.common.edit") }}
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="Delete"
            @click="handleDelete(row.id)"
          >
            {{ t("admin.common.delete") }}
          </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="t('webSearch.applicant')" prop="applyName">
          <el-input
            v-model="form.applyName"
            :placeholder="t('tips.pleaseEnterApplicant')"
          />
        </el-form-item>
        <el-form-item
          :label="t('webSearch.applicantAddress')"
          prop="applyAddress"
        >
          <el-input
            v-model="form.applyAddress"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.applicantAddress'),
              })
            "
          />
        </el-form-item>
        <el-form-item :label="t('admin.form.inventor')" prop="inventor">
          <el-input
            v-model="form.inventor"
            :placeholder="
              t('admin.form.placeholder', { type: t('admin.form.inventor') })
            "
          />
        </el-form-item>
        <el-form-item
          :label="t('admin.form.name', { type: t('admin.common.patent') })"
          prop="title"
        >
          <el-input
            v-model="form.title"
            :placeholder="
              t('admin.form.placeholder', { type: t('admin.common.patent') })
            "
          />
        </el-form-item>
        <el-form-item
          :label="t('webSearch.applicationNumber')"
          prop="applyCode"
        >
          <el-input
            v-model="form.applyCode"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.applicationNumber'),
              })
            "
          />
        </el-form-item>
        <el-form-item :label="t('webSearch.publicationNumber')" prop="openCode">
          <el-input
            v-model="form.openCode"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.publicationNumber'),
              })
            "
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item
              :label="t('admin.form.applicationDate')"
              prop="applyAt"
            >
              <el-date-picker
                style="width: 100%"
                v-model="form.applyAt"
                type="date"
                value-format="YYYY-MM-DD"
                :placeholder="
                  t('admin.form.placeholder', {
                    type: t('admin.form.applicationDate'),
                  })
                "
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="t('webSearch.publicationDate')" prop="openAt">
              <el-date-picker
                style="width: 100%"
                v-model="form.openAt"
                type="date"
                value-format="YYYY-MM-DD"
                :placeholder="
                  t('admin.form.placeholder', {
                    type: t('webSearch.publicationDate'),
                  })
                "
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="t('webSearch.patentType')" prop="kind">
              <el-select
                v-model="form.kind"
                clearable
                :placeholder="t('admin.form.pleaseSelect')"
                style="width: 100%"
              >
                <el-option
                  v-for="item in patentOptions"
                  :key="item.key"
                  :label="item[locale]"
                  :value="item.key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="t('webSearch.ipc')" prop="ipcCode">
              <el-input
                v-model="form.ipcCode"
                :placeholder="
                  t('admin.form.placeholder', {
                    type: t('webSearch.ipc'),
                  })
                "
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item :label="t('webSearch.patentStatus')" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="1"><!--授权-->{{ t("dict.authorized") }}</el-radio>
            <el-radio label="2"
              ><!--实审-->{{ t("dict.substantiveExamination") }}
            </el-radio>
            <el-radio label="3"><!--公开-->{{ t("dict.public") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="t('admin.form.summary')" prop="description">
          <el-input
            v-model="form.description"
            :placeholder="
              t('admin.form.placeholder', { type: t('admin.form.summary') })
            "
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8 }"
          />
        </el-form-item>
        <el-form-item
          :label="t('webSearch.sovereignItem')"
          prop="principalClaim"
        >
          <el-input
            v-model="form.principalClaim"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.sovereignItem'),
              })
            "
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 8 }"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm"
            >{{ t("admin.common.confirm") }}
          </el-button>
          <el-button @click="cancel">{{ t("admin.common.cancel") }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  deleteCasPatentByIds,
  expertPatentList,
  insertCasPatent,
  updateCasPatent,
} from "@/api/admin/expert/patent";
import { cloneDeep } from "lodash";
import { getInfo } from "@/api/admin/expert/account";
import dayjs from "dayjs";
import { ElMessage } from "element-plus";
import modal from "@/plugins/modal";
import { patentOptions } from "@/utils/parameter";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import { computed, getCurrentInstance, reactive, ref, toRefs } from "vue";

const { t, locale } = 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({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inventor: undefined,
  },
  rules: {
    applyName: [
      {
        required: true,
        /*"申请人不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.applicant"),
          })
        ),
        trigger: "blur",
      },
    ],
    applyAddress: [
      {
        required: true,
        /*"申请人地址不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.applicantAddress"),
          })
        ),
        trigger: "blur",
      },
    ],
    openCode: [
      {
        required: true,
        /*"公开号不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.publicationNumber"),
          })
        ),
        trigger: "blur",
      },
    ],
    openAt: [
      {
        required: true,
        /*"公开日不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.publicationDate"),
          })
        ),
        trigger: "blur",
      },
    ],
    inventor: [
      {
        required: true,
        /*"发明人不能为空"*/
        message: computed(() =>
          t("admin.validation.required", { type: t("admin.form.inventor") })
        ),
        trigger: "blur",
      },
    ],
    title: [
      {
        required: true,
        /* "专利名称不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("admin.form.name", {
              type: t("admin.common.patent"),
            }),
          })
        ),
        trigger: "blur",
      },
    ],
    applyCode: [
      {
        required: true,
        /*"申请号不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.applicationNumber"),
          })
        ),
        trigger: "blur",
      },
    ],
    applyAt: [
      {
        required: true,
        /*"申请日期不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.applicationDate"),
          })
        ),
        trigger: "blur",
      },
    ],
    kind: [
      {
        required: true,
        /*"请选择专利类型"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.patentType"),
          })
        ),
        trigger: "change",
      },
    ],
    ipcCode: [
      {
        required: true,
        /*"IPC分类号不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.ipc"),
          })
        ),
        trigger: "blur",
      },
    ],
    status: [
      {
        required: true,
        // message: "请选择专利状态",
        message: computed(() =>
          t("admin.validation.pleaseSelect", {
            type: t("webSearch.patentStatus"),
          })
        ),
        trigger: "change",
      },
    ],
    description: [
      {
        required: true,
        /*"摘要不能为空"*/
        message: computed(() =>
          t("admin.validation.required", { type: t("admin.form.summary") })
        ),
        trigger: "blur",
      },
    ],
    principalClaim: [
      {
        required: true,
        /*"主权项不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("webSearch.sovereignItem"),
          })
        ),
        trigger: "blur",
      },
    ],
  },
});

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

const queryRef = ref();
const patentRef = ref();
const expertId = ref(undefined);

/** 查询数据列表 */
const getList = async () => {
  loading.value = true;
  const { data } = await getInfo();
  form.value.expertId = data.casExpert.id;
  const id = data.casExpert.id;
  expertId.value = data.casExpert.id;
  expertPatentList(id, queryParams.value).then((resp) => {
    dataList.value = resp.rows;
    total.value = resp.total;
    loading.value = false;
  });
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
function resetQuery() {
  queryRef.value.resetFields();
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(id) {
  modal
    .confirm(
      t("admin.common.confirmAction", {
        type: t("admin.common.patent"),
        action: t("admin.common.delete"),
        number: id,
      })
    )
    .then(function () {
      return deleteCasPatentByIds(id);
    })
    .then(() => {
      getList();
      ElMessage.success(t("admin.common.deleteSuccess"));
    })
    .catch((err) => {
      console.log(err);
    });
}

/** 重置新增的表单以及其他数据  */
function 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,
    principalClaim: undefined,
  };
  proxy.resetForm("patentRef");
}

/** 添加数据 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = t("admin.form.addData");
};
/** 修改数据 */
const handleUpdate = (row) => {
  reset();
  const data = cloneDeep(row);
  // patentDetail({ id }).then((res) => {
  form.value = data;
  open.value = true;
  title.value = t("admin.form.editData");
  // });
};

/** 提交按钮 */
const submitForm = async () => {
  await patentRef.value.validate();
  if (form.value.id != undefined) {
    await updateCasPatent({ ...form.value, expertId: expertId.value });
    ElMessage.success(t("admin.common.editSuccess"));
    open.value = false;
    getList();
  } else {
    await insertCasPatent({ ...form.value, expertId: expertId.value });
    ElMessage.success(t("admin.common.addSuccess"));
    open.value = false;
    getList();
  }
};

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

getList();
</script>
