<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()"
            :disabled="count>=current.dosage"
          >
            关联设备实例
          </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="batchUnbind(row)">解绑</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 { queryBindSubstanceList } from '@/api/ruleManagement/plan';
  import MaterialAdd from './MaterialAdd.vue';
  import { bomSubstanceBind, batchUnbind } from '@/api/ledgerAssets';

  export default {
    components: { MaterialAdd },
    data() {
      return {
        visible: false,
        bomCategoryId: '',
        substanceId: '',
        current: {},
        count: 0,
        // 表格列配置
        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: {
      chooseEquipment(row) {
        bomSubstanceBind({
          bomCategoryId: this.bomCategoryId,
          bomCategorySubstanceId: row.id,
          substanceId: this.substanceId
        }).then((res) => {
          this.$refs.table.reload();
          this.$message.success('操作成功！');
        });
      },
      batchUnbind(row) {
        batchUnbind({
          bomCategorySubstanceIds: [row.id],
          substanceId: this.substanceId
        }).then((res) => {
         this.$refs.table.reload();
          this.$message.success('解绑成功！');
        });
      },

      /* 表格数据源 */
      async datasource({}) {
        let data = await queryBindSubstanceList({
          id: this.substanceId,
          bomCategoryId: this.bomCategoryId,
          pageNum: 1,
          type: 0,
          size: 1000
        });
        this.count = data.list.length;

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

      open(row) {
        this.substanceId = row.substanceId;
        this.bomCategoryId = row.id;
        this.current = row;
        this.visible = true;
        this.$nextTick(() => {
          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>
