/*
 *@description：运输计划单
 *@author: hudw
 *@date: 2023-06-16 11:35:37
*/
<template>
  <div class="pages">
    <plan-calendar/>

    <updateFormModal
      ref="updateFormModal"
      rowKey="shipbillId"
      :visible.sync="visible"
      :view="2"
      @ok="handle_ok"
      :title="card_title"
      :currentData="currentData"
      :gridDataSource="gridDataSource"
      :options="options"

    />
    <!-- :goodsTableColumn="goodsColumns" -->
    <!-- :formExtendedField="formExtendedField" -->

    <!-- <orderView
      v-show="detailsState === 1"
      :parcelTable="true"
      :record="detailsState === 1 ? mainCurrent : {}"
      :options="options"
      @back="onBack"
    /> -->


    <orderEdit :st="detailsState" :title="card_title" :record="detailsState > 0 ? mainCurrent : null" v-show="detailsState >0" :options="options" @back="onBack" />
    <div class="pages" style="display:flex;flex-direction:column;" v-show="detailsState === 0">
      <searchBar ref="searchBar" :fromItems="queryFromItems" :options="options" @submit="searchSubmit" />
      <mainTable
        ref="mainTable"
        title="运输计划单"
        :url="mainTableInfoUrl"
        :options="options"
        :activeID="mainId"
        @selectChange="selectChange"
        :operation="{ width: 200 }"
        @clickRow="handleRowClick"
      >
        <p-button slot="extra" type="primary"  @click.stop="addOder">新增</p-button>
        <export-button
            slot="extra"
            type="default"
            :getSearchValue="()=>$refs.searchBar&&$refs.searchBar.getFieldsValue()"
            :ids="exportUrls.ids"
            :pages="mainPages"
            downloadFileName="计划单信息"
            v-bind="exportUrls">导出</export-button>
            <a-space slot="operation" slot-scope="slotProps">
          <a href="javascript:;" @click.stop="handleView(slotProps.record)">查看</a>
          <a href="javascript:;" @click.stop="editOder(slotProps.record)">修改</a>
          <a href="javascript:;" @click.stop="remove([slotProps.record.planBillId])">删除</a>
        </a-space>
      </mainTable>
    </div>
  </div>
</template>
<script>
import useMixin from '../$common/mixins/useMixin.js';
import dictionary from '../$common/mixins/dictionary.js';
import exportButton from '@/blocks/components/export-modal/modal.index.vue'

import {

  mainTableInfoUrl,
  printUrl,
  exportUrls,
  deleteMainItem,
} from './api.js';
import orderEdit from './orderEdit/index.orderEdit.vue';
import queryFormConfig from './queryFormConfig';
import { toPrintServer, printAsn } from '@/utils/printServe.js';

import mainTable from './mainTable/index.mainTable.vue'; /**主表 */
import updateFormModal from './updateModal/index.updateModal.vue'; /**主表 */


export default {
  components: {
    updateFormModal,
    mainTable,
    orderEdit,
    exportButton
  },
  mixins: [useMixin, dictionary],

  data() {
    return {
      queryFromItems: queryFormConfig(this),
      currentData: null,
      mainPages: { pageNum: 1, pageSize: 15 },
      exportUrls,
      printUrl,
      card_title: '',
      mainTableInfoUrl,
      visible: false,
      tabsKey: '1',
      gridDataSource:[],
      detailsState: 0
      /**0=不显示,1=查看,2=新建,3=修改,4=复制 */

    };
  },
  watch: {

    tabsKey(value) {
      if (this.mainCurrent) {
        this.handleRowClick(null, this.mainId, this.mainCurrent);
      }
    },
  },
  computed: {

  },
  mounted() {},
  methods: {
    addOder() {
      this.detailsState = 2
      this.card_title = '新建计划单'
      this.mainCurrent=null
    },
    editOder(data) {
      this.detailsState = 3
      this.card_title = '修改计划单';
      this.mainCurrent = data;

    },
    doneBefore(res) {
      if (res.success) {
        const { pageNum, pageSize } = res.data
        this.mainPages = { pageNum, pageSize }
      }
    },
    // 打印
    async printAsnFun(selectedRowKeys) {
      if (selectedRowKeys.length < 1) {
        this.$message.error('请选择需要打印的订单');
        return;
      }

      // 请选择需要打印的订单/
      let data = {
        params: {
          shipbillIds: selectedRowKeys,
        },
        printBillCode: 'TMS_ALLSHIPBILL_PRINT',
      };
      try {
        let res = await printAsn(data);
        if (!res.success) {
          this.$exception(res);
          return;
        }
        if (!res.data) {
          this.$message.error('打印数据为空');
          return;
        }
        const printRes = await toPrintServer(res.data.printData, { url: res.data.printAddress });
        if (printRes.success) {
          this.$message.success(printRes.message);
        } else {
          this.$exception(printRes);
        }
      } catch (error) {
        this.$exception(error);
      }
    },

    remove(arr) {
      deleteMainItem(this,arr)
    },
    selectChange(selectKey, selectData) {
      this.exportUrls.ids = selectKey;
      this.mainsSelectedRowKeys = selectData
    },
    handelFeedback(title, arrData) {
      this.card_title = title;

      this.gridDataSource=arrData

      if (arrData.length > 0) {
        let planTotalPcs = 0;
        arrData.forEach((item) => {
          planTotalPcs += item.pcs;
        });

        this.currentData = {
          planTotalPcs,
        };
      }

      //name: 'planTotalPcs',
      //label: '计划箱数',
      if (title == '计划') {
      } else if (title == '批量计划') {
      }

      this.$nextTick(() => {
        this.visible = true;
      });
    },

    handle_ok(submit) {
      submit().then((values) => {
        this.$refs.mainTable.reload()
      });
    },

    handleRowClick(event, key, data) {
      this.mainId = key;


    },
  },
};
</script>
<style lang='less'>
</style>