<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)">
      <!--      查询操作-->
      <el-form ref="form" :inline="true" :model="form" label-width="70px">
        <el-select v-model="query.gcCode" clearable placeholder="接收工厂" style="width: 185px;" class="filter-item"
                   @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="baseStatus in dict.factory"
            :key="baseStatus.id"
            :label="baseStatus.label"
            :value="baseStatus.value"
          />

        </el-select>
        <el-input v-model="query.code" clearable placeholder="备货单号" style="width: 120px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"/>

        <el-select v-model="query.rkAreaCode"  value-key="id" filterable placeholder="指示纳所" style="width: 140px;" class="filter-item"  @keyup.enter.native="crud.toQuery" clearable>
          <el-option
            v-for="item in rkAreaOptions"
            :key="item.id"
            :label="item.name"
            :value="item.code"
          >
            <span style="float: left;width: 60px" >{{ item.code }}</span>
            <span style="width: 200px; color: #8492a6; font-size: 13px">{{ item.name }}</span>
          </el-option>
        </el-select>
        <el-select v-model="query.ckAreaCode"  value-key="id" filterable placeholder="出库库区" style="width: 140px;" class="filter-item"  @keyup.enter.native="crud.toQuery" clearable>
          <el-option
            v-for="item in areaOptions"
            :key="item.id"
            :label="item.code"
            :value="item.code"
          >
            <span style="float: left;width: 60px" >{{ item.code }}</span>
            <span style="width: 200px; color: #8492a6; font-size: 13px">{{ item.name }}</span>
          </el-option>
        </el-select>

        <label class="el-form-item-label" >状态
        <el-select v-model="query.status" clearable placeholder="状态" style="width: 100px;"  value-key="id" class="filter-item"
                   @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>
        </label>
        <el-form-item label="品番" label-width="45px">
          <el-input v-model="query.itemCode" clearable placeholder="请输入部品品番" style="width: 100px;"
                    class="filter-item"
                    @keyup.enter.native="crud.toQuery"/>
        </el-form-item>

        <el-form-item label="品名" label-width="45px">
          <el-input v-model="query.itemName" clearable placeholder="请输入部品品名" style="width: 100px;"
                    class="filter-item"
                    @keyup.enter.native="crud.toQuery"/>
        </el-form-item>
        <el-form-item label="备货需求不足>=" label-width="120px">
          <el-input v-model="query.pureXqBz" clearable placeholder="请输入数值" style="width: 100px;"
                    class="filter-item"
                    @keyup.enter.native="crud.toQuery"/>
        </el-form-item>
        <el-form-item label="在库量>=" label-width="75px">
          <el-input v-model="query.invQty" clearable placeholder="请输入数值" style="width: 100px;"
                    class="filter-item"
                    @keyup.enter.native="crud.toQuery"/>
        </el-form-item>
        <label class="el-form-item-label">订单日期
        <date-range-picker v-model="query.createTime" class="date-item" style="width: 100px"/>
        </label>
        <!--        搜索-->
        <rrOperation :crud="crud"/>
        <!--        重置-->
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetQuery">重置
        </el-button>
      </el-form>
    </div>
      <!--      业务操作-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-button size="mini" type="success" slot="right" class="filter-item"  v-permission="permission.autoBcblBtn"
        @click="autoBcblBtn()"
        >波次补料
        </el-button>
        <el-button
          slot="right"
          v-permission="permission.showSb"
          class="filter-item"
          type="primary"
          size="mini"
          :disabled="crud.selections.length != 1"
          @click="showDesign(crud.selections[0])"
        >
          设变
        </el-button>
      </crudOperation>
      <!--      设变-->
      <el-dialog title="设变" :visible.sync="designOnOff" width="700px" >
        <el-form ref="form" :model="designForm"  size="small" label-width="80px" :inline="true">
          <el-form-item label="品番">
            <el-input v-model="designForm.pickDetail.item_code"  :disabled="true" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="品名">
            <el-input v-model="designForm.pickDetail.item_name"  :disabled="true" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="设变品番">
            <el-input v-model="designForm.pickDetail.sb_item_code"  :disabled="true" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="在库数">
            <el-input v-model="designForm.pickDetail.inv_qty"  :disabled="true"  style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="设变数">
            <el-input v-model="designForm.pickDetail.sbQty"  style="width: 200px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="designOnOff= false">取 消</el-button>
          <el-button type="primary" @click="sbPickdetailBtn(crud.selections)">确 定</el-button>
        </div>
      </el-dialog>
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" @selection-change="crud.selectionChangeHandler" style="width: 100%;"
                height="58vh" border>
        <el-table-column type="selection" width="50"/>
        <el-table-column prop="id" width="60" label="ID"/>
        <el-table-column prop="gc_code" width="120" label="接收工厂"/>
        <el-table-column width="80px" prop="rk_code" label="工位" align="center"/>
        <el-table-column width="150px" prop="rk_name" label="指示纳所" align="center"/>
        <el-table-column prop="code" width="120" label="备货单号"/>
        <el-table-column width="160px" prop="ck_name" label="出库库区" align="center"/>
        <el-table-column prop="pick_status#status" label="状态" align="center">
          <template slot-scope="scope">
            {{ dict.label.pick_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column width="200px" prop="order_date" label="订单日期" align="center"/>
        <el-table-column prop="item_code" label="品番" width="100px" />
        <el-table-column prop="sb_item_code" label="设变品番" width="100px" >
          <template slot-scope="scope">
            <div v-if="scope.row.sb_item_code!=null" style="background: #ff9292">{{ scope.row.sb_item_code }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="item_name" label="品名" width="150px" :show-overflow-tooltip="true"/>
        <el-table-column prop="supplier" label="供应商" width="180px" :show-overflow-tooltip="true"/>
        <!--需求数量-->
        <el-table-column prop="xq_qty" label="需求数" width="60px"/>
        <el-table-column prop="extend_d3" label="收容数" width="60px" align="center"/>
        <!--纯需求-->
        <el-table-column prop="prop_c4" label="纯需求数" width="80px"/>
        <!--纯需求-->
        <el-table-column prop="pure_xq_bz" label="备货需求不足" width="100px"/>
        <el-table-column prop="inv_qty" label="在库量" width="60px" >
          <template slot-scope="scope">
            <div v-if="scope.row.inv_qty>0 && scope.row.pure_xq_bz>0" style="background: #00a0e9">{{ scope.row.inv_qty }}</div>
            <div v-if="!(scope.row.inv_qty>0 && scope.row.pure_xq_bz>0)">{{ scope.row.inv_qty }}</div>
          </template>
        </el-table-column>
        <!--纯需求数量-->
        <el-table-column prop="order_qty" label="备货需求数" width="90px"/>
        <!--未单数量  未出单=纯需求-出单数-->
        <el-table-column prop="wcd_qty" label="未单数" width="60px"/>
        <!--出单数量 分配 出单数=分配时候写入的数量-->
        <el-table-column prop="allocated_qty" label="出单数" width="60px"/>
        <!--备货数量 拣货-->
        <el-table-column prop="picked_qty" label="备货数" width="60px"/>
        <!--在途数量 发运-->
        <el-table-column prop="shipped_qty" label="在途数" width="60px"/>
        <!--补料数量-->
        <el-table-column prop="bc_qty" label="补料数" width="60px"/>
        <!--线边数量-->
        <el-table-column prop="xb_qty" label="线边数" width="60px"/>
        <el-table-column prop="contents" label="加工内容" width="70px"/>
        <el-table-column prop="ac" label="a/c" width="70px"/>
        <!--制造库位 点位-->
        <el-table-column prop="zzkw_code" label="制造库位" width="100px"/>
        <el-table-column :show-overflow-tooltip="true" prop="remark" label="备注"/>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
  </div>
</template>

<script>
import crudPickDetail, {autoBcbl,sbPickdetail} from '@/api/pickDetail'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation.vue'
import crudOperation from '@crud/CRUD.operation.vue'
import udOperation from '@crud/UD.operation.vue'
import pagination from '@crud/Pagination.vue'
import {getItems, getItemsList} from "@/api/item"
import PickTask from "@/views/business-pick/pickDetail/pickTicketTask.vue"
import DateRangePicker from "@/components/DateRangePicker/index.vue";
import { formatDate } from '@/utils/commonUtils'
import {getAreas,queryAreaList} from "@/api/area";
import bcblPickTicket from "@/views/business-pick/pickTicket/bcblPickTicket.vue";
import {batchOrder} from "@/api/automaticPlanning";
import Cookies from "js-cookie";
import {xppBd} from "@/api/xppRecord";

const defaultForm = {
  id: null,
  pickId: null,
  item: null,
  lineNo: null,
  po: null,
  status: 'OPEN',
  orderQty: 0,
  allocatedQty: 0,
  pickedQty: 0,
  shippedQty: 0,
  weight: 0,
  volume: 0,
  remark: null,
  propC1: null,
  propC2: null,
  propC3: null,
  propC4: null,
  propC5: null,
  propC6: null,
  propD1: null,
  propD2: null,
  deptId: null,
  sourceName: null,
  sourceId: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'ViewPickDetailZsc',
  components: {DateRangePicker, PickTask, pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['pick_status','factory'],
  cruds() {
    return CRUD({
      title: '出库明细',
      url: 'api/pickDetail/queryPickDetailZsc',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudPickDetail},
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: false,
        download: true
      },
      queryOnPresenterCreated: true
    })
  },
  data() {
    return {
      items: [],
      permission: {
        autoBcblBtn: ['admin', 'viewPickDetailZsc:autoBcblBtn'],
        showSb: ['admin', 'viewPickDetailZsc:showSb']
      },
      loading_add:false,
      itemListData: [],
      areaOptions: [],
      rkAreaOptions:[],
      designForm: {
        pickDetail:{item_code:null,sb_item_code:null,item_name:null,sbQty:null,inv_qty:null},
      },
      designOnOff:false,
    }

  },
  mounted() {
    //初始化数据
    this.getItem();
    this.getAreaOptionsAll();
    this.getRkAreaOptionsAll();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getItem() {
      getItems({}).then(res => {
        this.items = res.content.map(function (obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    //获取库区数据
    getAreaOptionsAll() {
      queryAreaList({bexb:false,enabled:true}).then(res => {
        this.areaOptions = res
      })
    },
    getRkAreaOptionsAll() {
      queryAreaList({bexb:true,enabled:true}).then(res => {
        this.rkAreaOptions = res
      })
    },
    shuaxin() {
      this.crud.toQuery();
    },
    resetQuery() {
      this.radio3 = '全部';
      this.crud.resetQuery()
    },
    //日期格式化
    getFormatDate(data){
      return  formatDate(data);
    },
    //波次补料
    autoBcblBtn(){
      this.loading_add=true;
      autoBcbl().then(res=>{
        this.$message.success(res);
        this.loading_add=false;
        this.crud.toQuery()
      },e=>{
        this.loading_add=false;
      })

    },
    //弹出设变界面
    showDesign(data){
      this.designForm.pickDetail=data;
      this.designForm.pickDetail.sbQty=data.order_qty-data.bc_qty;
      console.log(data)
      this.designOnOff=true;
    },
    sbPickdetailBtn(data) {
      let data_ = this.designForm
      data_.pdId = data[0].id
      data_.sbItemCode=this.designForm.pickDetail.sb_item_code;
      data_.sbQty=this.designForm.pickDetail.sbQty;
      sbPickdetail(data_).then(res=>{
        this.designOnOff=false;
      })
    },
  }
}
</script>

<style scoped>

</style>
