<template>
  <div class="padListPageOuter">
    <padListPage
      :listUrl="listUrl"
      ref="padListPage_Ref"
      :padListData="padListData"
      :searchFormData="searchFormData"
      :searchForm="searchForm"
      :headerButton="headerButton"
      :padMainColumn="padMainColumn"
      :padDeatilColumn="padDeatilColumn"
      :listRowButton="listRowButton"
      :padListLoading="padListLoading"
      :padListPager="padListPager"
      :getPadList="getPadList"
      :intSearchFilters="intSearchFilters"
      :padListParams="padListParams"
      :mainIsShowDetail="true"
      @listRowButtonClick="listRowButtonClick"
      @headerButtonClick="headerButtonClick"
      @headerSearchHandle="headerSearchHandle"
      @resetSearchHandle="resetSearchHandle"
      :padMainStaColumn="[{prop:'number'},{prop:'jobStatus',filters:'jobStatus'}]"
    ></padListPage>
    <!-- :mainIsShowDetail="true" -->
    <!-- 完成库移弹窗 -->
    <el-dialog
      title="完成库移"
      :visible="compeletHandleVisible"
      v-loading="compeletLoading"
      top="5vh"
      width="90%">
      <div style="height:calc(100vh - 280px);overflow:auto">
        <currenForm
          :labelWidth="'90px'"
          ref="compeletHandleForm_Ref"
          :style="'width:calc(100% - 100px)'"
          size="medium"
          :searchData="compeletFormData"
          :searchForm="compeletForm"
          :rules="compeletRule"
        ></currenForm>
        <curren-descriptions
          v-if="compeletHandleVisible"
          :border="true"
          :column="3"
          :direction="'horizontal'"
          :colon="false"
          :tabsDesTions="padDeatilColumn"
          :propsData="currentRowData.details[0]"
          style="width:100%;padding-top:20px"
        ></curren-descriptions>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="compeletHandleClose()">取 消</el-button>
        <el-button type="primary" @click="compeletHandleSure()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看库存余额 -->
    <el-dialog
      class="showInventoryPop"
      title="查看库存余额"
      :visible="showInventoryVisible"
      top="5vh"
      width="90%">
      <div v-loading="showInventoryLoading" style="height:calc(100vh - 220px);overflow:auto">
        <div 
          class="showInventoryRow"
          v-for="(item,index) in inventoryTableData"
          :key="index">
          <div class="commonRowTitle">
            <el-tag size="mini" effect="dark">{{index + 1}}</el-tag>
            <div>{{item[inventoryColumn[6].prop]}}</div>
          </div>
          <curren-descriptions
            :column="2"
            :border="true"
            :direction="'horizontal'"
            :tabsDesTions="inventoryColumn"
            :propsData="item"
            style="width:100%;padding-top:20px"
            :labelStyle="{'width':'200px'}"
          ></curren-descriptions>
        </div>
        <el-empty v-if="(!inventoryTableData || inventoryTableData.length <= 0) && !showInventoryLoading" description="暂无数据"></el-empty>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="inventoryCloseHandle()">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { padListPageMixins } from "@/mixins/padListPageMixins"
import padListPage from "./components/padListPage/index"
import currenForm from "@/components/currenForm"
import currenDescriptions from "@/components/currenDescriptions"
import { getInventoryByPackingCode } from '@/api/wms-api'
import { transferLibJobHandel } from '@/api/wms-job'
import { inventoryBalanceRecommendList } from '@/api/wms-pad'
import { filterSelectMixins } from '@/mixins/filter-Select'
import zh from '@/lang/zh'
let _Names = zh.ColumsNames // 业务字段

