<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
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        max-height="500"
        :initLoad="false"
        :needPage="false"
      >
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="handleAdd()"
          >
            关联表计台账
          </el-button>
        </template>
        <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-link type="primary" @click="batchUnbindByMeter(row.id)"
            >解绑</el-link
          >
        </template>
      </ele-pro-table>
    </el-card>

    <div class="rx-sc">
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
    <MaterialAdd
      ref="MaterialAddRef"
      @chooseEquipment="chooseEquipment"
    ></MaterialAdd>
  </el-dialog>
</template>

<script>
  import {
    bomSubstance,
    bindByMeter,
    batchUnbindByMeter
  } from '@/api/ruleManagement/plan';
  import MaterialAdd from '@/components/MaterialAdd.vue';

  export default {
    components: { MaterialAdd },
    data() {
      return {
        visible: false,
        radio: '',
        current: null,
        // 表格列配置
        columns: [
          {
            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
                : '';
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 120,
            align: 'center',
            slot: 'action'
          }
        ]
      };
    },
    methods: {
      batchUnbindByMeter(id) {
        batchUnbindByMeter({ bomCategorySubstanceIds: [id], substanceId: this.substanceId }).then(
          (res) => {
            this.$nextTick(() => {
              this.$refs.table.reload();
            });
          }
        );
      },
      /* 表格数据源 */
      async datasource({}) {
        let data = await bomSubstance({
          substanceId: this.substanceId,
          pageNum: 1,
          type: 1,
          size: 1000
        });

        return data.list;
      },
      handleAdd() {
        this.$refs.MaterialAddRef.open();
      },

      open(row) {
        this.substanceId = row.id;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs.table.reload();
        });
      },
      chooseEquipment(data) {
        console.log(data, 'data');
        bindByMeter({
          substanceId: this.substanceId,
          type: 1,
          bomCategorySubstanceId: data.id
        }).then((res) => {
          this.$refs.table.reload();
        });
      },

      handleClose() {
        this.visible = false;
      }
    }
  };
</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>
