<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <label class="el-form-item-label" v-show="false">物料编码</label>
      <el-select v-show="false" v-model="query.itemCode" @focus="getItem" value-key="id" filterable multiple
                 placeholder="请选择物料" style="width: 150px;"  @keyup.enter.native="crud.toQuery"
      >
        <el-option
            v-for="item in items"
            :key="item.id"
            :label="item.code"
            :value="item.code"
        />
      </el-select>
      <!--
            <el-input v-model="query.itemCode" clearable placeholder="请输入物料编码" style="width: 140px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
      -->
      <!--      <label class="el-form-item-label">物料编码</label>-->
      <el-input v-model="query.itemCodeOne" clearable placeholder="品番编码" style="width: 150px;"
                @keyup.enter.native="crud.toQuery"
      />
      <!--      <label class="el-form-item-label">物料名称</label>-->
      <el-input v-model="query.itemName" clearable placeholder="品番名称" style="width: 150px;"
                @keyup.enter.native="crud.toQuery"
      />

      <el-input v-model="query.pointCode" clearable placeholder="库位号" style="width: 150px;"
                @keyup.enter.native="crud.toQuery"
      />
      <el-date-picker
          v-model="query.propD1"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期"
      ></el-date-picker>

      <el-select v-model="query.areaNames"  value-key="id" clearable placeholder="指示纳所"  style="width: 180px" filterable multiple reserve-keyword="true" >
        <el-option
            v-for="item in areaOptions"
            :key="item.id"
            :label="item.name"
            :value="item.name"
        >
          <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>
      <rrOperation :crud="crud"/>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-button size="mini" slot="right" class="filter-item"  icon="el-icon-download" type="warning"
                   @click="downXbBtn()"
        >导出
        </el-button>
      </crudOperation>
      <el-dialog
          title="库内移位"
          :visible.sync="ywTF"
          width="30%"
          :close-on-click-modal="false"
      >
        <el-form ref="ywFrom" :rules="ywRules" :model="inventoryButton.inventoryYW"
                 size="small" label-width="90px"
        >
          <el-form-item label="品番">
            <el-input v-model="inventoryButton.inventoryYW.inventory.itemKey.item.code" placeholder="品番"
                      :disabled="true"
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="货品名称">
            <el-input v-model="inventoryButton.inventoryYW.inventory.itemKey.item.name" placeholder="货品名称"
                      :disabled="true"
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="原库位">
            <el-input v-model="inventoryButton.inventoryYW.inventory.point.name" placeholder="原库位" :disabled="true"
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="库存数">
            <el-input v-model="inventoryButton.inventoryYW.inventory.quantity" placeholder="库存数" :disabled="true"
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="目标点位" prop="dstPoint">
            <el-select v-model="inventoryButton.inventoryYW.dstPoint" value-key="id" placeholder="目标点位"
                       style="width: 220px;" clearable filterable
            >
              <el-option
                  v-for="item in pointOptions"
                  :key="item.code"
                  :label="item.code"
                  :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="移位数量" prop="rmNumber">
            <el-input v-model="inventoryButton.inventoryYW.rmNumber" placeholder="库存数" clearable
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="ywTF = false">取 消</el-button>
    <el-button type="primary" @click="planPickSaveM(inventoryButton)">确 定</el-button>
  </span>
      </el-dialog>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="'库存修改'" width="520px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="110px">
          <el-form-item label="品番">
            <el-input v-model="form.itemKey.item.code" style="width: 300px;" placeholder="品番" :disabled="true"/>
          </el-form-item>
          <el-form-item label="货品名" prop="name">
            <el-input v-model="form.itemKey.item.name" style="width: 300px;" placeholder="货品名" clearable/>
          </el-form-item>
          <el-form-item label="调整后的数量" prop="quantity">
            <el-input v-model="form.quantity" style="width: 300px;" placeholder="调整后的数量" clearable/>
          </el-form-item>
          <el-form-item label="调整原因" prop="description">
            <el-input v-model="form.description" :rows="3" type="textarea" style="width: 300px;"
                      placeholder="调整原因"
            />
          </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" :height="crud.tableHeight"  border
          show-summary
          v-loading="crud.loading"
          :data="crud.data"
          :header-cell-style="{background:'#e0f5ec'}"
          size="small"
          style="width: 100%;"
          @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="50"/>
        <el-table-column prop="id" label="序号"  align="center"/>
        <!--<el-table-column prop="area.code" label="库区" width="80"/>-->
        <el-table-column prop="area.name" label="库区" width="200"/>
        <el-table-column prop="point.code" label="库位号"  align="center"/>
        <el-table-column prop="itemKey.item.code" label="品番"/>
        <el-table-column prop="itemKey.item.name" label="品番名称"/>
        <el-table-column prop="quantity" label="数量"/>
        <!--<el-table-column prop="zzkw.code" label="制造库位" align="center"/>
        <el-table-column prop="area.pointCode" label="缓存库位" align="center"/>-->
        <el-table-column prop="itemKey.propC2" label="税别"/>
        <el-table-column  v-if="checkPer(['admin','fileManagement:edit','fileManagement:del'])" label="操作"
                          width="150px" align="center" fixed="right"
        >
          <template slot-scope="scope">
            <el-button
                class="filter-item"
                type="primary"
                size="mini"
                style="float: right"
                @click="ywFTM(scope.row)"
            >
              移位
            </el-button>
            <udOperation
                :data="scope.row"
                :permission="permission"
                :show-dle="false"
            >
            </udOperation>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudInventory, {downXb, knywInventory} from '@/api/inventory'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import { getItems } from '@/api/item'
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 { queryPointList } from '@/api/point'
import { manualGeneration } from '@/api/productionPlan'
import { formatDate } from '@/utils/commonUtils'
import { getAreas ,queryAreaList} from '@/api/area'
import {download} from "@/api/data";
import {downloadFile} from "@/utils";
const defaultForm = {
  id: null,
  billCode: null,
  itemKey: { item: { code: null } },
  point: null,
  pointCode: null,
  stock: null,
  stockCode: 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: 'InvZz',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      query: {
        quantity: 0,
        bexb:true,
        pointType:'ZZKW'
      },
      areaOptions: [],
      title: '库存信息',
      url: 'api/inventory',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudInventory },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: false
      },
    })
  },
  data() {
    return {
      points: [],
      stocks: [],
      items: [],
      permission: {
        add: ['admin', 'inventory:add'],
        edit: ['admin', 'inventory:edit'],
        del: ['admin', 'inventory:del']
      },
      rules: {
        quantity: { required: true, message: '调整后必填', trigger: 'blur' },
        description: { required: true, message: '调整原因必填', trigger: 'blur' }
      },
      ywRules:{
        dstPoint: { required: true, message: '目标点位必填', trigger: 'blur' },
        rmNumber: { required: true, message: '移位数量必填', trigger: 'blur' },
      },
      ywTF: false,
      inventoryButton: {
        inventoryYW: {
          inventory: { itemKey: { item: {} }, point: {} },
          dstPoint: {},
          rmNumber: 0
        }
      },
      pointOptions: []
    }
  },
  mounted() {
    this.getAreaOptionsAll()
    this.infoData()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    infoData(){
      queryPointList({}).then(res=>{
        this.pointOptions = res;
      })
    },
    getItem() {
      getItems({}).then(res => {
        this.items = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    deleteDataM(id) {//删除数据的方法
      let ids = []
      ids.push(id)
      crudInventory.del(ids).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.crud.toQuery()
      }).catch(err => {
        this.$message({
          message: '删除失败',
          type: 'error'
        })
      })
      this.crud.toQuery()
    },
    ywFTM(data){
      this.inventoryButton.inventoryYW.inventory = data;
      this.ywTF=true;
    },
    planPickSaveM(data){
      this.$refs['ywFrom'].validate((valid) => {
        if (valid) {
          knywInventory(data).then(res=>{
            this.ywTF = false;
            this.$message.success(res)
            this.inventoryButton.inventoryYW.rmNumber=0;
            this.inventoryButton.inventoryYW.dstPoint= { };
            this.crud.toQuery()
          })
        } else {
          //验证失败
          return false
        }
      })
    },
    downXbBtn(){
      console.log(123)
      this.crud.downloadLoading = true
      download(this.crud.url + '/downloadXb', this.crud.getQueryParams()).then(result => {
        downloadFile(result, this.crud.title + '数据', 'xlsx')
        this.crud.downloadLoading = false
      }).catch(() => {
        this.crud.downloadLoading = false
      })
    },
    //获取库区数据
    getAreaOptionsAll() {
      queryAreaList({bexb:true,enabled:true}).then(res => {
        this.areaOptions = res
      })
    },
    //日期格式化
    getFormatDate(data){
      return  formatDate(data);
    },
  }
}
</script>

<style scoped>

</style>
