<template>
  <div class="search">
    <el-form :inline="true" :model="searchParams" size="small">
      <el-form-item label="考核年度">
        <el-date-picker
          type="year"
          placeholder="选择考核年度"
          v-model="searchParams.year"
          format="yyyy"
          value-format="yyyy"
          @change="getCodeByYear"
          style="height: 32px; width: 160px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="表编号：">
        <el-select v-model="searchParams.code" clearable filterable placeholder="请输入表编号">
          <el-option
            v-for="(item, index) of codeList"
            :key="index"
            :label="item.code"
            :value="item.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="表格名称：">
        <el-input
          placeholder="请输入表格名称"
          v-model="searchParams.name"
          clearable
        ></el-input>
      </el-form-item>
      <el-button type="primary" size="small" class="search-btn" @click="search"
        >查询</el-button
      >
      <el-button
        type="default"
        size="small"
        class="search-btn"
        @click="
          clear();
          search();
        "
        >清空</el-button
      >
    </el-form>
  </div>
</template>

<script>
import api from "@/api/basicTable/category";
export default {
  data() {
    return {
      searchParams: {
        year: "",
        code: "",
        name: "",
      },
      codeList: [],
    };
  },
  methods: {
    clear() {
      this.searchParams = {
        year: "",
        code: "",
        name: "",
      };
    },
    search() {
      this.$emit("search", this.searchParams);
    },
    getCodeByYear() {
      api
        .getCodeByYear(this.searchParams.year)
        .then((res) => {
          this.codeList = res.data.data;
        })
        .catch(() => {
          this.codeList = [];
        });
    },
  },
  watch: {
    "searchParams.year"(val) {
      if (!val) {
        this.$set(this.searchParams, "code", "");
        this.codeList = [];
      }
    },
  },
};
</script>

<style scoped>
</style>