6.16 7

七安。發表於2024-06-17
export default {
  data() {
    return {
      currentDisplayCount: 2,
      searchItems: [
        {field: 'name', label: '政策名稱', value: '', logicalOperator: 'AND', matchType: '精確'},
        {field: 'type', label: '政策分類', value: '', logicalOperator: 'AND', matchType: '精確'},
        {field: 'category', label: '分類', value: '', logicalOperator: 'AND', matchType: '精確'},
        {field: 'range', label: '施行範圍', value: '', logicalOperator: 'AND', matchType: '精確'},
        {field: 'document', label: '發文字號', value: '', logicalOperator: 'AND', matchType: '精確'},
        {field: 'form', label: '頒佈形式', value: '', logicalOperator: 'AND', matchType: '精確'},
        {field: 'organ', label: '制定機關', value: '', logicalOperator: 'AND', matchType: '精確'},
        {field: 'text', label: '全文', value: '', logicalOperator: 'AND', matchType: '精確'},
      ],
      searchForm: {
        name: '', // 政策名稱
        type: '', // 政策分類
        category: '', // 分類
        range: '', // 施行範圍
        form: '', // 頒佈形式
        document: '', // 發文字號
        organ: '', // 發文機構
        text: '', // 全文檢索
      },
      tableData: [], // 統一使用一個tableData,根據是否有搜尋條件決定顯示資料
      currentPage4: 1, // 初始化 currentPage4
      pageSize: 10, // 分頁大小
      total: 0, // 總記錄數
      SList:[]
    };
  },
  computed: {
    limitedSearchItems() {
      return this.searchItems.slice(0,this.currentDisplayCount); // 只取前兩項
    },
  },
  async created() {
    await this.fetchPolicies();
  },
  methods: {
    async search1() {
      let isEmpty = true;
      for (let item of this.searchItems) {
        if (item.value) {
          isEmpty = false;
          break;
        }
      }

      if (isEmpty) {
        alert("至少需要填寫第一個搜尋框");
        return;
      }

      let isAnyLineIncomplete = false;
      for (let item of this.searchItems) {
        if (!item.logicalOperator || !item.matchType) {
          isAnyLineIncomplete = true;
          break;
        }
      }

      if (isAnyLineIncomplete) {
        alert("請確保每一行的‘AND/OR’、輸入框、精確/模糊都得到填寫");
        return;
      }
      let queryParts = [];
      for (let item of this.searchItems) {

        if (item.value) {
          let conditionPart = `${item.field}=${item.value}matchType=${item.matchType}`;
          if (queryParts.length >= 0) {
            conditionPart = `${encodeURIComponent(item.logicalOperator)}${conditionPart}`;
          }
          queryParts.push(conditionPart);
        }
      }
      let dp = queryParts.join('&');
      console.log(dp);
      try {
        const response = await axios.get(`http://localhost:9090/policy/selectDP?dp=${queryParts}`);
        this.tableData = response.data;
      } catch (error) {
        console.error('Search failed:', error);
      }

    },

    addSearchItem() {
      // 如果顯示的檢索項數量未達到最大值,則增加顯示一項
      if (this.currentDisplayCount < 8) {
        this.currentDisplayCount++;
      } else {
        alert("已達最大檢索項數量!");
      }
    },

    removeSearchItem(index) {
      // 只允許從第三項開始刪除,並確保至少保留兩項
      if (index > 1 && this.currentDisplayCount > 2) {
        this.currentDisplayCount--;
      }
    },