<template>
  <div class="app-container">
    <div style="display: flex; align-items: center;">
      <img src="@/assets/logo/logo.png" alt="Logo" style="width: 170px; height: 55px; margin-right: 10px;">
      <h3 style="text-align: left; color: black; font-family: '黑体', sans-serif;font-weight: bold;font-size: 38px; margin-left: 10px;" class="title">物料检索</h3>
    </div>
    <h3>   </h3>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料编号" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />

      </el-form-item>
      <el-form-item label="名称" prop="unit">
        <el-input
          v-model="queryParams.classificationName"
          placeholder="输入对应的名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="unitPrice">
        <el-input
          v-model="queryParams.imCategory"
          placeholder="请输入所属类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="材质" prop="materialQuality">
        <el-input
          v-model="queryParams.materialQuality"
          placeholder="请输入材质"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单重" prop="singleWeight">
        <el-input
          v-model="queryParams.singleWeight"
          placeholder="请输入单重"
          clearable
          @keyup.enter.native="handleQuery"
          style="font-family: '黑体', Arial, sans-serif;"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="materialList" class="material-table">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="ID" width="60" align="center" prop="id" v-if="true"/>
      <el-table-column label="物料编号" align="center" prop="materialCode">
        <template slot-scope="scope">
          <el-link type="primary" :underline="true" class="material-code-link" @click="handleClick(scope.row)">
            {{ scope.row.materialCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="物料名称" align="center" prop="materialName">
      <template slot-scope="scope">
        <span style="font-family: '微软雅黑',sans-serif;">{{ scope.row.materialName }}</span>
      </template>
      </el-table-column>
     
      <el-table-column label="单位" align="center" prop="unit"/>
       <el-table-column label="即时库存" align="center" prop="inInventory"/>
      <el-table-column label="材质" align="center" prop="materialQuality" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.material_properties" :value="scope.row.materialQuality"/>
        </template>
      </el-table-column>
      <el-table-column label="所属类别" align="center" prop="imCategory" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.im_category" :value="scope.row.imCategory"/>
        </template>
      </el-table-column>


<!--      <el-table-column label="单价" align="center" prop="unitPrice" />-->
      <el-table-column label="物料属性" align="center" prop="classificationNumber" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.item_attributes" :value="scope.row.classificationNumber"/>
        </template>
      </el-table-column>


    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <transition name="fade">
      <el-dialog :visible="dialogVisible" @close="dialogVisible = false" width="36%">
        <el-card class="box-card" v-loading="dialogLoading" element-loading-text="加载中..." element-loading-background="rgba(255, 255, 255, 0.8)">
          <div slot="header" class="clearfix header-bar">
            <span class="dialog-title">库存信息</span>
            <span class="material-title">{{ inventoryData.materialCode }}</span>
          </div>
          <el-descriptions :column="2" border class="inventory-desc" size="small">
            <el-descriptions-item label="库存">
              <span :class="['val', inventoryData.kucun > 0 ? 'val-pos' : (inventoryData.kucun < 0 ? 'val-neg' : 'val-zero')]">{{ inventoryData.kucun }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="子项未领料量">
              <span class="val">{{ inventoryData.inventory }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="生产订单未入库">
              <span class="val">{{ inventoryData.stockName }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="采购订单未入库">
              <span class="val">{{ inventoryData.materialName }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="销售订单未出库">
              <span class="val">{{ inventoryData.salesOrder }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="预计可用库存">
              <span :class="['val', inventoryData.stockUnit > 0 ? 'val-pos' : (inventoryData.stockUnit < 0 ? 'val-neg' : 'val-zero')]">{{ inventoryData.stockUnit }}</span>
            </el-descriptions-item>
          </el-descriptions>
        </el-card>
      </el-dialog>
    </transition>


  </div>


</template>

<script>
import {listMaterial_index, material_inventory} from "@/api/system/material";
/*import upload from "svg-sprite-loader/examples/custom-runtime-generator/build/main";*/
import dict from "@/utils/dict";
import DictData from "@/components/DictData";
export default {
  name: "Material",
  dicts: ['material_properties','item_attributes','im_category'],
  computed: {
    upload() {
      return upload
    }
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      dialogLoading:  false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料检索表格数据
      materialList: [],
      dialogVisible: false, // 控制弹框的显示与隐藏
      inventoryData: {materialCode: '', inventory: 0, materialName:''}, // 库存数据
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialCode: undefined,
        materialName: undefined,
        materialQuality: undefined,
        singleWeight: undefined,
        classificationName: undefined,
        imCategory: undefined,
        unitPrice: undefined,
        unit: undefined,
        flag: undefined,
        salesOrder:undefined
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
  },

  methods: {
    handleClick(row) {
      // 在这里调用后台方法，获取数据
      this.showInventory(row.materialCode); // 打开弹框
    },
    showInventory(materialCode) {
      this.dialogLoading = true; // 显示加载动画
      material_inventory(materialCode).then(response => {
        this.inventoryData.materialCode = materialCode;
        this.inventoryData.inventory = response.data.quantity;
        this.inventoryData.materialName = response.data.materialName;
        this.inventoryData.stockName = response.data.stockName;
        this.inventoryData.stockUnit = response.data.stockUnit;
        this.inventoryData.kucun = response.data.kucun;
         this.inventoryData.salesOrder = response.data.salesOrder;
        this.dialogVisible = true;
      })
        .catch(error => {
          console.error('获取库存数失败：', error);
        })
        .finally(() => {
          this.dialogLoading = false; // 隐藏加载动画
        });
    },

    getMaterialCodeColor(materialCode) {
      if (materialCode.startsWith('009')) {
        return 'blue';
      }  else if (materialCode.startsWith('002N')||materialCode.startsWith('002Y')) {
        return 'gold';
      } else if (materialCode.startsWith('005')) {
        return 'red';
      } else if (materialCode.startsWith('006')) {
        return 'pink';
      }  else if (materialCode.startsWith('015')) {
        return 'green';
      }  else if (materialCode.startsWith('016')) {
        return 'shrimp';
      }  else if (materialCode.startsWith('017')) {
        return 'teal';
      } else {
        return ''; // 默认颜色
      }
    },
    /** 查询物料检索列表 */
    getList() {
      this.loading = true;
      listMaterial_index(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        materialCode: undefined,
        materialName: undefined,
        materialQuality: undefined,
        singleWeight: undefined,
        unitPrice: undefined,
        unit: undefined,
        flag: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
<style>
/*.app-container {
  background: linear-gradient(to right, #ff7e5f, #feb47b); !* 从左到右的渐变 *!
}*/

</style>
<style>
.el-dialog {
  left: 50%;
  top: 40%;
  transform: translate(-45%, -32%);
  margin: 0px !important;
}


.dialog-content {
  display: flex;
  flex-direction: column;
  align-items: flex-start; /* 左对齐字段数据 */
  margin-left: 5px; /* 调整字段数据与边框的距禿 */
}
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.4s ease;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
.box-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.dialog-title {
  font-size: 18px;
  font-weight: bold;
  color: #409EFF;
}

.dialog-body {
  font-family: '微软雅黑', sans-serif;
  line-height: 1.6;
}

.dialog-item {
  margin-bottom: 10px;
  font-size: 14px;
  color: #333;
}

.clearfix::after {
  content: "";
  display: block;
  clear: both;
}

.dialog-text {
  margin: 8px 0;
}
.dialog-title {
  font-size: 20px;
  font-weight: bold;
  color: burlywood;
  text-align: center;
  margin-bottom: 10px;
}
.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.material-title {
  font-size: 16px;
  font-weight: 600;
  color: #409EFF;
}
.material-table .el-table__header .cell {
  font-size: 16px;
  font-weight: 700;
  color: #303133;
}
.material-table .el-table__body .cell {
  font-size: 14px;
  font-weight: 600;
}
.material-code-link {
  font-size: 16px;
  font-weight: 700;
}
.inventory-desc {
  --label-bg: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  --label-color: #409EFF;
}
.inventory-desc .el-descriptions__label {
  background: var(--label-bg);
  color: var(--label-color);
  font-weight: 600;
}
.inventory-desc .el-descriptions__content {
  color: #303133;
  font-size: 13px;
}
.val {
  font-weight: 600;
}
.val-pos {
  color: #67C23A;
}
.val-neg {
  color: #F56C6C;
}
.val-zero {
  color: #909399;
}
</style>
