<template>
  <div style="padding: 0 10px;">
    <!-- 导航栏 -->
    <Navbar :title="'盘亏数据处理'" :is-go-back="false" />
    <!-- 操作按钮 -->
    <a-space>
      <a-button type="primary" @click="confirmSave">开始处理</a-button>
    </a-space>
    <!-- 表格 -->
    <CommonTable :columns="columns" :data-source="checkVouchsDatas" :have-page="false">
      <template #bodyCell="{ column, index }">
        <template v-if="column.dataIndex === 'index'">
          {{ index + 1 }}
        </template>
      </template>
    </CommonTable>
    <!-- 模态框 -->
    <a-modal v-model:open="modal" title="提示" @ok="save" @cancel="cancel">
      <p>确认开始处理生成出库单？</p>
    </a-modal>
  </div>
  <Loading v-if="loading" />
</template>

<script>
import Common from '../common/Common.js';
import { UserStorageResource } from 'pc-component-v3';
import { message } from 'ant-design-vue';
import CommonTable from '../common/CommonTable.vue';

export default {
  components: {
    CommonTable,
  },
  data() {
    return {
      loading: false,
      checkVouchsDatas: [],
      checkVouchId: null,
      modal: false,
      columns: [
        { title: '序号', dataIndex: 'index' },
        { title: '入库单号', dataIndex: 'stockInDocumentNo' },
        { title: '批号', dataIndex: 'batchNo' },
        { title: '存货名称', dataIndex: 'invName' },
        { title: '规格型号', dataIndex: 'invType' },
        { title: '存货编号', dataIndex: 'invCode' },
        { title: '货位名称', dataIndex: 'positionName' },
        { title: '货位条码', dataIndex: 'positionBarCode' },
        { title: '账面数量', dataIndex: 'accountQuantity' },
        { title: '盘点数量', dataIndex: 'checkQuantity' },
      ],
    };
  },
  mounted() {
    const _self = this;
    const uuid = _self.$route.params.uuid;
    UserStorageResource.uniqueByKey(uuid + '_modelData').then(
      str => {
        if (str.data) {
          const modelData = JSON.parse(str.data);
          _self.checkVouchId = modelData.id;
          _self.queryCheckVouchses();
        }
      },
      errorData => {
        Common.processException(errorData);
      },
    );
  },
  methods: {
    // 查询需要盘亏的数据
    queryCheckVouchses() {
      console.log(this.checkVouchId);
      const _self = this;
      _self.loading = true;
      $.ajax({
        type: 'get',
        dataType: 'json',
        url: Common.getApiURL('checkVouchsResource/getCheckLossDatas?checkVouchId=' + _self.checkVouchId),
        contentType: 'application/json',
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        success: function (success) {
          if (success.errorCode == 0) {
            console.log(success);
            if (success.datas && success.datas.length > 0) {
              _self.checkVouchsDatas = success.datas;
            }
          } else {
            message.warning(success.errorMessage);
          }
          _self.loading = false;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          _self.loading = false;
          Common.processException(XMLHttpRequest, textStatus, errorThrown);
        },
      });

    },
    // 开始处理
    confirmSave() {
      this.modal = true;
    },
    // 取消处理
    cancel() {
      this.modal = false;
    },
    // 确认处理
    save() {
      const _self = this;
      this.modal = false;
      if (this.checkVouchsDatas.length === 0) {
        message.warning('没有可处理的数据');
        return;
      }

      _self.loading = true;
      $.ajax({
        type: 'get',
        dataType: 'json',
        url: Common.getApiURL('checkVouchsResource/processCheckLossDatas?checkVouchId=' + _self.checkVouchId),
        contentType: 'application/json',
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        success: function (success) {
          if (success.errorCode == 0) {
            message.success('处理成功！');
          } else {
            message.warning(success.errorMessage);
          }
          _self.loading = false;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          _self.loading = false;
          Common.processException(XMLHttpRequest, textStatus, errorThrown);
        },
      });

    },
  },
};
</script>

<style scoped>
/* 可以根据需要添加额外的样式 */
</style>