<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model:page="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #receiveStatus="{ row }">
        {{
          getProgressStatusText(row.receiveStatus)
        }}
      </template>
      <template #deptId="{ row }">
        {{ row.deptName }}
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getTransferReceiceListApi,
} from '@/api/filesTransfer/accept';
import { mapGetters } from 'vuex';
import website from '@/config/website';
import { getDeptSelectApi } from '@/api/select';

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 32,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        editBtn: false,
        delBtn: false,
        selection: false,
        addBtn:true,
        dialogClickModal: false,
        column: [

          {
            label: '项目名称',
            prop: 'proName',
            search: true,
          },
          {
            label: '单项工程名称',
            prop: 'singleProName',
            search: true,
          },
          {
            label: '移交时间',
            prop: 'transferTime',
            search: true,
            type: 'date',
            format: 'YYYY-MM-DD',
            valueFormat: 'YYYY-MM-DD',
            dataType: 'string'
          },
          {
            label: '接收单位',
            prop: 'deptId',
            search: true,
            type: 'tree',
            dicData: [], // 初始空，后面动态赋值
            props: {
              label: 'label',
              value: 'id',
              children: 'children'
            },
            // 显示时用插槽展示 deptName（因为 row.deptName 是名称）
            slot: true
          },

          {
            label: '接收清单',
            prop: 'transferIssue',
          },
          {
            label: '接收进度',
            prop: 'receiveStatus',
            slot: true,
          },

        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.post_add, false),
        viewBtn: this.validData(this.permission.post_view, false),
        delBtn: this.validData(this.permission.post_delete, false),
        editBtn: this.validData(this.permission.post_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
  },
  methods: {
    /* 查看接收进度 */
    getProgressStatusText(status) {
      switch (status) {
        case '0':
          return '进行中'
        case '1':
          return '已完成'
        default:
          return '未知状态'
      }
    },
    loadDeptOptions() {
      getDeptSelectApi().then(res => {
        this.treeDataList = this.convertToVueTree(res.data.data);
        // 找到 deptId 列并更新 dicData
        const deptColumn = this.option.column.find(col => col.prop === 'deptId');
        if (deptColumn) {
          deptColumn.dicData = this.treeDataList; // Vue 3 可直接赋值；Vue 2 建议用 this.$set
        }
      }).catch(err => {
        console.error('加载部门列表失败', err);
      });
    },
    // 树数据过滤 - 支持无限层级转换
    convertToVueTree(data, level = 1) {
      if (!data || !Array.isArray(data)) {
        return []
      }

      return data.map(item => {
        const node = {
          id: item.deptId,
          label: item.deptName,
        }

        if (item.children && Array.isArray(item.children) && item.children.length > 0) {
          if (level < 3) {
            const children = this.convertToVueTree(item.children, level + 1)
            if (children.length > 0) node.children = children
          }
        }
        return node
      })
    },
    beforeOpen(done, type, row) {
      done(); // 必须调用 done()
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      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;
      this.loadDeptOptions();
      let data = {
        ...params,
        pageNum:page.currentPage,
        pageSize:page.pageSize
      };
      getTransferReceiceListApi(data).then(res => {
        const data = res.data;
        this.page.total = data.total;
        this.data = data.rows;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>

<style></style>
