<template>
  <div>
    <turnovercar-search
      ref="searchRef"
      @search="reload"
      :isConsumer="isConsumer"
    >
    </turnovercar-search>
    <!-- 数据表格 -->
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      height="calc(100vh - 265px)"
      full-height="calc(100vh - 116px)"
      tool-class="ele-toolbar-form"
      cache-key="systemOrgUserTable"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <el-checkbox v-model="isConsumer">显示已消耗</el-checkbox>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-download"
          class="ele-btn-icon"
          @click="btnExport"
        >
          导出
        </el-button>
      </template>

      <!-- 编码列 -->
      <template v-slot:code="{ row }">
        <el-link type="primary" :underline="false" @click="details(row)">
          {{ row.code }}
        </el-link>
      </template>
      <!-- 分类列 -->
      <template v-slot:categoryLevelPath="{ row }">
        <div>
          {{ row.category.categoryLevelPath }}
        </div>
      </template>
      <!-- 操作列 -->
      <template v-slot:action="{ row }">
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-edit"
          @click="goEdit(row)"
        >
          编辑
        </el-link>
      </template>
    </ele-pro-table>
  </div>
</template>

<script>
  import TurnovercarSearch from './turnovercar-search.vue';
  import { getAssetList, downloadAsset } from '@/api/ledgerAssets';
  import dictMixins from '@/mixins/dictMixins';
  // import { downloadAction } from '@/api/flowable/manage';
  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: { TurnovercarSearch },
    mixins: [dictMixins],
    props: {
      // 类别id
      categoryId: [Number, String],
      rootId: [Number, String]
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: 'left'
          },
          {
            columnKey: 'index',
            type: 'index',
            label: '序号',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '编码',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'code',
            fixed: 'left'
          },
          {
            prop: 'name',
            label: '名称',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'codeNumber',
            label: '编号',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'fixCode',
            label: '固资编码',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'texture',
            label: '材质',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.getDictValue(
                '周转车材质',
                _row.categoryDetail.texture
              );
            }
          },
          {
            prop: 'modelType',
            label: '型号',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return _row.category.modelType;
            }
          },
          {
            prop: 'categoryLevelPath',
            label: '分类',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'categoryLevelPath'
          },
          {
            prop: 'ownershipGroupName',
            label: '所在部门',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'totalSum',
            label: '生命周期',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.getDictValue('生命周期', _row.position[0].type);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 100,
            align: 'left',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true,
            fixed: 'right'
          }
        ],
        isConsumer: false
      };
    },
    created() {
      this.requestDict('生命周期');
      this.requestDict('周转车材质');
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getAssetList({
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          categoryLevelId: this.categoryId,
          rootCategoryLevelId: this.rootId
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ pageNum: 1, where: where });
      },

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

      // 导出
      btnExport() {
        let params = {
          ...this.$refs.searchRef.where,
          exportType: 2,
          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>
<style lang="scss" scoped>
  .ele-btn-icon {
    margin-left: 20px;
  }
</style>
