<template>
  <div class="fullHeight whiteBlock">
    <div style="padding: 15px 15px 0">
      <p class="pageTitle">进场登记</p>
      <el-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        size="medium"
      >
        <el-form-item label="单号">
          <el-input v-model="searchForm.number" placeholder="请输入"></el-input>
        </el-form-item>
        <!-- <el-form-item label="货号">
          <el-input v-model="searchForm.stuffNumber" placeholder="请输入"></el-input>
        </el-form-item> -->
        <el-form-item label="仓库">
          <el-input
            v-model="searchForm.warehouse"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <el-input
            v-model="searchForm.department"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-input
            v-model="searchForm.category"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="经手人">
          <el-input
            v-model="searchForm.handler"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="助查码">
          <el-input v-model="searchForm.lookupCode" placeholder="请输入"></el-input>
        </el-form-item> -->

        <el-form-item label="日期">
          <el-date-picker
            v-model="timeValue"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="getTime"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供货单位">
          <el-input v-model="searchForm.vendor" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="getList">{{
            $t('message.energyManage.waybill.query')
          }}</el-button>
          <el-button type="warning" plain @click="refresh">{{
            $t('message.deviceManage.refresh')
          }}</el-button>
          <el-button type="primary" size="medium" @click="add">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_wrap whiteBlock">
      <el-table class="tables" :data="List" @row-click="showDetailFn">
        <el-table-column
          prop="serialNumber"
          label="编号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="number"
          align="center"
          label="单号"
        ></el-table-column>
        <el-table-column
          prop="date"
          align="center"
          label="日期"
        ></el-table-column>
        <el-table-column
          prop="warehouse"
          align="center"
          label="仓库"
        ></el-table-column>
        <el-table-column
          prop="direction"
          align="center"
          label="方向"
        ></el-table-column>
        <el-table-column
          prop="category"
          align="center"
          label="类别"
        ></el-table-column>
        <el-table-column
          prop="department"
          align="center"
          label="部门"
        ></el-table-column>
        <el-table-column
          prop="vendor"
          align="center"
          label="供应商"
        ></el-table-column>
        <el-table-column
          prop="handler"
          align="center"
          label="经手人"
        ></el-table-column>
        <el-table-column
          prop="lotNumber"
          align="center"
          label="批号"
        ></el-table-column>
        <el-table-column
          prop="singleHanded"
          align="center"
          label="制单人"
        ></el-table-column>
        <el-table-column
        width="110"
          prop="createTime"
          align="center"
          label="制单时间"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <div class="tableBtns" style="margin-left: -14px !important">
              <div @click.stop="edit(scope.row)" class="operationText" style="margin-right: 10px !important;margin-left: 20px !important;">
                <img
                  src="@/assets/images/icon-edit.png"
                  width="15px"
                  height="15px"
                />
                <span>编辑</span>
              </div>
              <div @click.stop="deleteDev(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-delete.png"
                  width="15px"
                  height="15px"
                />
                <span>删除</span>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagerBox"
        @size-change="SizeChange"
        @current-change="CurrentChange"
        :current-page="pagInfo.pageNo"
        :page-sizes="$store.state.PAGESIZRS"
        :page-size="pagInfo.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="Number(pagInfo.total)"
        background
      ></el-pagination>
    </div>
    <!--新增入库单信息弹框-->
    <el-dialog
      :modal-append-to-body="false"
      :title="title"
      :visible.sync="dialogShow"
      width="1167px"
    >
      <p>入库单信息</p>
      <div class="dialog_content">
        <el-form
          style="margin: 0 28px"
          size="medium"
          :model="addForm"
          ref="addForm"
          :rules="addFormules"
          :inline="true"
        >
          <el-form-item label="日期" prop="createTime">
            <el-date-picker
              v-model="addForm.date"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="仓库" prop="warehouse">
            <el-input
              v-model="addForm.warehouse"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="category">
            <el-input
              v-model="addForm.category"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="方向" prop="direction" style="margin-left: 15px">
            <el-input
              v-model="addForm.direction"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="编号"
            prop="serialNumber"
            style="margin-left: 10px"
          >
            <el-input
              v-model="addForm.serialNumber"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="单号" prop="number">
            <el-input v-model="addForm.number" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="批号" prop="lotNumber">
            <el-input
              v-model="addForm.lotNumber"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="部门" prop="department">
            <el-input
              v-model="addForm.department"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="经手人" prop="handler">
            <el-input v-model="addForm.handler" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="制单人" prop="singleHanded">
            <el-input
              v-model="addForm.singleHanded"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="供货单位" prop="vendor">
            <el-input v-model="addForm.vendor" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="备注" prop="remarks">
            <el-input
              style="width: 790px"
              type="textarea"
              :rows="1"
              v-model="addForm.remark"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="importMaterialBox">
          <el-button
            style="margin-bottom: 15px; margin-left: 1005px"
            type="primary"
            size="medium"
            @click="importMaterial"
          >
            增加明细
          </el-button>
          <el-table
            class="tables"
            :data="materialData"
            height="360"
            style="min-height: auto"
          >
            <el-table-column prop="name" label="品名" align="center">
              <template slot-scope="scope">
                <el-input
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.name"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="specification" label="规格" align="center">
              <template slot-scope="scope">
                <el-input
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.specification"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="unit" label="单位" align="center">
              <template slot-scope="scope">
                <el-input
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.unit"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="quantity" label="数量" align="center">
              <template slot-scope="scope">
                <el-input
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.quantity"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="unitPrice" label="单价" align="center">
              <template slot-scope="scope">
                <el-input
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.unitPrice"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="amount" label="金额" align="center">
              <template slot-scope="scope">
                <el-input
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.amount"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" align="center">
              <template slot-scope="scope">
                <el-input
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.remark"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <div class="tableBtns" style="margin-left: 35px !important">
                  <div
                    @click="deleteTable(scope.row, scope.index)"
                    class="operationText"
                  >
                    <img
                      src="@/assets/images/icon-delete.png"
                      width="15px"
                      height="15px"
                    />
                    <span>删除</span>
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="dialog-footer" style="text-align: center">
          <el-button
            class="cancleBtn"
            @click="dialogShow = false"
            icon="el-icon-circle-close"
            size="medium"
            >取消
          </el-button>

          <el-button
            type="primary"
            icon="el-icon-circle-check"
            @click="saveContractInfoFn"
            size="medium"
          >
            保存
          </el-button>
        </div>
      </div>
    </el-dialog>
    <!--详情弹框-->
    <el-dialog
      :modal-append-to-body="false"
      title="详情"
      :visible.sync="isShow"
      width="1167px"
    >
      <div class="dialog_content">
        <el-form
          style="margin: 0 28px"
          size="medium"
          :model="addForm"
          ref="addForm"
          :rules="addFormules"
          :inline="true"
        >
          <el-form-item label="日期" prop="createTime">
            <el-date-picker
              v-model="detailData.date"
              disabled
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="仓库" prop="warehouse">
            <el-input
              disabled
              v-model="detailData.warehouse"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="category">
            <el-input
              disabled
              v-model="detailData.category"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="方向" prop="direction" style="margin-left: 15px">
            <el-input
              disabled
              v-model="detailData.direction"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="编号"
            prop="serialNumber"
            style="margin-left: 10px"
          >
            <el-input
              disabled
              v-model="detailData.serialNumber"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="单号" prop="number">
            <el-input
              disabled
              v-model="detailData.number"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="批号" prop="lotNumber">
            <el-input
              disabled
              v-model="detailData.lotNumber"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="部门" prop="department">
            <el-input
              disabled
              v-model="detailData.department"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="经手人" prop="handler">
            <el-input
              disabled
              v-model="detailData.handler"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="制单人" prop="singleHanded">
            <el-input
              disabled
              v-model="detailData.singleHanded"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="供货单位" prop="vendor">
            <el-input
              disabled
              v-model="detailData.vendor"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="备注" prop="remarks">
            <el-input
              disabled
              style="width: 790px"
              type="textarea"
              :rows="1"
              v-model="detailData.remark"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="importMaterialBox">
          <el-table
            class="tables"
            :data="materialData"
            height="360"
            style="min-height: auto"
          >
            <el-table-column prop="name" label="品名" align="center">
              <template slot-scope="scope">
                <el-input
                  disabled
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.name"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="specification" label="规格" align="center">
              <template slot-scope="scope">
                <el-input
                  disabled
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.specification"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="unit" label="单位" align="center">
              <template slot-scope="scope">
                <el-input
                  disabled
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.unit"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="quantity" label="数量" align="center">
              <template slot-scope="scope">
                <el-input
                  disabled
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.quantity"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="unitPrice" label="单价" align="center">
              <template slot-scope="scope">
                <el-input
                  disabled
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.unitPrice"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="amount" label="金额" align="center">
              <template slot-scope="scope">
                <el-input
                  disabled
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.amount"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" align="center">
              <template slot-scope="scope">
                <el-input
                  disabled
                  style="width: 122px"
                  size="mini"
                  v-model="scope.row.remark"
                ></el-input>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="dialog-footer" style="text-align: center">
          <el-button
            type="primary"
            icon="el-icon-circle-check"
            size="medium"
            @click="isShow = false"
          >
            确认
          </el-button>
        </div>
      </div>
    </el-dialog>
   
  </div>