export default {
  name: 'isPadForTransferLibJob',
  mixins: [
    padListPageMixins,
    filterSelectMixins,
  ],
  components: { padListPage,currenForm,currenDescriptions },
  computed: {
    // 头部按钮显隐控制
    hideButton: function () {
      return function (val) {
        return this.showFilterSta == val
      }
    },
    // 单独行按钮显隐控制
    hideRowButton: function () {
      return function (row,val) {
        let data = true
        val.forEach(key => {
          if (row.jobStatus == key) {
            data = false
          }
        })
        return data
      }
    }
  },
  data () {
    return {
      listUrl:'wms/store/transfer-lib-job',
      searchFormData:{
        number:"",
        requestNumber:"",
      },
      searchForm:[
        {label:'传入申请单号',prop:'callRequestNumber',type:'input',width:'140px',action:'Like'},
        {label:'传入任务单号',prop:'callJobNumber',type:'input',width:'140px',action:'Like'},
      ],
      showFilterSta:false,
      headerButton:[
        {
          type: 'warning',
          label: '查看待执行数据',
          name: "showCanHandle",
          hide: () => { return this.hideButton(true) },
        },
        {
          type: 'danger',
          label: '重置[待执行]数据',
          name: "showAllData",
          hide: () => { return this.hideButton(false) },
        },
      ],
      padMainColumn:[
        { label:'任务单号',prop:'number'},
        { label:'申请单号',prop:'requestNumber'},
        // { label: "状态", prop: "jobStatus",type: "tagFilter", filters: "jobStatus",titleBack:true },
        { label: "传入申请单号", prop: "callRequestNumber" },
        { label: "传入任务单号", prop: "callJobNumber" },
        { label: "任务描述", prop: 'jobDescription' },
        { label: "使用在途库", prop: 'useOnTheWayLocation', type: "filter", filters: "whetherOrNot" },
        { label: "已确认", prop: 'confirmed',type: "filter", filters: "whetherOrNot" },
        { label: "确认时间", prop: 'confirmTime' },
        { label: "备注", prop: "remark" },
        { label: "操作员", prop: "worker" },
        { label: "承接用户", prop: "acceptUserName" },
        { label: "承接时间", prop: 'acceptTime', type: "dateTime" },
        { label: "完成者用户名", prop: 'completeUserName' },
        { label: "完成时间", prop: "completeTime", type: "dateTime" },
        { label: "创建时间", prop: 'creationTime', type: "dateTime" },
        // { label: "上次修改时间", prop: "lastModificationTime", type: "dateTime" },
        // { label: "回调服务名称", prop: "callServerName" },
        // { label: "回调业务类型", prop: "callBusinessType" },
      ],
      padDeatilColumn:[
        { label: _Names.itemCode, prop: "itemCode"},
        { label: "物品名称", prop: "itemName" },
        { label: "物品描述1", prop: "itemDesc1" },
        { label: "物品描述2", prop: "itemDesc2" },
        { label: "请求数量", prop: 'requestQty' },
        { label: "请求ERP库位", prop: 'requestLocationErpCode' },
        { label: "推荐来源数量", prop: 'recommendFromQty' },
        { label: "推荐目标数量", prop: 'recommendToQty' },
        { label: "推荐来源箱标签", prop: 'recommendFromPackingCode' },
        { label: "推荐目标箱标签", prop: 'recommendToPackingCode' },
        { label: "推荐来源库位", prop: 'recommendFromLocationCode' },
        { label: "推荐目标库位", prop: 'recommendToLocationCode' },
        { label: "推荐来源ERP库位", prop: 'recommendFromLocationErpCode' },
        { label: "推荐目标ERP库位", prop: 'recommendToLocationErpCode' },
        { label: "推荐来源批次", prop: 'recommendFromLot' },
        { label: "推荐目标批次", prop: 'recommendToLot' },
        { label: "推荐来源过期时间", prop: 'recommendFromExpireDate', type: "dateTime" },
        { label: "推荐目标过期时间", prop: 'recommendToExpireDate', type: "dateTime" },
        { label: "实际来源数量", prop: 'handledFromQty' },
        { label: "实际目标数量", prop: 'handledToQty' },
        { label: "实际来源箱标签", prop: 'handledFromPackingCode' },
        { label: "实际目标箱标签", prop: 'handledToPackingCode' },
        { label: "实际来源库位", prop: 'handledFromLocationCode' },
        { label: "实际目标库位", prop: 'handledToLocationCode' },
        { label: "实际来源ERP库位", prop: 'handledFromLocationErpCode' },
        { label: "实际目标ERP库位", prop: 'handledToLocationErpCode' },
        { label: "实际来源批次", prop: 'handledFromLot' },
        { label: "实际目标批次", prop: 'handledToLot' },
        { label: "实际来源过期时间", prop: 'handledFromExpireDate', type: "dateTime" },
        { label: "实际目标过期时间", prop: 'handledToExpireDate', type: "dateTime" },
        { label: "位置码", prop: 'positionCode' },
        { label: "生产线", prop: 'prodLine' },
        { label: "任务状态", prop: "jobStatus",type: "filter", filters: "jobStatus" },
        { label: "推荐类型", prop: 'recommendType' },
      ],
      listRowButton:[
        {
          type: 'success',
          label: '完成库移',
          name: "compelet",
          hide: (row) => { return this.hideRowButton(row,[1,2,4]) },
        },
        {
          type: 'primary',
          label: '查看库存余额',
          name: "showInventory",
        },
      ],
      // 完成库移显隐
      compeletHandleVisible:false,
      compeletLoading:false,
      compeletFormData: {
        packingCode:"",
        fromLocationCode:"",
        toLocationCode:"",
      },
      compeletForm: [
        { type: "input", label: "箱码", prop: 'packingCode', colSpan: 12,  },
        { type: "input", label: "来源库位", prop: 'fromLocationCode', colSpan: 12,  },
        { type: "input", label: "目标库位", prop: 'toLocationCode', colSpan: 12, },
        // filterSelect示例：
        // { type: "filterSelect", label: "目标库位", prop: "toLocationCode", optionsLabel: "name", optionsValue: "code", 
        //   focus: (type,val) => { return this.getFilterList(type, val, "basedata/location")},colSpan: 12 
        // },
      ],
      compeletRule: {
        packingCode: [{ required: true, trigger: "blur", message: "不可为空" }],
        fromLocationCode: [{ required: true, trigger: "blur", message: "不可为空" }],
        toLocationCode: [{ required: true, trigger: "blur", message: "不可为空" }],
      },
      // 当前行数据
      currentRowData:null,
      // 查看库存余额显隐
      showInventoryVisible:false,
      showInventoryLoading:false,
      inventoryTableData:[],
      inventoryColumn:[
        { label: _Names.itemCode, prop: "itemCode" },
        { label: "物品名称", prop: "itemName" },
        { label: "物品描述1", prop: "itemDesc1" },
        { label: "物品描述2", prop: "itemDesc2" },
        { label: "计量单位", prop: "uom" },
        { label: '数量', prop: "qty" },
        { label: "箱码", prop: 'packingCode' },
        { label: "批次", prop: 'lot' },
        { label: "供应商批次", prop: "supplierBatch" },
        { label: "标包数量", prop: "stdPackQty" },
        { label: "库位", prop: 'locationCode' },
        { label: "ERP储位", prop: "locationErpCode" },
        { label: "库位组", prop: 'locationGroup' },
        { label: "标准成本单价", prop: "stdCostPrice" },
        { label: "标准成本金额", prop: "stdCost" },
        { label: "备注", prop: 'remark' },
        { label: "序号", prop: 'serialNumber' },
        { label: "状态", prop: "status", type: "filter", filters: "openToClose" },
        { label: "库区", prop: 'locationArea' },
        { label: "管理类型", prop: "manageType", type: "filter", filters: "manageType" },
        { label: "入库时间", prop: 'putInTime', type:"dateTime" },
        { label: "生产时间", prop: "produceDate", type: "dateTime" },
        { label: "到货时间", prop: "arriveDate", type: "dateTime" },
        { label: "过期时间", prop: "expireDate", type: "dateTime" },
        { label: "创建时间", prop: "creationTime", type: "dateTime" },
      ]
    }
  },
  mounted(){
    this.initPage()
  },
  methods: {
    // 头部其他按钮点击事件
    headerButtonClick(item,index){
      if(item.name == 'showCanHandle'){
        this.showCanHandleFunc()
      }
      else if(item.name == 'showAllData'){
        this.showFilterSta = false
        document.getElementById("padListCard").scrollTop = 0
        this.clearSearchHandle(true)
        this.getPadList();
      }
    },
    // 筛选可执行功能
    showCanHandleFunc(){
      this.showFilterSta = true
      let _filters = [{
        logic:"And",
        column:"jobStatus",
        action:"In",
        value: JSON.stringify([1,2,4,30])
      }]
      this.padListParams.condition.filters = _filters
      document.getElementById("padListCard").scrollTop = 0
      this.clearSearchHandle()
      this.getPadList();
    },
    // 当前行按钮点击事件
    listRowButtonClick(row,item){
      if(item.name == "showInventory"){
        this.currentRowData = row
        this.showInventoryVisible = true
        let _params = {
          itemCode:row.details[0].itemCode,
          qty:9999999999,
          statuses:'OK',
          locationTypes:'RAW'
        }
        this.showInventoryLoading = true
        inventoryBalanceRecommendList(_params)
        .then(res=>{
          this.inventoryTableData = res
          this.showInventoryLoading = false
        })
        .catch(err=>{
          this.showInventoryLoading = false
        })
      }
      if(item.name == "compelet"){
        this.currentRowData = row
        this.compeletHandleVisible = true
      }
    },
    headerSearchHandle(){
      this.showFilterSta = false
    },
    resetSearchHandle(){
      this.clearSearchHandle(true)
      this.showFilterSta = false
    },
    // 完成库移-关闭
    compeletHandleClose(){
      this.compeletFormData = {
        packingCode:"",
        fromLocationCode:"",
        toLocationCode:"",
      }
      if(this.$refs.compeletHandleForm_Ref)this.$refs.compeletHandleForm_Ref.clearValidate()
      this.compeletHandleVisible = false
    },
    // 完成库移-确定
    compeletHandleSure(val){
      let _form = this.$refs.compeletHandleForm_Ref.$refs.form
      _form.validate((valid) => {
        if (valid) {
          this.compeletLoading = true
          // 先获取库存余额进行部分项赋值
          getInventoryByPackingCode(this.compeletFormData.packingCode).then(inventory => {
            // 目前定的需求为：details中只有一条明细，所以按钮写在最外层，默认赋值第一位
            let _data = this.currentRowData
            _data.details[0].handledFromPackingCode = this.compeletFormData.packingCode
            _data.details[0].handledToPackingCode = this.compeletFormData.packingCode
            _data.details[0].handledFromLocationCode = this.compeletFormData.fromLocationCode
            _data.details[0].handledToLocationCode = this.compeletFormData.toLocationCode
            _data.details[0].handledToLot = inventory.lot
            _data.details[0].handledFromLot = inventory.lot
            _data.details[0].handledFromSupplierBatch = inventory.supplierBatch
            _data.details[0].handledToSupplierBatch = inventory.supplierBatch
            _data.details[0].handledFromQty = inventory.qty
            _data.details[0].handledToQty = inventory.qty
            transferLibJobHandel(this.currentRowData.id, _data).then(res => {
              this.compeletLoading = false
              this.$successMsg("完成库移成功！")
              if(this.showFilterSta){
                this.showCanHandleFunc()
              }
              else{
                this.getPadList()
              }
              this.compeletHandleClose()
            }).catch(err => {
              this.compeletLoading = false
            })
          }).catch(err => {
            this.compeletLoading = false
          })
        } else {
          return false;
        }
      });
    },
    // 查看库存余额关闭
    inventoryCloseHandle(){
      this.showInventoryVisible = false
      this.inventoryTableData = []
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/padMain.scss";
.showInventoryPop{
  ::v-deep .el-dialog__body{
    padding:0 15px !important;
  }
  .showInventoryRow{
    margin-bottom: 20px;
    .el-descriptions{
      padding: 0 !important;
    }
  }
}
</style>