<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      :before-open="beforeOpenFn"
    >
      <template #menu-left>
        <!-- <el-button type="danger" @click="delFn">删除</el-button> -->
      </template>
      <template #menu-right> </template>
      <template #menu="scope">
        <el-button v-if="scope.row.stationStatus == '0'" type="text" @click="handleEdit(scope.row)"
          >占用</el-button
        >
        <el-button v-if="scope.row.stationStatus == '1'" type="text" @click="handleEdit(scope.row)"
          >闲置</el-button
        >
      </template>

      <template #stationStatus="scope">
        <el-tag v-if="scope.row.stationStatus == '0'" type="success" @click="handleEdit(row)">闲置</el-tag>
        <el-tag v-if="scope.row.stationStatus == '1'" type="danger" @click="handleEdit(row)">占用</el-tag>
      </template>
      <template #stationPosition="scope"> {{ scope.row.stationPosition }}楼 </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getStationList,stationUpdate } from '@/api/logisticsManagement/siteBasic';
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: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        editBtnText: '修改',
        labelWidth: 120,
        menuWidth: 60,
        dialogWidth: 900,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        menu: true,
        gridBtn: false,
        searchMenuPosition: 'right',
        align: 'center',
        column: [
          {
            label: '站点区域',
            prop: 'stationRegion',
            search: false,
            sortable: true,
            span: 12,
            display: false,
          },
          {
            label: '作业中心',
            prop: 'wcName',
            search: false,
            sortable: true,
            span: 12,
            display: false,
          },
          {
            label: '作业中心',
            prop: 'wcId',
            search: true,
            sortable: true,
            span: 12,
            filterable: true,
            hide: true,
            searchOrder: 17,
            type: 'select',
            dicUrl: '/api/blade-desk/BA/WorkCenter/listForSelect',
            props: {
              label: 'wcName',
              value: 'id',
            },
          },
          {
            label: '站点名称',
            prop: 'stationName',
            search: true,
            sortable: true,
            span: 12,
            searchOrder: 19,
          },
          {
            label: '站点码',
            prop: 'stationCode',
            search: true,
            sortable: true,
            span: 12,
            searchOrder: 20,
          },
          {
            label: '站点楼层',
            prop: 'stationPosition',
            search: true,
            sortable: true,
            span: 12,
            searchOrder: 18,
          },
          {
            label: '站点状态',
            prop: 'stationStatus',
            search: true,
            sortable: true,
            span: 12,
            type: 'select',
            display: false,
            searchOrder: 16,
            dicData: [
              {
                label: '占用',
                value: 1,
              },
              {
                label: '闲置',
                value: 0,
              },
            ],
          },
          {
            label: '更新时间',
            prop: 'updateTime',
            search: true,
            sortable: true,
            span: 12,
            display: false,
          },
        ],
      },

      data: [],
    };
  },
  methods: {
    // 修改站点状态（闲置/占用切换）
    handleEdit(row) {
      // 确定新状态
      const newStatus = row.stationStatus == '0' ? '1' : '0';
      const statusText = newStatus == '0' ? '闲置' : '占用';
      
      this.$confirm(`确定将站点 "${row.stationName}" 设置为${statusText}状态？`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        row.stationStatus = Number(newStatus);
        // 调用更新接口
         stationUpdate(row).then(() => {
          this.$message.success('状态修改成功');
          this.onLoad(this.page, this.query);
         }).catch(err => {
           this.$message.error('状态修改失败');
           console.error(err);
         });
      }).catch(() => {
        // 用户取消
      });
    },
    beforeOpenFn(done, type, loading) {
      if (type == 'edit') {
        this.form.wcId = this.form.wcId + '';
      }
      done();
    },
    delFn() {
      if (this.selectionList.length <= 0) {
        this.$message.error('请选择要删除的行');
        return;
      }
      // 2. 检查是否有占用状态的数据（stationStatus == '1'）
      const hasOccupiedData = this.selectionList.some(item => item.stationStatus == '1');
      if (hasOccupiedData) {
        this.$message.warning('只能删除闲置状态的站点，请先释放占用的站点');
        return;
      }
      // 3. 确认删除
      this.$confirm('确定将选择数据删除？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 4. 调用删除接口
          const ids = this.selectionList.map(item => item.id).join(',');
          // remove(ids).then(() => {
          //   this.$message.success('删除成功');
          //   this.onLoad(this.page, this.query);
          //   this.selectionClear();
          // });
        })
        .catch(() => {
          // 用户取消删除
        });
    },

    rowSave(row, done, loading) {
      // addPersonAbility(row).then(
      //   () => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: 'success',
      //       message: '操作成功!',
      //     });
      //     done();
      //   },
      //   error => {
      //     window.console.log(error);
      //     loading();
      //   }
      // );
    },
    rowUpdate(row, index, done, loading) {
      // updatePersonAbility(row).then(
      //   () => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: 'success',
      //       message: '操作成功!',
      //     });
      //     done();
      //   },
      //   error => {
      //     window.console.log(error);
      //     loading();
      //   }
      // );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // return removePersonAbility(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },

    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);
    },
    handleChange(file, fileList) {
      // proxy.$Export.xlsx(file.raw).then((data) => {
      //     data.value = data.results;
      // });
      this.$message({
        type: 'success',
        message: '操作成功!',
      });
    },

    onLoad(page, params = {}) {
      this.loading = true;
      getStationList(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