<template>
  <div class="appMainPage" v-loading="pageLoading">
    <!-- 标签管理-业务标签-补打标签 -->
    <!--新增-->
    <StepsFormAlone
      :stepArray="stepArray"
      ref="steps_bd"
      :hideLastBtsn="true"
    >
      <template v-slot:stepSlot1>
        <curren-Form
          size="medium"
          ref="form_bd"
          :searchData="selectData"
          :searchForm="selectForm"
          :rules="selectRules"
          :searchOptions="editOptions"
        ></curren-Form>
      </template>
      <template v-slot:stepSlot2>
        <currenTableFlex
          :flexTableData="flexTableData"
          :flexSearchOptions="editOptions"
          :flexTableColumns="flexTableColumns"
          :showAddBtn="false"
          :showAllDeleteButton="false"
        ></currenTableFlex>
      </template>
      <template v-slot:stepSlot3>
        <!-- 结果 -->
        <resultStatus>
           <template>
              <el-button @click="rest()">返回</el-button>
              <el-button type="primary" @click="printHandle()">打印</el-button>
           </template>
        </resultStatus>
      </template>
    </StepsFormAlone>
    <!-- 搜索按钮——窗体组件 -->
    <searchPage
      ref="searchTable"
      :tableLoading="Loading.autoTableLoading"
      :advancedFilter="advancedFilter()"
      :filterPageListParams="filterPageListParams"
      :formTitle="searchTitle"
      :displayDialog="displayDialog.AddNewDialog"
      :searchTableData="searchData"
      :searchTableColumns="searchColumns"
      :searchTotalCount="searchTotalCount"
      :supplierItemPage="searchPageListParams"
      @handleSelectionChange="prepareFormData"
      @SizeChange="searchAlterResultCount($event, searchPageListParams)"
      @CurrentChange="searchAlertoldSkipCount($event, searchPageListParams)"
      @tableButtonClick="searchSubmit(arguments)"
    ></searchPage>
  </div>
</template>
<script>
import currenForm from "@/components/currenForm"
import StepsFormAlone from "@/components/StepsFormAlone"
import resultStatus from "@/components/resultStatus"
import currenTableFlex from "@/components/currenTableFlex"
import {initPrintPackingCodeData} from "@/mixins/printMixin"
import { getDetailed} from "@/api/wms-api"
import { getOneInventoryLabelByCode} from "@/api/wms-core"
import _ from "lodash"
import { mixins } from "@/mixins/mixins"
import { LoadingMixins } from "@/mixins/LoadingMixins"
import { filterSelectMixins } from '@/mixins/filter-Select'
export default {
  mixins: [
    mixins,
    LoadingMixins,
    filterSelectMixins
  ],
  components: {
    currenForm,
    StepsFormAlone,
    resultStatus,
    currenTableFlex
  },
  name: "RepairLabels",
  data () {
    let _this = this
    return {
      pageLoading:false,
      // 步骤条options
      stepArray:[
        {title:"选择箱标签",next:()=>{return _this.firstNext()}},
        {title:"预览信息"},
        {title:"打印"},
      ],
      // 筛选数据
      selectData:{
        packingCode:null,
        canType:null,//1采购件  2制造件
      },
      // editOptions: {
      //   cantypeRadio:[
      //     {
      //       id: '1',
      //       value: 1,
      //       label: '采购件'
      //     },
      //     {
      //       id: '2',
      //       value: 2,
      //       label: '制造件'
      //     },
      //   ],
      // },
      // 筛选条件
      selectForm:[
        {
          type: "filterSelect", label: "箱标签", prop: "packingCode", optionsLabel: "itemCode", optionsValue: "packingCode", clearable:true,
          focus: (type,val) => { return this.getFilterList(type, val, "wms/inventory/Inventory-Balance")},
          searchButton: (val) => { this.showSerarchPage(val, 'wms/inventory/Inventory-Balance', 'InventoryBalance', '箱标签选择', this.selectData) },
          colSpan: 12,
        },
        // { type: "radio", label: "零件类型", prop: "canType",radio:'cantypeRadio', value: '1', colSpan: 24},
        // 制造件
        // { type: "select", label: "类型", prop: "packingCode", optionsLabel: "itemCode", optionsValue: "packingCode", focus: (val) => { return this.getSelectList(val, "wms/inventory/Inventory-Balance") }, colSpan: 12 },
      ],
      // 筛选Rules
      selectRules:{
        packingCode: [{ required: true, trigger: "change", message: "不可为空！" }],
      },
      // 箱码表格信息
      flexTableData:[],
      // 箱码表格头部信息
      flexTableColumns:[
        { label: "物品代码", prop: "itemCode", width:"auto" },
        { label: "物品名称", prop: "itemName", width:"auto" },
        { label: "箱标签", prop: "packingCode", width:"auto" },
        { label: "数量", prop: "qty", width:"auto" },
        { label: "计量单位", prop: "uom", width:"auto" },
        { label: "批次", prop: "lot", width:"auto" },
        { label: "生产日期", prop: "produceDate",type: "dateTime",width:"auto"  },
      ],
    };
  },
  methods: {
    // 返回
    rest(){
      this.selectData={
        packingCode:null
      };
      this.flexTableData = [];
      this.$refs.steps_bd.returnFirstActive()
      this.$refs.form_bd.getDom().resetFields();
    },
    // 第一步骤下一步操作
    firstNext(){
      return new Promise((resolve,reject) => {
        if(!this.selectData.packingCode || this.selectData.packingCode.length <= 0){
          this.$errorMsg('箱标签不能为空')
          reject()
        }else{
          // let params = {
          //   condition: {
          //     filters: []
          //   }
          // }
          // params.condition.filters.push(...this.isFilter("packingCode", this.selectData.packingCode))
          this.pageLoading = true
          // getDetailed(this.selectData.packingCode, "label/purchase-label/by-code")
          // getDetailed(this.selectData.packingCode, "label/inventory-label/by-code")
          getOneInventoryLabelByCode(this.selectData.packingCode)
            .then(res => {
              this.pageLoading = false
              this.flexTableData = []
              if (res != "") {
                this.flexTableData.push(res)
                this.flexTableData.forEach(item=>{
                  item.packingCode = item.code
                })
                resolve()
              }else{
                this.$warningMsg("未找到相关信息")
                reject()
              }
              //   this.flexTableData = res.items
              //   resolve()
              // } else {
              //   this.$warningMsg("未找到库存余额")
              //   reject()
              // }
            })
            .catch(err => {
              this.pageLoading = false
              reject()
            })
        }
      })
    },
    // 打印
    printHandle(){
      let _printData = {
        supplierSimpleName:this.flexTableData[0].supplierSimpleName || this.flexTableData[0].supplierName || "",
        supplierCode:this.flexTableData[0].supplierCode || "",
        title:"补打标签",
        contacts:this.flexTableData[0].contacts || "",
        customerAddressCode:this.flexTableData[0].customerAddressCode || "",
        remark:this.flexTableData[0].remark || "",
        planArriveDate:this.flexTableData[0].planArriveDate || "",
        poNumber:this.flexTableData[0].poNumber || "",
        asnNumber:this.flexTableData[0].asnNumber || "",
        details:[this.flexTableData[0]],
      }
      _printData.details[0].packingCode = _printData.details[0].code
      this.Print(initPrintPackingCodeData(_printData))
    },
    // //过滤查询条件
    isFilter (val, data) {
      let filter = [
        {
          logic: "And",
          column: val,
          action: "==",
          value: data
        }
      ]
      return filter
    },
  },
}
</script>