<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container"  v-loading="this.loading_add" element-loading-text="拼命加载中" element-loading-background="rgba(0, 0, 0, 0.1)">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.orderNo" clearable placeholder="单号" style="width: 185px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"
        />
        <el-input v-model="query.plateNo" clearable placeholder="车牌号" style="width: 185px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"
        />

        <el-select v-model="query.status" clearable placeholder="状态" value-key="id" class="filter-item" filterable
                   @keyup.enter.native="crud.toQuery"
        >
          <el-option
            style="width: 180px"
            v-for="item in this.dict.pick_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <rrOperation :crud="crud"/>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="crud.selections.length == 0"
          @click="xbjsButtonM(crud.selections)"
          v-permission="permission.xbjsButton"
        >
          接收确认
        </el-button>
      </crudOperation>
      <!--  出库确认    -->
      <el-dialog :close-on-click-modal="false" :visible.sync="onOff"
                 :title="'出库确认'" width="90%" top="20px"
      >
        <!--表格渲染-->
        <el-table ref="table" :data="taskLogListDataS" height="350px" size="small" style="width: 100%;"
                  @selection-change="selectionTaskLogMS" border
        >
          <el-table-column type="selection" width="55"/>
          <el-table-column prop="item.code" label="品番"/>
          <el-table-column prop="moveQty" label="拣货数量"/>
          <el-table-column prop="shipQty" label="发运数量"/>
          <el-table-column prop="srcPoint.code" label="起始点位"/>
          <el-table-column prop="dstPoint.code" label="目标点位"/>
          <!--          <el-table-column prop="dstStockCode" label="源容器编码"/>-->
          <el-table-column prop="dstStockCode" label="目标托盘号"/>
          <el-table-column prop="createBy" label="创建人"/>
          <el-table-column prop="createTime" label="创建时间"/>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="onOff = false">取消</el-button>
          <el-button type="primary" @click="confirmOutboundM(crud.selections)">确认</el-button>
        </div>
      </el-dialog>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="单号">
            <el-input v-model="form.orderNo" style="width: 370px;" :disabled="true"/>
          </el-form-item>
          <el-form-item label="车牌号">
            <el-input v-model="form.plateNo" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="订单数量">
            <el-input v-model="form.orderQty" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="出库数量">
            <el-input v-model="form.dispatchQty" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.description" :rows="3" type="textarea" style="width: 370px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" :height="crud.tableHeight" size="small"
                style="width: 100%;" @selection-change="crud.selectionChangeHandler" border
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="序号"/>
        <el-table-column prop="gcCode" label="接收工厂" width="100"/>
        <el-table-column prop="orderNo" label="单号" width="120px"/>
        <el-table-column prop="plateNo" label="车牌号" width="120px"/>
        <el-table-column prop="orderQty" label="订单数量"/>
        <el-table-column prop="dispatchQty" label="出库数量"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.pick_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="slQty" label="线边接收"/>
        <el-table-column prop="slBy" label="接收人"/>
        <el-table-column prop="slDate" label="接收日期"  width="150px"/>
        <el-table-column prop="tlQty" label="投料数"/>
        <el-table-column prop="tlBy" label="投料人"/>
        <el-table-column prop="tlDate" label="投料日期"  width="150px">
          <template slot-scope="scope">
            {{getFormatDate(scope.row.tlDate)}}
          </template>
        </el-table-column>
        <el-table-column prop="description" :show-overflow-tooltip="true" label="描述"/>
        <el-table-column prop="bhNo" label="备货单号" width="120px"/>
        <el-table-column prop="description" label="备注"/>
        <el-table-column prop="createBy" label="创建人"/>
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            {{getFormatDate(scope.row.createTime)}}
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudPickOut, {xbjsOut} from '@/api/pickOut'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { formatDate } from '@/utils/commonUtils'
import Cookies from "js-cookie";
const defaultForm = {
  id: null,
  orderNo: null,
  plateNo: null,
  orderQty: 0,
  dispatchQty: 0,
  enabled: true,
  description: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  status: 'OPEN',
  bhNo:null
}
export default {
  name: 'XbjsPickTicket',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['pick_status'],
  cruds() {
    return CRUD({
      title: 'xbjsPickTicket',
      url: 'api/pickOut',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudPickOut }, optShow: {
        add: false,
        edit: false,
        del: false,
        reset: false,
        download: false
      }
    })
  },
  data() {
    return {
      permission: {
        xbjsButton: ['admin', 'xbjsPickTicket:xbjsButton']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'orderNo', display_name: '单号' },
        { key: 'plateNo', display_name: '车牌号' }
      ],
      //未绑定出库单的的数据
      taskLogListData: [],
      selectionTaskLogListData: [],
      //新政开关
      saveOnOff: false,
      loading_add:false,
      pickOutButton: {
        //按钮 1、新增功能 2、出库确认
        button: 0,
        //选择的tasklog的id
        taskLogIdS: [],
        //选着的PickOut的id
        pickOutIdS: [],
        //出库单维护的数据
        pickOut: {
          id: null,
          orderNo: null,
          plateNo: null,
          orderQty: 0,
          dispatchQty: 0,
          enabled: true,
          description: null,
          createBy: null,
          updateBy: null,
          createTime: null,
          updateTime: null,
          status: 'OPEN'
        },
        //品番
        itemCode: null,
        //起始点位
        srcPointCode: null,
        //目标容器
        dstStockCode: null,
        pickOutId: null
      },
      //出库确认的数据
      taskLogListDataS: [],
      selectionTaskLogListDataS: [],
      onOff: false
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    initData() {
      //TaskLog未绑定出库单的的数据
      crudPickOut.queryTaskLog(this.pickOutButton).then(res => {
        this.taskLogListData = res
      })
    },
    saveM() {
      this.$router.push({ path: '/business-pick/pickOutSave', query: {} })
    },
    //新增数据
    savePickOut() {
      let data_ = this.pickOutButton
      data_.button = 1
      // if (this.selectionTaskLogListData.length > 0) {
      //选择的tasklog
      data_.taskLogIdS = []
      for (const datum of this.selectionTaskLogListData) {
        data_.taskLogIdS.push(datum.id)
      }
      // } else {
      //   this.$message.error('你未选着数据')
      // }
      crudPickOut.operationAll(data_).then(res => {
        data_ = {};
        this.saveOnOff = false
        this.pickOutButton.pickOut = defaultForm
        this.crud.toQuery()
        this.$message.success(res)
        this.selectData()
      })
      data_ = {};
    },
    //出库确认
    confirmOutbound(data) {
      if (data.length === 0) {
        this.$message.error('你未选着数据')
      }
      let data_ = this.pickOutButton
      if (data.length === 1){//单条数据的操着
        let data_ = this.pickOutButton
        data_.itemCode = null
        data_.srcPointCode = null
        data_.dstStockCode = null
        data_.pickOutId = data[0].id
        crudPickOut.queryTaskLog(data_).then(res => {
          data_ = {};
          this.taskLogListDataS = res
          this.onOff = true
        })
      }
      if (data.length>1){
        //选择的数据 getPickOutIdS
        data_.button = 2
        data_.pickOutIdS = []
        for (const datum of data) {
          data_.pickOutIdS.push(datum.id);
        }
        crudPickOut.operationAll(data_).then(res=>{
          data_ = {};
          this.crud.toQuery();
          this.$message.success(res)
          this.selectData();
        })
      }
      data_ = {};
    },
    confirmOutboundM(data) {
      let data_ = this.pickOutButton
      data_.button = 2
      data_.pickOutIdS = []
      data_.taskLogIdS = []
      for (const datum of data) {
        data_.pickOutIdS.push(datum.id)
      }
      for (const selectionTaskLogListData of this.selectionTaskLogListDataS) {
        data_.taskLogIdS.push(selectionTaskLogListData.id)
      }
      crudPickOut.operationAll(data_).then(res => {
        data_ = {};
        this.onOff = false
        this.crud.toQuery()
        this.$message.success(res)
        this.selectData()
      })
      data_ = {};
    },
    //查看明细
    pickOutRouter(data) {
      this.$router.push({ path: '/business-pick/pickOutSave', query: { id: data.id } })
    },
    //日期格式化
    getFormatDate(data){
      return  formatDate(data);
    },
    //获取库区数据
    getAreaOptionsAll() {
      getAreas().then(res => {
        this.areaOptions = res
      })
    },
    xbjsButtonM(data){
      this.loading_add=true;
      let ids = [];
      for (const obj of data) {
        ids.push(obj.id)
      }
      xbjsOut(ids).then(res=>{
        this.crud.toQuery()
        this.loading_add=false;
        this.$message.success("接收确认成功")
      },e=>{
        this.loading_add=false;
      })
    }
  }
}
</script>

<style scoped>

</style>
