<template>
  <div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
    >
      <template #menu-left> </template>
      <template #menu-right> </template>
      <template #menu="scope">
        <el-button type="text" @click="handleEdit(scope.row)">明细</el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      form: {},
      selectionList: [],
      query: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        columnSort: true,
        tip: false,
        height: 'auto',
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 12,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        editBtnText: '修改',
        labelWidth: 120,
        menuWidth: 80,
        dialogWidth: 900,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchLabelWidth: 'auto',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'center',
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',
        column: [
          {
            label: '配套日期',
            prop: 'subpkdate',
            search: true,
            sortable: true,
            span: 12,
            type: 'date',
            format: 'YYYY-MM-DD', // 显示格式
            valueFormat: 'YYYY-MM-DD', // 提交给后端的格式
            searchRange: true,
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间',
            searchRules: [
              {
                required: true,
                message: '请选择配套日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划部门',
            prop: 'partName',
            search: true,
            sortable: true,
            span: 12,
          },
          {
            label: '配套数量',
            prop: 'num',
            search: false,
            sortable: false,
            span: 12,
          },
        ],
      },

      data: [
        {
          subpkdate: '2026-01-02',
          partName: '部门1',
          num: '33',
        },
      ],
    };
  },
  methods: {
    handleEdit(row) {
      this.$emit('edit-row', {...this.query,...row});
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.subpkdate && Array.isArray(params.subpkdate)) {
        // 根据后端接口要求，拆分开始时间和结束时间
        // 如果后端接收 startDate 和 endDate
        params.startDate = params.subpkdate[0];
        params.endDate = params.subpkdate[1];

        // 删除原始的 subpkdate 数组，避免传递多余参数或格式错误
        delete params.subpkdate;
      }

      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    onLoad(page, params = {}) {
      // this.loading = true;
      console.log(page, params, this.query);
      // getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
      //   this.data = res.data.data.records;
      //   this.loading = false;
      //   this.page.total = res.data.data.total;
      //   this.selectionClear();
      // });
    },
  },
  mounted() {},
};
</script>
d