<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <el-input
        v-model="query.itemCodeOne"
        clearable
        placeholder="物料编码"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
      />

      <el-input
        v-model="query.propC1"
        clearable
        placeholder="批次号"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
      />

      <el-input
        v-model="query.pointCode"
        clearable
        placeholder="点位号"
        style="width: 140px;"
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
      />
      <el-select v-model="query.areaCode" style="width: 120px;" clearable placeholder="区域"
                 class="filter-item" @change="crud.toQuery">
        <el-option
            v-for="area in areaList"
            :key="area.id"
            :label="area.name"
            :value="area.code"
        />
      </el-select>
      <el-input
          v-model="query.prodMtrl"
          clearable
          placeholder="成品物料码"
          style="width: 120px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
      />
      <el-input
          v-model="query.owner"
          clearable
          placeholder="货主"
          style="width: 100px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
      />
      <el-input
          v-model="query.itemKeyPropC7"
          clearable
          placeholder="标签码"
          style="width: 300px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
      />
      <rrOperation :crud="crud" />

      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :table-key="this.$options.name">
        <el-button
            v-if="ButReplaceInvFlag"
            slot="right"
            class="filter-item"
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="crud.selections.length !== 1 || this.crud.selections[0].orderQty<=this.crud.selections[0].allocatedQty"
            v-on:click="replaceInv()"
        >
          替换分配库存
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <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" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="点位" prop="point">
            <el-select v-model="form.point" filterable placeholder="请选点位" value-key="id" style="width: 200px;">
              <el-option
                v-for="point in points"
                :key="point.id"
                :label="point.code"
                :value="point"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="form.quantity" :disabled="false" style="width: 100px;" />
          </el-form-item>
          <el-form-item label="分配数">
            <el-input v-model="form.queuedQty" :disabled="true" style="width: 100px;" />
          </el-form-item>
          <el-form-item label="RFID">
            <el-input v-model="form.itemKey.propC7" :disabled="false" :rows="3" type="textarea" 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-dialog title="拆托" :visible.sync="FromDevanningFlag" width="800px">
        <el-form ref="Devanning" :model="fromDevanning" :rules="rules" size="small" 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="原流水号集合" prop="textPropC6">
            <el-input v-model="fromDevanning.textPropC6" :rows="3" type="textarea" style="width: 270px;" />
          </el-form-item>
          <el-form-item label="流水号集合1" prop="text1PropC6">
            <el-input v-model="fromDevanning.text1PropC6" :rows="3" type="textarea" style="width: 270px;" @keyup.native="devan_submit()" />
            <el-input v-model="devanPropC6.endPropC6" style="width: 270px" placeholder="终止流水号">
              <el-input slot="prepend" v-model="devanPropC6.startPropC6" style="width: 90px" placeholder="起始流水号" />
              <el-button slot="append" icon="el-icon-search" @click="devan_propC6()" />
            </el-input>
          </el-form-item>
          <el-form-item label="流水号集合2" prop="text2PropC6">
            <el-input v-model="fromDevanning.text2PropC6" :rows="3" type="textarea" style="width: 270px;" />
          </el-form-item>
          <!--          <el-form-item label="RFID" prop="RFID">
            <el-input v-model="fromDevanning.RFID" :rows="3" type="textarea" style="width: 270px;"/>
          </el-form-item>
          <el-form-item label="目标库位" prop="RFID">
            <el-select v-model="fromDevanning.pointId" clearable value-key="id" filterable placeholder="请选择" style="width: 270px;">
              <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="fromDevanningFlag = false">取 消</el-button>
          <el-button type="primary" @click="devanning()">确 定</el-button>
        </div>
      </el-dialog>
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        border
        size="small"
        style="width: 100%;"
        height="70vh"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="30" />
        <el-table-column prop="id" label="库存ID" />
        <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="prodMtrl" label="成品物料码" />
        <el-table-column prop="owner" label="货主" />

        <el-table-column :show-overflow-tooltip="true" prop="propC1" label="批次号">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.propC1 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="propC2" label="字符">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.propC2 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="propC3" label="系统单号">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.propC3 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="propC4" label="厂家供货码">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.propC4 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="propC5" label="供货数量">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.propC5 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="propC6" label="流水号">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.propC6 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="propC7" label="RFID码">
          <template slot-scope="scope">
            <div>{{ scope.row.itemKey.propC7 }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="areaName" label="区域">
          <template slot-scope="scope">
            <span v-if="scope.row.point != null">{{ scope.row.point.area.name }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="pointCode" label="点位号">
          <template slot-scope="scope">
            <span v-if="scope.row.point != null">{{ scope.row.point.code }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="storageType" label="容器类型">
          <template slot-scope="scope">
            <span v-if="scope.row.point != null">{{ scope.row.point.storageType }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="quantity" label="数量" />
        <el-table-column prop="queuedQty" label="占用数" />
        <el-table-column prop="orderNumber" label="登记单号">
          <template slot-scope="scope">
            <div>{{ scope.row.billCode }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
<!--        <el-table-column v-if="checkPer(['admin','inventory:ckxq'])" fixed="right" label="操作" style="width: 120px" align="center">
          <template slot-scope="scope">
            <el-button
                v-permission="permission.ckxq"
                class="filter-item"
                type="primary"
                size="mini"
                icon="el-icon-document-add"
                @click="jumpAsnDetail(scope.row.relatedBill1)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination />
      </div>
    </div>
  </div>
</template>

<script>
import crudInventory, { getItemKey, devan, devanPropC6, devanning } from '@/api/inventory'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import { getItems } from '@/api/item'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {queryPickTask} from "@/api/task";
import {getPoints} from "@/api/point";
import {getAres} from "@/api/area";

const defaultForm = {
  id: null,
  billCode: null,
  itemKey: { item: { id:null },propC7:null },
  point: null,
  stock: null,
  owner: null,
  prodMtrl: null,
  bigStockId: null,
  quantity: null,
  queuedQty: null,
  weight: null,
  volume: null,
  description: null,
  beLock: null,
  beReject: null,
  status: null,
  storageDate: null,
  deptId: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  itemCodeOne: null
}
export default {
  name: 'Inventory',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      query: {
        quantity: 0
      },
      title: '库存信息',
      url: 'api/inventory',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudInventory },
      optShow: {
        add: false,
        edit: true,
        del: true,
        reset: false,
        download: true
      }
    })
  },
  data() {
    return {
      // 替换分配库存
      ButReplaceInvFlag: false,
      points: [],
      stocks: [],
      areaList: [],
      items: [],
      permission: {
        add: ['admin', 'inventory:add'],
        edit: ['admin', 'inventory:edit'],
        del: ['admin', 'inventory:del'],
        download: ['admin', 'inventory:download'],
        ctDeav: ['admin', 'inventory:ctDeav'],
        ckxq: ['admin', 'inventory:ckxq']
      },
      FromDevanningFlag: false,
      devanPropC6: { startPropC6: null, endPropC6: null },
      fromDevanning: { invIds: [], textPropC6: null, text1PropC6: null, text2PropC6: null, startPointCode: null, endPointCode: null },
      rules: {}
    }
  },
  mounted() {
    this.getPoints()
    this.getArea()
  },
  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
        })
      })
    },
    getArea() {
      getAres({size: 50}).then(res => {
        this.areaList = res.content.map(function (obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    getPoints() {
      getPoints().then(res => {
        this.points= res.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    LoadDevanning() {
      this.FromDevanningFlag = true
      this.fromDevanning.invIds = []
      for (let i = 0; i < this.crud.selections.length; i++) {
        this.fromDevanning.invIds.push(this.crud.selections[i].id)
      }
      getItemKey(this.fromDevanning.invIds).then(res => {
        if (res.status === 200) {
          console.log('值：' + res.data)
          this.fromDevanning.textPropC6 = res.data
          this.fromDevanning.text1PropC6 = ''
          this.fromDevanning.text2PropC6 = ''
        } else {
          this.crud.notify(res.message, res.status)
        }
      })
    },
    // 拆托-查询流水号
    devan_propC6() {
      devanPropC6(this.devanPropC6).then(res => {
        if (res.status === 200) {
          console.log('值：' + res.data)
          this.fromDevanning.text1PropC6 = res.data.text1PropC6
        } else {
          this.crud.notify(res.message, res.status)
        }
        this.devan_submit()
      })
    },
    // 拆托
    devan_submit: function() {
      devan(this.fromDevanning).then(res => {
        if (res.status === 200) {
          this.fromDevanning.text2PropC6 = res.data.text2PropC6
        } else {
          this.crud.notify(res.message, res.status)
        }
      })
    },
    devanning() {
      devanning(this.fromDevanning).then(res => {
        this.crud.notify(res.message, res.status)
        this.FromDevanningFlag = false
        this.crud.toQuery()
      })
    },
    // 替换分配库存
    replaceInv:function () {
      this.$emit('replaceTaskInv',this.crud.selections[0].id)
    },
    // 替换分配库存，加载页面
    queryInventory(itemId) {
      this.ButReplaceInvFlag=true;
      this.query.queuedQty=0;
      this.query.itemId=itemId;
      this.crud.toQuery()
    },
    deleteDataM(id) { // 删除数据的方法
      const ids = []
      ids.push(id)
      crudInventory.del(ids).then(res => {
        this.crud.notify('删除成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      }).catch(err => {
        this.crud.notify('删除失败！', CRUD.NOTIFICATION_TYPE.ERROR)
      })
      this.crud.toQuery()
    },
    resetQuery() {
      this.crud.resetQuery()
    }
  }
}
</script>

<style>

</style>
