<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <!-- <el-button type="primary" @click="batchPush">批量入库</el-button> -->
        <!-- 物料入库 -->
        <!-- <el-form v-model="searchFrom"> -->
          <!-- <el-form-item label="物料编号："> -->
            <span>物料编号：</span>
            <el-input style="width:240px;" v-model="searchFrom.prtno" @keyup.enter.native="changeCode"></el-input>
          <!-- </el-form-item> -->
        <!-- </el-form> -->
      </template>
      <template #menu="scope">
        <el-button type="text" @click="warehousing(scope.row)"> 入库</el-button>
      </template>
      <!-- <template #shCode="{ row }">
        {{ row.pushHouse.coStorehouse.shCode }}
      </template>
      <template #location="{ row }">
        {{ row.pushHouse.location }}
      </template> -->
    </avue-crud>
    <el-dialog title="入库" append-to-body v-model="warehouseShow">
      <el-form
        ref="warehouseForm"
        :model="warehouseForm"
        :rules="warehouseRules"
        label-width="80"
      >
        <el-form-item label="入库仓库" prop="shId">
          <el-select v-model="warehouseForm.shId" @change="changeWare">
            <el-option v-for="item in wareList" :key="item.id" :label="item.shName" :value="item.id"></el-option>
            <!-- <el-option label="仓库二" value="2"></el-option> -->
          </el-select>
        </el-form-item>
        <el-form-item label="库位号" prop="slId">
          <el-select v-model="warehouseForm.slId">
            <el-option v-for="item in locationList" :key="item.slId" :label="item.location" :value="item.slId"></el-option>
            <!-- <el-option label="仓库二" value="2"></el-option> -->
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="批次号" prop="piNo">
          <el-input v-model="warehouseForm.piNo"></el-input>
        </el-form-item> -->
        <el-form-item label="数量" prop="pushQuantity">
          <el-input
            v-model="warehouseForm.pushQuantity"
            oninput="value=value.replace(/[^0-9.]/g,'')"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="warehouseShow = false">取 消</el-button>

          <el-button type="primary" @click="submitWarehouse">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </basic-container>