</template>
<script>
import {
  addGtMaterialInOutWarehouseApi,
  deletGtMaterialInOutWarehouseApi,
  editGtMaterialInOutWarehouseApi,
  gettGMaterialInOutWarehousePageApi,//查询表单数据
  gettGMaterialInOutWarehouseListApi,//根据id查询物料
} from '@/assets/js/api/materialManagement.js'
export default {
  mounted() {
    this.getList()
  },
  data() {
    return {
      timeValue: '',
      isShow: false,
      deliteShow: false,
      detailData: {},
      searchForm: {
        number: "",
        // stuffNumber:"",//货号
        warehouse: "",
        department: "",
        category: "",
        handler: "",
        // lookupCode: "",//助查码
        vendor: "",
        startTime: '',//开始时间
        endTime: '',//结束时间
      },//查询
      addForm: {
        date: "",
        warehouse: "",
        category: "",
        direction: "",
        serialNumber: "",
        number: "",
        lotNumber: "",
        department: "",
        handler: "",
        singleHanded: "",
        vendor: "",
        remark: "",
        materialList: [],
        gtMaterialInOutWarehouseId: '',
        type: "",
      },//表单
      materialData: [
        {
          name: "",
          specification: "",
          unit: "",
          quantity: "",
          unitPrice: "",
          amount: "",
          remark: "",
        }
      ],
      deliteMaterialList: [],//详情
      addFormules: {},
      title: "",
      dialogShow: false,
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0 //总条数
      },
      List: [],
      gtMaterialInOutWarehouseId: '',//编辑查询

    }
  },
  methods: {
    //选择时间时
    getTime(val) {
      if (val) {
        this.searchForm.startTime = val[0]
        this.searchForm.endTime = val[1]
      } else {
        this.searchForm.startTime = ''
        this.searchForm.endTime = ''
      }
    },
    //查询仓库数据
    getList() {
      gettGMaterialInOutWarehousePageApi({
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        projectSn: this.$store.state.projectSn,
        number: this.searchForm.number,
        warehouse: this.searchForm.warehouse,
        department: this.searchForm.department,
        category: this.searchForm.category,
        handler: this.searchForm.handler,
        vendor: this.searchForm.vendor,
        endTime:this.searchForm.endTime,
        startTime:this.searchForm.startTime,
        type: 30,
      }).then((result) => {
        if (result.success) {
          this.List = result.result.records
          this.pagInfo.total = result.result.total
        }
      })
    },
    //查询物料信息
    getMaterialData() {
      gettGMaterialInOutWarehouseListApi({
        projectSn: this.$store.state.projectSn,
        gtMaterialInOutWarehouseId: this.gtMaterialInOutWarehouseId
      }).then((result) => {
        this.materialData = result.result
      })
    },
    //显示检查详细弹框
    showDetailFn(row) {
      this.detailData = row
      this.isShow = true
      this.gtMaterialInOutWarehouseId = this.detailData.id
      this.getMaterialData()
    },
    //表格动态添加
    importMaterial() {
      // let statusType = true;
      // this.materialList.forEach((item) => {
      //   if (item.name == "" || item.specifications == "" || item.unit == "" || item.quantity == "") {
      //     this.$message({
      //       message: "请完善信息后在添加",
      //       type: "warning",
      //     });
      //     statusType = false;
      //   }

      // })
      // if (statusType) {
      this.materialData.push({
        name: "",
        specification: "",
        unit: "",
        quantity: "",
        unitPrice: "",
        amount: "",
        remark: "",
      });
      // }
    },
    //删除表格动态操作
    deleteTable(index) {
      this.materialData.splice(index, 1)
    },
    add() {
      this.title = '新增入库'
      this.dialogShow = true
      this.materialData = []
      this.addForm = {}
    },

    edit(obj) {
      console.log('obj=======', obj);
      this.title = '编辑入库'
      this.dialogShow = true
      this.addForm = JSON.parse(JSON.stringify(obj))
      this.gtMaterialInOutWarehouseId = obj.id
      this.getMaterialData()
    },
    saveContractInfoFn() {
      let params = JSON.parse(JSON.stringify(this.addForm))
      params.materialList = this.materialData
      params.projectSn = this.$store.state.projectSn
      params.type = 30
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (this.title == '新增入库') {
            addGtMaterialInOutWarehouseApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.title === '编辑入库') {
            console.log('编辑信息', this.addEditForm)
            editGtMaterialInOutWarehouseApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          }
          this.dialogShow = false
        } else {
          return false
        }
      })
    },
    deleteDev(obj) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletGtMaterialInOutWarehouseApi({ id: obj.id }).then((res) => {
            if (res.success) {
              this.getList()
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message({
                type: "error",
                message: res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    SizeChange(val) {
      this.pagInfo.pageSize = val
      this.getList()
    },
    CurrentChange(val) {
      this.pagInfo.pageNo = val
      this.getList()
    },
    refresh() {
      this.searchForm = {}
      this.pagInfo.pageNo = 1 //页数
      this.pagInfo.pageSize = 10 //条数
      this.getList()
    },
  }
}
</script>
<style lang="less" scoped>
.tables2 {
  min-height: auto;
}
.dialog_content {
  padding: 25px 0 !important;
}

::v-deep .el-select {
  width: 160px;
}

::v-deep .el-date-editor {
  width: 260px;
}

::v-deep .el-input {
  width: 160px;
}

::v-deep .el-dialog__body {
  padding: 30px 15px !important;
}
</style>