<template>
  <el-dialog
    title="关联设备"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
    :maxable="true"
  >
    <el-card shadow="never">
      <ele-pro-table
        :pageSizes="tablePageSizes"
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        :initLoad="false"
        @cell-click="cellClick"
      >
        <template v-slot:modelType="{ row }">
          <span>{{ row.category.modelType }}</span>
        </template>
        <template v-slot:specification="{ row }">
          <span>{{ row.category.specification }}</span>
        </template>
        <template v-slot:pathName="{ row }">
          <span>{{ row.position[0] && row.position[0].pathName }}</span>
        </template>
        <template v-slot:action="{ row }">
          <el-radio
            class="radio"
            v-model="radio"
            :disabled="row.isBindMainDevice"
            :label="row.id"
          >
            <i></i>
          </el-radio>
        </template>
      </ele-pro-table>
    </el-card>

    <div class="rx-sc">
      <el-button type="primary" size="small" @click="selected">选择</el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getAssetList } from '@/api/ruleManagement/plan';
  export default {
    components: {},
    data() {
      return {
        visible: false,
        radio: '',
        current: null,
        // 表格列配置
        columns: [
          {
            action: 'action',
            slot: 'action',
            align: 'center',
            label: '选择',
            reserveSelection: true,
            fixed: 'left'
          },
          {
            label: '设备名称',
            prop: 'name'
          },
          {
            label: '编码',
            prop: 'code'
          },
          {
            label: '固资编码',
            prop: 'fixCode'
          },
          {
            label: '使用人',
            prop: 'usePerson'
          },
          {
            label: '使用岗位',
            prop: 'postName'
          },
          {
            label: '片区负责人负责人',
            prop: 'areaPersonInChargeUserName'
          },
          {
            label: '有效开始时间',
            prop: 'startTime'
          },
          {
            label: '有效结束时间',
            prop: 'endTime'
          },
          {
            prop: 'category.modelType',
            label: '型号',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'category.specification',
            label: '规格',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            label: '设备位置',
            prop: 'pathName',
            formatter: (_row) => {
              const positionDetail =
                _row.position &&
                _row.position.length != 0 &&
                _row.position[0].detailPosition
                  ? _row.position[0].detailPosition
                  : '-';
              return _row.deviceLocationName
                ? _row.deviceLocationName + '-' + positionDetail
                : '';
            }
          }
        ]
      };
    },
    methods: {
      /* 表格数据源 */
      async datasource({}) {
        return await getAssetList({
          name: this.categoryName,
          pageNum: 1,
          size: 1000
        });
      },
      open(row) {
        this.categoryName = row.name;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs.table.reload();
        });
      },

      handleClose() {
        this.visible = false;
        // this.$refs.table.setSelectedRows([]);
        this.current = null;
        this.radio = '';
      },

      // 单击获取id
      cellClick(row) {
        if (row.isBindMainDevice) {
          return;
        }
        this.current = row;
        this.radio = row.id;
      },
      selected() {
        if (!this.current?.id) {
          this.$message.error('请选择一条数据');
          return;
        }

        this.$emit('chooseEquipment', this.current);
        this.handleClose();
      }
    }
  };
</script>

<style lang="scss" scoped>
  ::v-deep {
    .el-checkbox__input.is-disabled .el-checkbox__inner {
      background-color: #c1c1c1 !important;
    }
  }
  .ml60 {
    margin-left: 60px;
  }
  .rx-sc {
    flex: 0 0 50px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>
