<template>
  <div>
    <equipment-search
      @search="reload"
      :networkCounts="networkCounts"
      ref="search"
    />
    <!-- 数据表格 -->
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      height="calc(100vh - 265px - 102px)"
      full-height="calc(100vh - 116px - 102px)"
      tool-class="ele-toolbar-form"
      cache-key="systemOrgUserTable"
      @select="selectChange"
      @select-all="changeSelectAll"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar="{ row }">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          class="ele-btn-icon"
          @click="add"
        >
          新建
        </el-button>
        <el-button
          size="small"
          icon="el-icon-download"
          class="ele-btn-icon"
          @click="exportFile"
        >
          导出
        </el-button>
        <el-button
          size="small"
          :disabled="checkRadioData.length == 0"
          icon="el-icon-set-up"
          class="ele-btn-icon"
        >
          打印条码
        </el-button>
        <el-button
          size="small"
          :disabled="checkRadioData.length == 0"
          icon="el-icon-thumb"
          class="ele-btn-icon"
          @click="moveTo(checkRadioData, 'person')"
        >
          设置片区负责人
        </el-button>
        <el-button size="small" class="ele-btn-icon" @click="handlDelete"
          >删除</el-button
        >
        <el-button
          size="small"
          @click="moveTo(checkRadioData, 'move')"
          :disabled="checkRadioData.length == 0"
          class="ele-btn-icon"
          >移动到</el-button
        >
      </template>
      <!-- 编码列 -->
      <template v-slot:code="{ row }">
        <el-link type="primary" :underline="false" @click="handleView(row)">
          {{ row.code }}
        </el-link>
      </template>
      <!-- 操作列 -->
      <template v-slot:action="{ row }">
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-edit"
          @click="handEdit(row)"
        >
          编辑
        </el-link>
      </template>
    </ele-pro-table>
    <DialogMoveto ref="movetoRef" @success="sucesstion" />
  </div>
</template>

<script>
  import EquipmentSearch from './equipment-search.vue';
  import DialogMoveto from './DialogMoveTo.vue';
  import {
    getAssetList,
    downloadAsset,
    getNetworkCount,
    batchDel
  } from '@/api/ledgerAssets';
  import dictMixins from '@/mixins/dictMixins';
  import axios from 'axios';
  import {
    API_BASE_URL,
    TOKEN_HEADER_NAME,
    LAYOUT_PATH
  } from '@/config/setting';
  import { download } from '@/utils/file';
  import { getToken, setToken } from '@/utils/token-util';

  export default {
    components: { EquipmentSearch, DialogMoveto },
    mixins: [dictMixins],
    props: {
      // 类别id
      categoryId: [Number, String],
      rootId: [Number, String],
      current: {
        typeof: Object,
        default: {}
      }
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          },
          {
            columnKey: 'index',
            type: 'index',
            label: '序号',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'fixCode',
            label: '固资编码',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            columnKey: 'code',
            slot: 'code',
            label: '设备编码',
            showOverflowTooltip: true,
            minWidth: 130
          },
          // {
          //   prop: 'code',
          //   label: '设备编码',
          //   showOverflowTooltip: true,
          //   minWidth: 110,
          //   slot: 'code'
          // },
          {
            prop: 'name',
            label: '设备名称',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'code1',
            label: '编号',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'category.modelType',
            label: '型号',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'category.specification',
            label: '规格',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'pathName',
            label: '位置',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row) => {
              return _row.position[0].pathName;
            }
          },
          {
            prop: 'source',
            label: '生命周期',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'networkStatus',
            label: '网络状态',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row) =>
              this.getDictValue('网络状态', _row.networkStatus)
          },
          {
            columnKey: 'action',
            slot: 'action',
            label: '操作',
            minWidth: 100
          }
        ],
        networkCounts: {},
        checkRadioData: []
      };
    },
    created() {
      this.requestDict('网络状态');
    },
    methods: {
      // 刷新回调
      sucesstion(is) {
        if (is) {
          this.reload();
        }
        this.checkRadioData = [];
      },
      // 移动到
      moveTo(arr, type) {
        this.$refs.movetoRef.open(arr, this.current, type);
      },
      // 全选
      changeSelectAll(arr) {
        console.log(arr);
        if (arr.length != 0) {
          this.checkRadioData = arr;
        } else {
          this.checkRadioData = [];
        }
      },
      selectChange(selection, row) {
        if (selection.length != 0) {
          this.checkRadioData = selection;
        } else {
          this.checkRadioData = [];
        }
      },
      handlDelete() {
        if (this.checkRadioData.length != 0) {
          this.$confirm('是否删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async () => {
              const paramsArr = this.checkRadioData.map((item) => {
                return item.id;
              });
              await batchDel(paramsArr);
              this.sucesstion(true);
            })
            .catch(() => {});
        }
      },

      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        this.getNetWork(page, limit, where, order);
        return getAssetList({
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          categoryLevelId: this.categoryId,
          rootCategoryLevelId: this.rootId
        });
      },
      async getNetWork(page, limit, where, order) {
        const res = await getNetworkCount({
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          categoryLevelId: this.categoryId,
          rootCategoryLevelId: this.rootId
        });
        this.networkCounts = res;
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ pageNum: 1, where: where });
      },

      // 跳转到详情页
      handleView({ id }) {
        this.$router.push({
          path: '/ledgerAssets/equipment/detail',
          query: {
            id
          }
        });
      },
      // 跳转到编辑
      handEdit({ id }) {
        this.$router.push({
          path: '/ledgerAssets/equipment/edit',
          query: {
            id
          }
        });
      },
      // 跳转到详情页
      add() {
        this.$router.push({
          path: '/ledgerAssets/equipment/edit'
        });
      },

      exportFile() {
        let params = {
          ...this.$refs.search.where,
          exportType: 1,
          categoryLevelId: this.categoryId,
          rootCategoryLevelId: this.rootId
        };
        // downloadAsset(params, '设备台账导出数据');
        axios({
          url: `${API_BASE_URL}/main/asset/page/export`,
          method: 'post',
          responseType: 'blob',
          headers: {
            Authorization: getToken()
          },
          data: params
        }).then((res) => {
          download(res.data, '设备台账导出数据');
        });
      }
    },
    watch: {
      // 监听类别id变化
      categoryId() {
        this.reload();
      }
    }
  };
</script>
