<template>
  <CommonTable
    ref="table" :columns="columns" :data-source="assetChangeWaitDtos" :total="pagination.total"
    :is-select="true" @get-pager="getPageParams" @get-selected="getSelectParams"
  >
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'clientName'">
        {{ record.clientName }} {{ record.organizationName }}
      </template>
      <template v-if="column.dataIndex === 'responseClientName'">
        {{ record.responseClientName }} {{ record.responseOrganizationName }}
      </template>
    </template>
  </CommonTable>

  <Loading v-if="loading" />
</template>

<script>
import AssetBatchOperationQueueResource from '../../api/asset/AssetBatchOperationQueueResource.js';
import Common from '../../common/Common.js';
import { Uuid } from 'pc-component-v3';
import CommonTable from '../../common/CommonTable.vue';

export default {
  components: {
    CommonTable,
  },

  props: {
    changeType: {
      type: String,
      default: null,
    },
  },

  data: function () {
    return {
      assetChangeWaitDtos: [],
      pagination: {
        total: 0,
        per_page: Common.pageSize, // required
        current_page: 1, // required
        last_page: 0, // required
      },
      checked: false,
      tableId: Uuid.createUUID(),
      loading: false,
      columns: [
        { title: this.$t('lang.AssetBatchOperationQueue.assetName'), dataIndex: 'name', width: 160 },
        { title: this.$t('lang.AssetBatchOperationQueue.assetNumber'), dataIndex: 'assetNo', width: 240 },
        { title: this.$t('lang.AssetBatchOperationQueue.cardNumber'), dataIndex: 'no', width: 240 },
        { title: this.$t('lang.AssetBatchOperationQueue.ownerDepartment'), dataIndex: 'clientName', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.useDepartment'), dataIndex: 'responseClientName', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.type'), dataIndex: 'type', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.projectName'), dataIndex: 'projectItemName', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.assetCategory'), dataIndex: 'categoryName', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.originalValue'), dataIndex: 'orginalValue', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.placeOfPlacement'), dataIndex: 'savePosition', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.aLocationToBePlacedIn'), dataIndex: 'locationName', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.startDate'), dataIndex: 'depreciationStartDate', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.user'), dataIndex: 'useUserName', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.userInput'), dataIndex: 'useUserNameInput', width: 150 },
        { title: this.$t('lang.AssetBatchOperationQueue.usageStatus'), dataIndex: 'useStatusName', width: 150 },
      ].map(column => ({
        ...column,
        ellipsis: true,
        align: 'center',
        resizable: true,
      })),
      waitChangeAssetDtos: [],
    };
  },

  mounted: function () {
    var _self = this;
    _self.getDatas();

  },

  methods: {
    /**
     * 重置分页，然后查询
     */
    resetQuery: function () {
      var _self = this;
      _self.$refs.table.backFirstPage();
    },

    getPageParams: function (pager, pageSize) {
      var _self = this;
      _self.pagination.current_page = pager;
      _self.pagination.per_page = pageSize;
      _self.getDatas();
    },

    getSelectParams: function (selected) {
      this.waitChangeAssetDtos = selected.selectedRows;
      console.log(this.waitChangeAssetDtos);
    },

    /**
     * 调拨经手人框change事件
     */
    userValueChanged: function (newResponsibilityFieldValue) {
      this.userFieldValue = newResponsibilityFieldValue;
    },

    /**
     * /查询数据
     */
    getDatas: function (isClear = false, isClearTable = false) {
      var _self = this;
      if (isClearTable) {
        _self.$refs.table.backFirstPage();
        return;
      }
      _self.loading = true;
      var param = {
        range: {
          start:
            (_self.pagination.current_page - 1) * _self.pagination.per_page,
          length: _self.pagination.per_page,
        },
        changeType: _self.changeType,
      };
      AssetBatchOperationQueueResource.listByCondition(param).then(
        baseRangeResponse => {
          if (baseRangeResponse.errorCode == 0) {
            _self.pagination.total = baseRangeResponse.total;
            if (baseRangeResponse.datas && baseRangeResponse.datas.length > 0) {
              baseRangeResponse.datas.forEach(function (item) {
                item.assetSimpleResponse.assetChangeWaitId = item.id;
              });
              _self.assetChangeWaitDtos = baseRangeResponse.datas.map(item => item.assetSimpleResponse);
            } else {
              _self.assetChangeWaitDtos.splice(0, _self.assetChangeWaitDtos.length);
              _self.waitChangeAssetDtos.splice(0, _self.waitChangeAssetDtos.length);
            }
            if (isClear) {
              _self.$refs.table.clear();
            }
          } else {
            Common.showDialog('提示', baseRangeResponse.errorMessage, 'error');
          }
          _self.loading = false;
        },
        errorData => {
          _self.loading = false;
          Common.processException(errorData);
        },
      );
    },

    /**
     * 取消转移
     */
    removeAssetChangeWaitDtos: function () {
      var _self = this;

      if (_self.waitChangeAssetDtos.length < 1) {
        Common.showDialog('提示', '请选择数据后再提交', 'error');
        return;
      }
      _self.loading = true;
      if (_self.waitChangeAssetDtos.length > 0) {
        let removeDtos = [];
        _self.waitChangeAssetDtos.forEach(function (item) {
          let removeDto = {
            assetSimpleResponse: item,
            id: item.assetChangeWaitId,
          };
          removeDtos.push(removeDto);
        });
        AssetBatchOperationQueueResource.remove(removeDtos).then(
          baseListResponse => {
            if (baseListResponse.errorCode == 0) {
              _self.getDatas(true);
              Common.showDialog('提示', '取消成功', 'success');
            } else {
              Common.showDialog('提示', baseListResponse.errorMessage, 'error');
            }
            _self.loading = false;
          },
          errorData => {
            _self.loading = false;
            Common.processException(errorData);
          },
        );
      }
    },

    /**
     * 获取转移资产DTO集合
     */
    getAssetChangeDtos: function () {
      var _self = this;
      let assetChangeDtos = [];

      _self.waitChangeAssetDtos.forEach(function (item) {
        let assetChangeDto = {
          assetInstanceId: item.id,
          assetChangeWaitId: item.assetChangeWaitId,
        };
        assetChangeDtos.push(assetChangeDto);
      });

      return assetChangeDtos;
    },
  },
};
</script>

<style scoped></style>