</template>
<script>
import {getList,pushWare,getWareList,getLocationList,getLocationData,addIssue} from "@/api/storeManagement/returnReceiving"
export default {
  data() {
    return {
      loading: false,
      data: [],
      form: {},
      searchFrom:{
        prtno:''
      },
      query:{},
      wareList:[],
      selectionList: [],
      warehouseShow: false,
      warehouseForm: {},
      warehouseRules: {
          shId: [{ required: true, message: "请选择入库仓库", trigger: "blur" }],
          slId: [{ required: true, message: "请选择库位号", trigger: "blur" }],
          pushQuantity:[{ required: true, message: "请填写入库数量", trigger: "blur" }]
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      locationList:[],
      checkGoodsCode:'',
      option: {
        columnSort: true,
        tip: false,
        height: "auto",
        align: "center",
        calcHeight: 32,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: false,
        selection: true,
        viewBtn: false,
        delBtn: false,
        editBtn: false,
        editBtnText: "修改",
        editBtnIcon: " ",
        delBtnIcon: " ",
        addBtn: false,
        labelWidth: 140,
        searchLabelWidth: 120,
        menu: true,
        menuWidth: 100,
        dialogWidth: 600,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: true,
        gridBtn: false,
        searchShowBtn: false,
        showOverflowTooltip: true,
        searchLabelPosition: "left",
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "center",
        gridBtn: false,
        searchMenuPosition: "right",
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",

        column: [
          {
            label: "物料编号",
            prop: "prtno",
            span: 24,
            overflow: true,
            search: false,
            width:160,
            rules: [
              {
                required: true,
                message: "请输入物料编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "材料名称",
            prop: "prtdesc",
            span: 24,
            overflow: true,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入材料名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "规格",
            prop: "prtinfo",
            span: 24,
            overflow: true,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入规格",
                trigger: "blur",
              },
            ],
          },
          {
            label: "生产标识",
            prop: "prtlotno",
            span: 24,
            overflow: true,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入生产标识",
                trigger: "blur",
              },
            ],
          },
          {
            label: "出库单号",
            prop: "invadjCode",
            span: 24,
            overflow: true,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入出库单号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "出库数量",
            prop: "invysqty",
            span: 24,
            overflow: true,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入出库数量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "入库数量",
            prop: "pushQuantity",
            span: 24,
            overflow: true,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入入库数量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "入库库房",
            prop: "shCode",
            span: 24,
            overflow: true,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入入库库房",
                trigger: "blur",
              },
            ],
          },
          {
            label: "入库库位",
            prop: "location",
            span: 24,
            overflow: true,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入入库库位",
                trigger: "blur",
              },
            ],
          },
          {
            label: "单位",
            prop: "prtum",
            span: 24,
            overflow: true,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入单位",
                trigger: "blur",
              },
            ],
          },
          {
            label: '检验编号',
            prop: 'qutno',
            // bind: 'stAccBill.checkNo',
            width: 150,
            search: false,
            sortable: true,
            overHidden: true,
          },
        ],
      },
    };
  },
  mounted() {
    this.getWare()
  },
  methods: {
    searchChange(params, done){
      this.query = params
      this.page.currentPage = 1;
      this.onLoad()
      done()
    },
    searchReset(){
      this.query = {}
      this.onLoad()
    },
    currentChange(cuurrentPage){
      this.page.currentPage = cuurrentPage
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize
    },
    refreshChange(){
      this.onLoad()
    },
    getWare(){
      getWareList().then(res =>{
        this.wareList = res.data.data.records;
      })
    },
    // 切换仓库
    changeWare(val){
      getLocationData({
        shId:val,
        goodsId:this.checkGoodsCode
      }).then(res =>{
        this.locationList = res.data.data.records
      })
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    changeCode(val){
      // console.log('val-------------------------',val)
      this.page.currentPage = 1
      this.onLoad()
    },
    // 单个入库
    warehousing(row) {
      this.warehouseShow = true;
      this.checkGoodsCode = row.goodsId
      this.warehouseForm.id = row.id
      this.warehouseForm.piNo = row.piNo
      this.$refs.warehouseForm.resetFields();
    },
    // 批量入库
    batchPush() {
      if (this.selectionList.length && this.selectionList.length != 0) {
        this.$confirm('确定对数据进行入库操作?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(() => {
            pushWare(this.selectionList).then(res =>{
              if(res.data.code == 200){
                this.$message.success('入库成功')
                this.onLoad()
              }
            })
          })
      } else {
        this.$message.error("请选择需要入库物料");
      }
    },
    // 提交入库
    submitWarehouse() {
      this.$refs.warehouseForm.validate((valid) => {
        if (valid) {
          let params = [
            {
              srId: this.warehouseForm.id,
              pushQuantity: this.warehouseForm.pushQuantity,
              // piNo:this.warehouseForm.piNo,
              pushHouse: {
                  id: this.warehouseForm.slId,
                  shId: this.warehouseForm.shId,
              }
            }
          ]
          addIssue(params).then(res =>{
            if(res.data.code == 200){
              this.$message.success('入库成功')
              this.warehouseShow = false
              this.onLoad()
            }
          })
          console.log('params------------',params)
        }
      });
    },
    onLoad() {
      console.log('prtlotno--------------',this.searchFrom)
      getList({
        current:this.page.currentPage,
        size:this.page.pageSize,
        prtno:this.searchFrom.prtno,
        ...this.query
      }).then(res =>{
        this.data = res.data.data.records
        this.page.total = res.data.data.total
      })
      // this.data = [
      //   {
      //     id: 1,
      //     prtno: "001",
      //     prtdesc: "材料一",
      //     prtinfo: "规格一",
      //     prtlotno: "生产标识",
      //     invadjCode: "",
      //     invysqty: "",
      //     pushQuantity: 20,
      //     pushHouse: {
      //       coStorehouse: {
      //         shCode: "仓库一",
      //       },
      //       location: "00-10",
      //     },
      //     prtum: "个",
      //   },
      //   {
      //     id: 2,
      //     prtno: "001",
      //     prtdesc: "材料一",
      //     prtinfo: "规格一",
      //     prtlotno: "生产标识",
      //     invadjCode: "",
      //     invysqty: "",
      //     pushQuantity: 20,
      //     pushHouse: {
      //       coStorehouse: {
      //         shCode: "仓库一",
      //       },
      //       location: "00-10",
      //     },
      //     prtum: "个",
      //   },
      // ];
      // this.page.total = this.data.length;
    },
  },
};
</script>
<style lang="scss" scoped></style>
