<template>
  <div class="page-box" v-loading="Loading.appMainLoading">
    <tablePagination
      :currenButtonData="currenButtonData"
      :tableData="tableData"
      :tableLoading="Loading.tableLoading"
      :tableColumns="tableColumns"
      @rowDrop="rowDrop"
      :totalCount="totalCount"
      :multipleSelection="multipleSelection"
      :MaxResultCount="PageListParams.MaxResultCount"
      @topbutton="topbutton"
      @inlineDialog="inlineDialog"
      @sortChange="sortChange"
      @alertoldSkipCount="alertoldSkipCount"
      @alterResultCount="alterResultCount"
      @handleSelectionChange="handleSelectionChange"
      :currentPageProps="oldSkipCount"
      :quicklySearchOption="quicklySearchOption"
      @quicklySearchClick="quicklySearchClick"
      @quicklySearchClear="quicklySearchClear"
      :primarySearchOption="primarySearchOption"
      @overallSearchFormClick="overallSearchFormClick"
      :httpOverallSearchData="httpOverallSearchData"
      :tableRowClassName="tableRowClassName"
    >
    </tablePagination>
    <!-- 抽屉 -->
    <curren-Drawer
      :title="tableColumns"
      :DrawerLoading="Loading.DrawerLoading"
      :drawer="displayDialog.detailsDialog"
      :propsData="propsData"
      :Butttondata="DrawerButtonData"
      :tabsDesTions="tabsDesTions"
      @drawerShut="(val) => (displayDialog.detailsDialog = val)"
      @drawerbutton="drawerbutton"
      @handleCommand="drawerHandle"
      @close-value="closeValue"
      :tableColumns="detailsTableColumns"
      :totalCount="totalCountDetails"
      :MaxResultCount="MaxResultCountDetails"
      @alterResultCountDetails="alterResultCountDetails"
      @alertoldSkipCountDetails="alertoldSkipCountDetails"
    ></curren-Drawer>
  </div>
</template>
<script>
import { getPageListWip,putawayPrintUpdateRemark,getDetailed } from "@/api/wms-api"
import { tableMixins } from "@/mixins/TableMixins"
import { LoadingMixins } from "@/mixins/LoadingMixins"
import { drawerMixins } from "@/mixins/drawerMixins"
import { TableHeaderMixins } from "@/mixins/TableHeaderMixins"
import { mixins } from "@/mixins/mixins"
import { initPrintAllData } from '@/mixins/printMixin'
import { getInventoryLabelByCodes} from "@/api/wms-core"
export default {
  name: "PutawayNote",
  mixins: [
    tableMixins,
    LoadingMixins,
    drawerMixins,
    TableHeaderMixins,
    mixins,
  ],
  data () {
    return {
      URL: 'wms/store/putaway-note',
      //常用按钮数据
      currenButtonData: [
        this.defaultExportBtn(),//导出
        this.defaultFieldSettingBtn(),//字段设置
        this.defaultFreshBtn(),//刷新
        this.defaultFilterBtn(),//筛选
      ],
      DrawerButtonData: [
        {
          type: 'info',
          icon: 'el-icon-printer',
          label: '打印',
          hide: false,
          name: "print",
          size: 'mini'
        },
      ],
    };
  },
  mounted () {
    this.paging();
  },
  methods: {
    //渲染数据
    paging(callback) {
      this.Loading.tableLoading = true;
      this.PageListParams.SkipCount = (this.oldSkipCount - 1) * this.PageListParams.MaxResultCount,
      getPageListWip(this.PageListParams, 'wms/store/putaway-note/get-list-by-purchase').then(res => {
          this.tableData = res.items
          this.totalCount = res.totalCount
          this.pagingCallback(callback)
        }).catch(err => {
          this.Loading.tableLoading = false
        })
    },
    //抽屉常用按钮
    drawerbutton (val) {
      // 打印
      if(val == 'print'){
        let _ids = []
        this.tableDataDetails.details.forEach(item => {
          _ids.push(item.handledPackingCode)
        });
        this.Loading.appMainLoading = true;
        getInventoryLabelByCodes(_ids).then(async res => {
          let _printData = JSON.parse(JSON.stringify(this.tableDataDetails))
          if(res && res.length > 0){
            _printData.supplierCode = res[0].supplierCode;
            _printData.supplierSimpleName = res[0].supplierSimpleName;
            res.forEach((item)=>{
              _printData.details.forEach((item2)=>{
                if(item.itemCode == item2.itemCode){
                  item2.supplierItemCode = item.supplierItemCode
                }
                // 时间转换格式为20240708
                item2.arriveDate = item2.arriveDate.slice(0,10).replaceAll("-","")
              })
            })
          }
          // 按照物料单号和储位相同的汇总
          let _sumArr = {}
          _printData.details.forEach(item=>{
            let _name = item.itemCode + '_' + item.toLocationErpCode
            if(_sumArr.hasOwnProperty(_name)){ _sumArr[_name].qty += Number(item.qty) }
            else{ _sumArr[_name] = JSON.parse(JSON.stringify(item)) }
          })
          _printData.details = []
          for(let i in _sumArr){ _printData.details.push(_sumArr[i]) }
          let data = await initPrintAllData(_printData,'ysd.rdlx',false,false,true);
          this.Print(data).then(()=>{
            // 记录已打印接口，打印成功后刷新页面
            putawayPrintUpdateRemark(this.propsData.id)
            .then(res=>{
              this.Loading.appMainLoading = false;
              this.paging()
              getDetailed(this.propsData.id, this.URL).then(res => {
                this.propsData = res
              })
            })
            .catch(err => {
              this.Loading.appMainLoading = false;
            })
          })
        }).catch(err => {
          this.Loading.appMainLoading = false;
        })
      }
    },
    // 已打印底色判断
    tableRowClassName({row, rowIndex}){
      if (row.remark == "已打印") {
        return 'success-row';
      }
      return '';
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../../styles/basicData.scss";
</style>
<style lang="scss">
  .el-table .success-row {
    background: #d5f1e1 !important;
    td{
      background: #d5f1e1 !important;
    }
  }
</style>