<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
<!--      <el-input v-model="query.itemCode" clearable placeholder="物料代码" style="width: 140px;" class="filter-item" @keyup.enter.native="crud.toQuery" />-->
      <el-select v-model="query.itemCode" placeholder="物料代码" style="width: 140px;" class="filter-item">
        <el-option
          @keyup.enter.native="crud.toQuery"
          v-for="item in this.itemListData"
          :key="item.code"
          :label="item.code"
          :value="item.code">
          <span style="float: left">{{ item.code }}</span>
          <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
        </el-option>
      </el-select>
      <el-input v-model="query.itemName" clearable placeholder="物料名称" style="width: 140px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
      <el-select
        v-model="query.agvStatus"
        clearable
        size="small"
        placeholder="任务状态"
        class="filter-item"
        style="width: 150px"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in dict.task_status"
          :key="item.id"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <date-range-picker v-model="query.createTime" class="date-item" style="width: 100px" />
      <label class="el-form-item-label">原点位</label>
      <el-input v-model="query.srcPointCode" clearable placeholder="原点位" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
      <label class="el-form-item-label">目标点位</label>
      <el-input v-model="query.dstPointCode" clearable placeholder="目标点位" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
      <rrOperation :crud="crud" />
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-button
            slot="right"
            class="filter-item"
            type="primary"
            size="mini"
            icon="el-icon-document-add"
            v-show="true"
            @click="ManualReceivingFlag=true"
        >
          人工入库
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog title="人工入库" :visible.sync="ManualReceivingFlag" width="600px">
        <el-form ref="fromBack" :inline="true" :model="fromManualReceiving" :rules="rules" size="mini" label-width="120px">
