<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select v-model="query.shAreaCode" clearable filterable placeholder="工位" value-key="id" class="filter-item"
                   @keyup.enter.native="crud.toQuery"
        >
          <el-option
            style="width: 180px"
            v-for="item in areaOptions"
            :key="item.id"
            :label="item.code"
            :value="item.code"
          >
          </el-option>
        </el-select>
        <el-input v-model="query.orderNo" clearable placeholder="单号" style="width: 155px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"
        />
        <el-input v-model="query.plateNo" clearable placeholder="车牌号" style="width: 155px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"
        />

        <el-select v-model="query.status" clearable placeholder="状态" value-key="id" style="width: 120px" class="filter-item" filterable
                   @keyup.enter.native="crud.toQuery"
        >
          <el-option
            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="primary"
          icon="el-icon-plus"
          size="mini"
          v-permission="permission.saveM"
          @click="saveM"
        >
          新增
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          size="mini"
          :disabled="!(crud.selections.length>0)"
          @click="confirmOutbound(crud.selections)"
          v-permission="permission.confirmOutbound"
        >
          出库确认
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="crud.selections.length != 1"
          @click="showReport(crud.selections[0])"
          v-permission="permission.showReport"
          icon="el-icon-document-add"
        >
          打印出库单
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="crud.selections.length != 1"
          @click="showReporthc(crud.selections[0])"
          v-permission="permission.showReporthc"
          icon="el-icon-document-add"
        >
          打印回传单
        </el-button>
      </crudOperation>
      <!--   新增   -->
      <el-dialog :close-on-click-modal="false" :visible.sync="saveOnOff"
                 :title="'新增'" width="90%" top="20px"
      >
        <el-form ref="form" :model="pickOutButton.pickOut" :rules="rules" size="small" label-width="80px">
          <el-form-item label="车牌号">
            <el-input v-model="pickOutButton.pickOut.plateNo" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="pickOutButton.pickOut.description" :rows="3" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-input v-model="pickOutButton.itemCode" clearable style="width: 180px" class="filter-item"
                    placeholder="品番" @keyup.enter.native="selectData"
          ></el-input>
          <el-input v-model="pickOutButton.srcPointCode" clearable style="width: 180px" class="filter-item"
                    placeholder="起始点位" @keyup.enter.native="selectData"
          ></el-input>
          <el-input v-model="pickOutButton.dstStockCode" clearable style="width: 180px" class="filter-item"
                    placeholder="目标托盘号" @keyup.enter.native="selectData"
          ></el-input>
          <!--表格渲染-->
          <el-table ref="table" :data="taskLogListData" height="200px" size="small" style="width: 100%;" border
          >
            <el-table-column type="selection" width="55"/>
            <el-table-column prop="item.code" label="品番"/>
            <el-table-column prop="item.name" label="品名"/>
<!--            <el-table-column prop="itemKey.propD1" label="生产日期" width="120px"/>-->
            <el-table-column prop="itemKey.propC1" label="批次号"/>
            <el-table-column prop="moveQty" 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="srcStockCode" label="源容器编码"/>-->
            <el-table-column prop="createBy" label="创建人"/>
            <el-table-column prop="createTime" label="创建时间"/>
          </el-table>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="saveOnOff = false">取消</el-button>
          <el-button type="primary" @click="savePickOut()">确认</el-button>
        </div>
      </el-dialog>
      <!--  出库确认    -->
      <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%;" 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="orderNo" label="单号" width="120"/>
        <el-table-column prop="gcCode" label="接收工厂" width="100"/>
        <el-table-column prop="plateNo" label="车牌号"/>
        <el-table-column prop="orderQty" label="订单数量"/>
        <el-table-column prop="dispatchQty" label="出库数量"/>
        <el-table-column prop="bhNo" label="备货单号"  width="120"/>
        <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="description" label="备注"/>
        <el-table-column prop="createBy" label="创建人"/>
        <el-table-column prop="createTime" label="创建时间" width="150"/>
        <el-table-column v-if="checkPer(['admin','pickOut:edit','pickOut:del'])" label="操作" width="180px"
                         align="center"
        >
          <template slot-scope="scope">
            <el-button type="primary" @click="pickOutRouter(scope.row)"
                       style="float: right" size="mini"
            >明细
            </el-button>
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudPickOut, { operationAll } 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 {getAreas} from "@/api/area";
import {mapGetters} from "vuex";
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: 'PickOut',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['pick_status'],
  cruds() {
    return CRUD({
      title: 'pickOut',
      url: 'api/pickOut',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudPickOut }, optShow: {
        add: false,
        edit: false,
        del: true,
        reset: true,
        download: false
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'pickOut:add'],
        edit: ['admin', 'pickOut:edit'],
        del: ['admin', 'pickOut:del'],
        saveM: ['admin', 'pickOut:saveM'],
        confirmOutbound: ['admin', 'pickOut:confirmOutbound'],
        showReport: ['admin', 'pickOut:showReport'],
        showReporthc: ['admin', 'pickOut:showReporthc']
      },
      rules: {},
      queryTypeOptions: [
        { key: 'orderNo', display_name: '单号' },
        { key: 'plateNo', display_name: '车牌号' }
      ],
      //未绑定出库单的的数据
      taskLogListData: [],
      selectionTaskLogListData: [],
      //新政开关
      saveOnOff: 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: [],
      areaOptions: [],
      selectionTaskLogListDataS: [],
      onOff: false
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  mounted() {
    this.getAreaOptionsAll();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    //获取库区数据
    getAreaOptionsAll() {
      getAreas().then(res => {
        this.areaOptions = 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);
    },
    showReport(data){
      this.$router.push({path:'/business-pick/business-bom/ckd_report',query:{tableID:'出库报表',parm: "code="+data.orderNo+"&username="+this.user.username}})
    },
    showReporthc(data){
      this.$router.push({path:'/business-pick/business-bom/ckhcd_report',query:{tableID:'出库回传单',parm: "code="+data.orderNo+"&username="+this.user.username}})
    }
  }
}
</script>

<style scoped>

</style>
