/*
 *@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"

    />

    <orderEdit :formExtendedField="formExtendedField" :title="card_title" :record="detailsState === 2 ? mainCurrent : null" v-show="detailsState === 2" :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 }"
        @view="handleView"
        @clickRow="handleRowClick"
      >
        <p-button slot="extra" type="primary" :disabled="mainsSelectedRowKeys.length === 0" @click.stop="handelFeedback('批量计划', mainsSelectedRowKeys)">批量计划</p-button>
        <a-space slot="operation" slot-scope="slotProps">
          <a href="javascript:;" :style="slotProps.record.colorTagFontColor&&{color: slotProps.record.colorTagFontColor}" @click.stop="handelFeedback('计划', [slotProps.record])">计划</a>
        </a-space>
      </mainTable>
    </div>
  </div>
</template>
<script>
import useMixin from '../$common/mixins/useMixin.js';
import dictionary from '../$common/mixins/dictionary.js';
import {

  mainTableInfoUrl,
  printUrl,
  exportUrls,
} 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,
  },
  mixins: [useMixin, dictionary],

  data() {
    return {
      queryFromItems: queryFormConfig(this),
      currentData: null,

      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: {
    // 打印
    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);
      }
    },


    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,productName=[]
        arrData.forEach((item) => {
          item.productName&&productName.push(item.productName)
          planTotalPcs += item.pcs;
        });

        this.currentData = {
          planTotalPcs,
          productName:productName.join(',')
        };
      }

      //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>
