<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" />
      -->
      <!--      <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-select
        v-model="query.bonded"
        clearable
        size="small"
        placeholder="税别"
        style="width: 150px"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in dict.bonded_type"
          :key="item.id"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-input v-model="query.quantity2" 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-checkbox label="库存预警" v-model="query.kcyj"></el-checkbox>
      <AreaSelect  v-model="query.areaName" :return-type="'value'" :return-value-key="'name'" :bexb="true"
                   class="filter-item" placeholder="库区" style="width: 155px;"
      />
      <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>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          size="mini"
          @click="cxjlFromFlag = true"
          icon="el-icon-document-add"
        >
          制造入库
        </el-button>
      </crudOperation>
      <el-dialog
          title="库内移位"
          :visible.sync="ywTF"
          width="30%"
          :close-on-click-modal="false"
      >
        <el-form ref="ywFrom" :rules="ywRules" :model="ywFrom"
                 size="small" label-width="90px"
        >
          <el-form-item label="品番">
            <el-input v-model="ywFrom.item_code" placeholder="品番"
                      :disabled="true"
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="货品名称">
            <el-input v-model="ywFrom.item_name" placeholder="货品名称"
                      :disabled="true"
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="原库位">
            <el-input v-model="ywFrom.point_code" placeholder="原库位" :disabled="true"
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="库存数">
            <el-input v-model="ywFrom.quantity" placeholder="库存数" :disabled="true"
                      style="width: 220px;"
            ></el-input>
          </el-form-item>
          <el-form-item label="目标库位" prop="zPoint">
            <PointSelect :value-key="'id'" v-model="ywFrom.dstPoint" :point_type="'ZZKW'" :immediate="true"/>
          </el-form-item>
          <el-form-item label="移位数量" prop="rmNumber">
            <el-input v-model="ywFrom.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(ywFrom)">确 定</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.item_code" style="width: 300px;" placeholder="品番" :disabled="true"/>
          </el-form-item>
          <el-form-item label="货品名" prop="name">
            <el-input v-model="form.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-dialog title="制造入库" :visible.sync="cxjlFromFlag" width="500px" >
        <el-form ref="cxjlFrom" :model="cxjlFrom" :rules="rules" size="small" label-width="80px">
          <el-form-item label="制造库位" prop="zzkwCode"  :rules=" { required: true, message: '制造库位不能为空', trigger: 'blur' }">
            <BomAccountPointSelect v-model="cxjlFrom.zzkw_code" :value-key="'bom_account_id'" @change="getChangeVule"/>
          </el-form-item>
          <el-form-item label="品番">
            <el-input v-model="cxjlFrom.item_code" />
          </el-form-item>
          <el-form-item label="品名">
            <el-input  v-model="cxjlFrom.item_name"  />
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="cxjlFrom.order_qty" />
          </el-form-item>
          <el-form-item label="库区">
            <el-input  v-model="cxjlFrom.gw_name" />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="cxjlFrom.remark" :rows="3" type="textarea" style="width: 370px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="cxjlFromFlag=false">取消</el-button>
          <el-button type="primary" @click="btnZzjl">入库确认</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_name" label="库区" width="200"/>
        <el-table-column prop="point_code" label="库位号"  align="center"/>
        <el-table-column prop="point_zflx" label="总分类型"  align="center"/>
        <el-table-column prop="item_code" label="品番"/>
        <el-table-column prop="item_name" label="品名"/>
        <el-table-column prop="prop_c2" label="税别"/>
        <el-table-column prop="quantity" label="数量"/>
        <el-table-column prop="inv_min" label="库存下限"/>
        <el-table-column prop="kcyj_qty" label="库存不足数">
          <template slot-scope="scope">
            <div v-if="scope.row.kcyj_qty>0" style="background: #ff9292">{{ scope.row.kcyj_qty }}</div>
            <div v-if="scope.row.kcyj_qty<=0">{{ scope.row.kcyj_qty }}</div>
          </template>
        </el-table-column>


        <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,zzrkInv} 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";
import BomAccountPointSelect from "@/views/generic-component/BomAccountPointSelect.vue";
import {zzjl} from "@/api/pickDetail";
import AreaSelect from "@/views/generic-component/AreaSelect.vue";
import PointSelect from "@/views/generic-component/PointSelect.vue";
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: {PointSelect, AreaSelect, BomAccountPointSelect, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      query: {
        quantity: 0,
        bexb:true,
        pointType:'ZZKW'
      },
      title: '库存信息',
      url: 'api/inventory/queryInventoryZz',
      downurl: 'api/inventory',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudInventory },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: false
      },
    })
  },
  dicts: ['bonded_type'],
  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,
      ywFrom: {
          //inventory: {id},
          dstPoint: {},
          rmNumber: 0,
          id:null,item_code:null,item_name:null,point_code:null,quantity:null

      },
      cxjlFromFlag: false,
      cxjlFrom: { bom_account_id: null,order_qty:0, item_code:null, item_name:null, zzkw_id:null, zzkw_code:null, gw_code:null, gw_name:null, status:null, remark:null,srs:0  }

    }
  },
  mounted() {
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    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.ywFrom = 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.ywFrom.rmNumber=0;
            this.ywFrom.dstPoint= { };
            this.crud.toQuery()
          })
        } else {
          //验证失败
          return false
        }
      })
    },
    downXbBtn(){
      console.log(this.crud.downurl)
      this.crud.downloadLoading = true
      download('api/inventory/downloadZz', this.crud.getQueryParams()).then(result => {
        downloadFile(result, this.crud.title + '数据', 'xlsx')
        this.crud.downloadLoading = false
      }).catch(() => {
        this.crud.downloadLoading = false
      })
    },
    getChangeVule(dben) {
      this.cxjlFrom.order_qty=dben.srs;
      this.cxjlFrom.item_code=dben.item_code;
      this.cxjlFrom.item_name=dben.item_name;
      this.cxjlFrom.gw_code=dben.gw_code;
      this.cxjlFrom.gw_name=dben.gw_name;
      this.cxjlFrom.bom_account_id=dben.bom_account_id
      this.cxjlFrom.item_id=dben.item_id;
      this.cxjlFrom.zzkw_id=dben.zzkw_id;
      this.cxjlFrom.rk_id=dben.rk_id;
    },

    btnZzjl() {
      zzrkInv(this.cxjlFrom).then(res => {
        if (res.status == 200) {
          this.crud.notify(res.message, CRUD.NOTIFICATION_TYPE.SUCCESS)
        }
        this.cxjlFromFlag = false
        this.crud.toQuery()
        this.resetForm()
      }).catch(() => {
      })
    },
    //日期格式化
    getFormatDate(data){
      return  formatDate(data);
    },
  }
}
</script>

<style scoped>

</style>