<!--          <el-form-item label="容器类型" prop="storageType">
            <el-select v-model="fromBack.storageType" value-key="id" filterable placeholder="请选择">
              <el-option
                  v-for="item in storageTypes"
                  :key="item.id"
                  :label="item.name"
                  :value="item.code"
              />
            </el-select>
          </el-form-item>-->
          <el-form-item label="RFID" prop="RFID">
            <el-input v-model="fromManualReceiving.RFID" :rows="3" type="textarea" style="width: 350px;" @change="devan_rfid()"/>
          </el-form-item>
          <el-form-item label="登记单号" prop="relatedBill2">
            <el-input v-model="fromManualReceiving.relatedBill2"  :disabled="true" style="width: 100px;"/>
          </el-form-item>
          <el-form-item label="系统单号" prop="relatedBill1">
            <el-input v-model="fromManualReceiving.relatedBill1"  :disabled="true" style="width: 100px;"/>
          </el-form-item>
          <el-form-item label="sku" prop="sku">
            <el-input v-model="fromManualReceiving.sku"  :disabled="true" style="width: 330px;"/>
          </el-form-item>
          <el-form-item label="明细行数" prop="lineNo">
            <el-input v-model="fromManualReceiving.lineNos"  :disabled="true" style="width: 100px;"/>
          </el-form-item>
          <el-form-item label="剩余订单数量" prop="orderQty">
            <el-input v-model="fromManualReceiving.orderQty"  :disabled="true" style="width: 100px;"/>
          </el-form-item>
          <el-form-item label="收货数量" prop="receivedQty">
            <el-input v-model="fromManualReceiving.receivedQty"  :disabled="true" style="width: 330px;"/>
          </el-form-item>
          <el-form-item label="目标库位" prop="RFID">
            <el-select v-model="fromManualReceiving.pointId" clearable value-key="id" filterable placeholder="请选择" style="width: 330px;">
              <el-option
                  v-for="item in pointList"
                  :key="item.id"
                  :label="item.area.name+item.name"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="ManualReceivingFlag = false">取 消</el-button>
          <el-button type="primary" @click="manualReceiving()">确 定</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" border height="66vh" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column  prop="xh" type="index" :index="indexMethod" label="序号"/>
        <el-table-column :show-overflow-tooltip="true" prop="itemCode" label="物料编码">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.item.code }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="itemName" label="物料名称">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.item.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="agvStatus" label="任务状态">
          <template slot-scope="scope">
            <span v-if="dict.label.asn_status[scope.row.taskStatus] ==null">{{ scope.row.taskStatus }}</span>
            <span v-if="scope.row.taskStatus !=null">{{ dict.label.asn_status[scope.row.taskStatus] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="srcPointCode" label="原点位">
          <template slot-scope="scope">
            <div v-if="scope.row.srcPoint !=null">{{ scope.row.srcPoint.code }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="dstPointCode" label="目标点位">
          <template slot-scope="scope">
            <div v-if="scope.row.dstPoint !=null">{{ scope.row.dstPoint.code }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="agvTask" label="AGV搬运任务">
          <template slot-scope="scope">
            <div v-if="scope.row.agvTask !=null">{{ scope.row.agvTask.id }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="beSkip" label="是否AGV任务" />
        <el-table-column prop="putCode" :show-overflow-tooltip="true" label="单品流水号" />
        <el-table-column prop="planQty" label="计划数量" />
        <el-table-column prop="moveQty" label="移位数量" />
        <el-table-column prop="itemKey.propC1" label="批次号" />
        <el-table-column prop="itemKey.propC2" label="字符" />
        <el-table-column prop="itemKey.propC3" label="计划单号" />
        <el-table-column prop="itemKey.propC4" label="厂家供货码" />
        <el-table-column prop="itemKey.propC5" label="供货数量" />
        <el-table-column prop="itemKey.propC6" :show-overflow-tooltip="true" label="流水号" />
        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" />
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudTask, { devanRfid } from '@/api/task'
import DonMessage from "@/utils/message";
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 DateRangePicker from "@/components/DateRangePicker/index.vue";
import {getItemsList} from "@/api/item";
import {getPoints} from "@/api/point";
import {getStockTypes} from "@/api/stockType";
import {devan} from "@/api/inventory";

const defaultForm = { id: null, itemId: null,agvTask: null, itemKeyId: null, billCode: null, taskType: null, asnDetailId: null, moveDetailId: null, waveDetailId: null, pickDetailId: null, srcStockId: null, dstStockId: null, srcPoint: null, dstPoint: null, srcStockCode: null, dstStockCode: null, srcPointCode: null, dstPointCode: null, invStatus: null, taskStatus: null, beSkip: null, beBack: null, planQty: null, moveQty: null, putCode: null, invId: null, deptId: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'Task',
  components: {DateRangePicker, pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['task_status','asn_status'],
  cruds() {
    return CRUD({
      query: {taskType: ['FC_RK']},
      title: '收货任务',
      url: 'api/task',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudTask},
        optShow: {
          add: false,
          edit: false,
          del: false,
          reset: true,
          download: true
        },
        queryOnPresenterCreated: true
      }
    )
  },
  data() {
    return {
      ManualReceivingFlag: false,
      permission: {
        add: ['admin', 'task:add'],
        edit: ['admin', 'task:edit'],
        del: ['admin', 'task:del']
      },
      rules: {
        RFID: [
          {required: true, message: '必填', trigger: 'blur'}
        ],
      },
      queryTypeOptions: [
        {key: 'itemCode', display_name: '物料代码'},
        {key: 'itemName', display_name: '物料名称'},
        {key: 'taskStatus', display_name: '任务状态'},
        { key: 'srcPointCode', display_name: '源点位编码' },
        { key: 'dstPointCode', display_name: '目标点位编码' }
      ],
      itemListData: [],
      pointList: [],
      // 人工入库
      fromManualReceiving:{ RFID:null,pointId:null }
    }
  },
  mounted() {
    // 初始化数据
    this.itemDataGet();
    this.getPoints(null);
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    indexMethod(index) {
      return index * 1 + 1;
    },
    // 物料数据
    itemDataGet() {
      getItemsList().then(res => {
        this.itemListData = res;
      }).catch(e => {
        this.$message({
          showClose: true,
          message: '物料加载失败',
          type: 'error'
        });
      })
    },
    // 解析RFID码
    devan_rfid() {
      console.log("解析RFID码")
      devanRfid(this.fromManualReceiving.RFID).then(res => {
        if (res.status === 200) {
          this.fromManualReceiving = res.data
          this.getPoints(res.data.billCode);
        }
      })
    },
    getPoints(billCode) {
      getPoints(billCode+"").then(res => {
        this.pointList= res.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    manualReceiving(){
      crudTask.manualReceiving(this.fromManualReceiving).then(res => {
        this.crud.notify(res.message, res.status)
        this.crud.toQuery()
      })
    }
  }
}
</script>

<style scoped>

</style>

