<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.materialName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>

        <el-form-item label="规格型号">
          <el-input
            v-model="searchForm.specificationAndModel"
            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">
        <el-table-column
          prop="materialName"
          label="物料名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="specificationAndModel"
          align="center"
          label="规格型号"
        ></el-table-column>
        <el-table-column
          prop="projectPlanUsage"
          align="center"
          label="项目计划用量"
        >
          <template slot-scope="scope">
            {{
              scope.row.materialUnit == 1
                ? scope.row.projectPlanUsage? scope.row.projectPlanUsage+'斤':''
                : scope.row.materialUnit == 2
                ? scope.row.projectPlanUsage? scope.row.projectPlanUsage + '克':''
                : scope.row.materialUnit == 3
                ? scope.row.projectPlanUsage? scope.row.projectPlanUsage+ '千克':''
                : scope.row.materialUnit == 4
                ? scope.row.projectPlanUsage? scope.row.projectPlanUsage+ '吨':''
                : scope.row.projectPlanUsage
            }}
          </template>
        </el-table-column>
        <el-table-column prop="inventoryNumber" align="center" label="库存数">
          <template slot-scope="scope">
            {{
              scope.row.materialUnit == 1
                ? scope.row.inventoryNumber?scope.row.inventoryNumber + '斤':''
                : scope.row.materialUnit == 2
                ? scope.row.inventoryNumber?scope.row.inventoryNumber  + '克':''
                : scope.row.materialUnit == 3
                ? scope.row.inventoryNumber?scope.row.inventoryNumber  + '千克':''
                : scope.row.materialUnit == 4
                ? scope.row.inventoryNumber?scope.row.inventoryNumber  + '吨':''
                : scope.row.inventoryNumber
            }}
          </template>
        </el-table-column>
        <el-table-column
          prop="safetyStockNumber"
          align="center"
          label="安全库存数"
        >
          <template slot-scope="scope">
            {{
              scope.row.materialUnit == 1
                ? scope.row.safetyStockNumber ?scope.row.safetyStockNumber+ '斤':''
                : scope.row.materialUnit == 2
                ? scope.row.safetyStockNumber?scope.row.safetyStockNumber + '克':''
                : scope.row.materialUnit == 3
                ? scope.row.safetyStockNumber?scope.row.safetyStockNumber + '千克':''
                : scope.row.materialUnit == 4
                ? scope.row.safetyStockNumber?scope.row.safetyStockNumber + '吨':''
                : scope.row.safetyStockNumber
            }}
          </template>
        </el-table-column>
        <el-table-column
          prop="purchasedQuantity"
          align="center"
          label="需采购数量"
        >
          <template slot-scope="scope">
            {{
              scope.row.materialUnit == 1
                ? scope.row.purchasedQuantity?scope.row.purchasedQuantity + '斤':''
                : scope.row.materialUnit == 2
                ? scope.row.purchasedQuantity?scope.row.purchasedQuantity + '克':''
                : scope.row.materialUnit == 3
                ? scope.row.purchasedQuantity?scope.row.purchasedQuantity + '千克':''
                : scope.row.materialUnit == 4
                ? scope.row.purchasedQuantity?scope.row.purchasedQuantity + '吨':''
                : scope.row.purchasedQuantity
            }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <div class="tableBtns" style="margin-left: 60px !important">
              <div @click="edit(scope.row)" class="operationText">
                <img
                  src="@/assets/images/icon-edit.png"
                  width="15px"
                  height="15px"
                />
                <span>编辑</span>
              </div>
              <div @click="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"
      @close="close"
      :title="title"
      :visible.sync="dialogShow"
      width="667px"
    >
      <div class="dialog_content">
        <el-form
          size="medium"
          ref="addEditForm"
          :model="addEditForm"
          label-width="120px"
          class="dialogFormBox"
        >
          <el-form-item label="物料名称" prop="materialName">
            <el-input
              v-model="addEditForm.materialName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="物料单位" prop="materialUnit">
            <el-select v-model="addEditForm.materialUnit">
              <el-option
                v-for="(item, index) in resultList"
                :key="index"
                :value="item.value"
                :label="item.materialUnit"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="规格型号" prop="specificationAndModel">
            <el-input
              v-model="addEditForm.specificationAndModel"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目计划用量" prop="projectPlanUsage">
            <el-input
              type="number"
              v-model="addEditForm.projectPlanUsage"
              placeholder="请输入数字类型"
            ></el-input>
          </el-form-item>
          <el-form-item label="库存数" prop="inventoryNumber">
            <el-input
              type="number"
              v-model="addEditForm.inventoryNumber"
              placeholder="请输入数字类型"
            ></el-input>
          </el-form-item>
          <el-form-item label="安全库存数" prop="safetyStockNumber">
            <el-input
              type="number"
              v-model="addEditForm.safetyStockNumber"
              placeholder="请输入数字类型"
            ></el-input>
          </el-form-item>
          <el-form-item label="需采购数量" prop="purchasedQuantity">
            <el-input
              type="number"
              v-model="addEditForm.purchasedQuantity"
              placeholder="请输入数字类型"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="addEditForm.remark"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <div class="dialog-footer">
            <el-button
              class="cancleBtn"
              @click="dialogShow = false"
              icon="el-icon-circle-close"
              size="medium"
              >{{ $t('message.deviceManage.cancel') }}
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              @click="submit"
              size="medium"
              >{{ $t('message.deviceManage.save') }}
            </el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addMaterialPurchaseDemandApi,
  deleteMaterialPurchaseDemandApi,
  editMaterialPurchaseDemandApi,
  getMaterialPurchaseDemandPageApi
} from '@/assets/js/api/materialManagement.js'
export default {
  mounted() {
    this.getList()
  },
  data() {
    return {
      title: "",
      dialogShow: false,
      pagInfo: {
        pageNo: 1, //页数
        pageSize: 10, //条数
        total: 0 //总条数
      },
      List: [],
      addEditForm: {
        materialName: "",//物料名称
        specificationAndModel: "",//规格型号
        projectPlanUsage: "",//项目计划用量
        inventoryNumber: "",//库存数
        safetyStockNumber: "",//安全库存数
        purchasedQuantity: "",//需采购数量
        remark: "",//备注
        materialUnit: '',//物料单位
      },
      resultList: [
        {
          value: 1,
          materialUnit: '斤'
        },
        {
          value: 2,
          materialUnit: '克'
        },
        {
          value: 3,
          materialUnit: '千克'
        },
        {
          value: 4,
          materialUnit: '吨'
        },
      ],

      searchForm: {
        materialName: "",
        specificationAndModel: "",
      },
    }
  },
  methods: {
    //查询仓库数据
    getList() {
      getMaterialPurchaseDemandPageApi({
        pageNo: this.pagInfo.pageNo,
        pageSize: this.pagInfo.pageSize,
        projectSn: this.$store.state.projectSn,
        materialName: this.searchForm.materialName,
        specificationAndModel: this.searchForm.specificationAndModel,
      }).then((result) => {
        if (result.success) {
          this.List = result.result.records
          this.pagInfo.total = result.result.total
        }
      })
    },

    add() {
      this.title = '新增计划'
      this.dialogShow = true
      this.close()
    },

    edit(obj) {
      this.title = '编辑计划'
      this.dialogShow = true
      this.addEditForm = JSON.parse(JSON.stringify(obj))
    },
    submit() {
      let params = JSON.parse(JSON.stringify(this.addEditForm))
      params.projectSn = this.$store.state.projectSn
      this.$refs.addEditForm.validate((valid) => {
        if (valid) {
          if (this.title == '新增计划') {
            addMaterialPurchaseDemandApi(params).then((result) => {
              if (result.success) {
                this.$message.success(result.message)
                this.getList()
              }
            })
          } else if (this.title === '编辑计划') {
            console.log('编辑信息', this.addEditForm)
            editMaterialPurchaseDemandApi(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(() => {
          deleteMaterialPurchaseDemandApi({ 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: "已取消删除",
          });
        });
    },

    close() {
      this.addEditForm = {}
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    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;
}
</style>