<template>
  <ele-modal
    :width="modelWidth"
    :visible.sync="dispatchVisible"
    v-if="dispatchVisible"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    :maxable="true"
    title="选择工位"
    append-to-body
    :before-close="cancel"
  >
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      @update:selection="handleSelectionChange"
      :cache-key="cacheKeyUrl"
      :pageSize="20"
      :selection.sync="selection"
      :row-click-checked="true"
      :row-click-checked-intelligent="false"
      row-key="id"
    >
      <template v-slot:meterTime="{ row }">
        <div v-if="row.extInfo.meterTime">
          {{ `${row.extInfo.meterTime}  ${row.extInfo.meterTimeUnit}/次 ` }}
        </div>
      </template>

      <template v-slot:enabled="{ row }">
        {{ dict.enabled[row.enabled] }}
      </template>
    </ele-pro-table>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dispatchVisible = false">取 消</el-button>
      <el-button type="primary" @click="comfirm">确定</el-button>
    </span>
  </ele-modal>
</template>

<script>
  import { getFactoryworkstation } from '@/api/factoryModel';
  export default {
    data() {
      return {
        dispatchVisible: false,
        workCenterId: '',
        cacheKeyUrl: 'chooseStation-2025-12-04',
        selection: [],
        dict: {
          groupId: [],
          factory: [],
          enabled: {
            1: '生效',
            0: '未生效'
          }
        },
        allSelection: [],
        index: '',
        sourceTaskId: ''
      };
    },

    computed: {
      modelWidth() {
        return '75vw';
      },

      columns() {
        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            reserveSelection: true,
            selectable: (row) => this.isRowSelectable(row)
          },
          {
            columnKey: 'index',
            slot: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '工位编码',
            showOverflowTooltip: true
          },
          {
            label: '工位名称',
            prop: 'name',
            showOverflowTooltip: true
          },
          {
            label: '责任人',
            prop: 'leaderName',
            slot: 'factory',
            showOverflowTooltip: true
          },
          {
            label: '所属区域',
            prop: 'areaName',
            showOverflowTooltip: true
          },

          {
            label: '所属工厂',
            prop: 'factoryName',
            showOverflowTooltip: true
          },

          {
            label: '所属厂房',
            prop: 'workshopPlanName',
            showOverflowTooltip: true
          },
          {
            label: '所属车间',
            prop: 'workshopName',
            showOverflowTooltip: true
          },
          {
            label: '所属产线',
            prop: 'productionLineName',
            showOverflowTooltip: true
          },
          {
            label: '设备名称',
            prop: 'extInfo.assetName',
            formatter: (_row, _column, cellValue) => {
              return typeof cellValue === 'string' ? cellValue : '';
            },
            showOverflowTooltip: true
          },
          {
            label: '设备编码',
            prop: 'extInfo.assetCode',
            formatter: (_row, _column, cellValue) => {
              return typeof cellValue === 'string' ? cellValue : '';
            },
            showOverflowTooltip: true
          },
          {
            label: '节拍时间',
            prop: 'extInfo.meterTime',
            slot: 'meterTime',
            showOverflowTooltip: true
          },
          {
            //修改此prop名称时，请同步修改columnKey属性和下方selectType方法
            label: '状态',
            prop: 'enabled',
            slot: 'enabled',
            filters: [
              { value: 1, text: '生效' },
              { value: 0, text: '未生效' }
            ],
            filterMultiple: false,
            columnKey: 'enabled',
            showOverflowTooltip: true
          }
        ];
      }
    },

    methods: {
      open(id, list = [], index) {
        this.dispatchVisible = true;
        this.index = index;
        this.workCenterId = id;

        this.allSelection = Array.isArray(list) ? list : [];

        this.$nextTick(() => {
          const table = this.$refs.table;
          if (!table) return;

          this.allSelection.forEach((item) => {
            if (item.isNew == 1) {
              table.toggleRowSelection(item, true);
            }
          });
        });
      },

      isRowSelectable(row) {
        const existsOld = this.allSelection.some(
          (item) => item.id === row.id && item.isNew !== 1
        );
        return !existsOld;
      },

      cancel() {
        this.dispatchVisible = false;
      },

      reload(where) {
        this.$refs.table.reload({ pageNum: 1, where: where });
      },

      datasource({ page, limit }) {
        return getFactoryworkstation({
          pageNum: page,
          size: limit,
          workCenterId: this.workCenterId
        });
      },

      handleSelectionChange(val) {
        // this.allSelection = val;
      },

      comfirm() {
        if (this.selection.length == 0) {
          return this.$message.warning('请选择一条数据');
        }

        this.$emit('chooseStationList', this.selection, this.index);
        this.dispatchVisible = false;
      }
    }
  };
</script>

<style></style>
