<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="仓库名称" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" filterable placeholder="选择仓库" clearable>
          <el-option
            v-for="item in warehouseList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <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="materialSpecifications">
        <el-input
          v-model="queryParams.materialSpecifications"
          placeholder="请输入物资规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialList">
      <el-table-column label="仓库" align="center" fixed="left" prop="warehouseName" />
      <el-table-column label="供应商" align="center" prop="vendorName" fixed="left" show-overflow-tooltip />
      <el-table-column label="物资编码" align="center" fixed="left" prop="materialCode" />
      <el-table-column label="物资名称" align="center" fixed="left" prop="materialName" />
      <el-table-column label="物资规格" align="center" fixed="left" prop="materialSpecifications" />
<!--      <el-table-column label="物资品牌" align="center" fixed="left" prop="materialBrand" />-->
      <el-table-column label="物资单位" align="center" fixed="left" prop="materialUnit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.material_unit" :value="scope.row.materialUnit" />
        </template>
      </el-table-column>
<!--      <el-table-column label="采购标的" align="center" prop="procurementSubject" />-->
      <el-table-column label="适用车型" align="center" prop="compatibleWith" show-overflow-tooltip/>
      <el-table-column label="物资图片" align="center" prop="materialUrl">
        <template slot-scope="scope">
          <image-preview :src="scope.row.materialUrl" style="width: 50px; height: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="质保期(月)" align="center" prop="materialWarrantyPeriod" />
      <el-table-column label="参考价" align="center" prop="referencePrice" />
      <el-table-column label="销售价" align="center" prop="sellingPrice" />
      <el-table-column label="库存" align="center" prop="inventory" />
      <el-table-column label="操作" align="center" width="90px" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:materialStock:query']"
          >库存明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-tabs v-model="active">
        <el-tab-pane label="入库记录" name="1">
          <el-table :data="materialPutawayInfoList">
            <el-table-column label="入库单号" align="center" prop="putawayCode" />
            <el-table-column label="入库数量" align="center" prop="number" />
            <el-table-column label="入库单价" align="center" prop="price" />
            <el-table-column label="入库金额" align="center" prop="money" />
            <el-table-column label="创建人" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="150px" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="退货记录" name="2">
          <el-table :data="materialReturnsInfoList">
            <el-table-column label="退货单号" align="center" prop="returnsCode" />
            <el-table-column label="入库单号" align="center" prop="putawayCode" />
            <el-table-column label="退货数量" align="center" prop="number" />
            <el-table-column label="退货单价" align="center" prop="price" />
            <el-table-column label="退货金额" align="center" prop="money" />
            <el-table-column label="创建人" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="150px" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="出库记录" name="3">
          <el-table :data="materialPickingInfoList">
            <el-table-column label="报修单号" align="center" prop="repairNumber" />
            <el-table-column label="出库单号" align="center" prop="outNumber" />
            <el-table-column label="出库数量" align="center" prop="number" />
            <el-table-column label="出库单价" align="center" prop="price" />
            <el-table-column label="出库金额" align="center" prop="money" />
            <el-table-column label="创建人" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="150px" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="退料记录" name="4">
          <el-table :data="materialBackInfoList">
            <el-table-column label="报修单号" align="center" prop="repairNumber" />
            <el-table-column label="出库单号" align="center" prop="outNumber" />
            <el-table-column label="退料单号" align="center" prop="returnNumber" />
            <el-table-column label="退料数量" align="center" prop="number" />
            <el-table-column label="退料单价" align="center" prop="price" />
            <el-table-column label="退料金额" align="center" prop="money" />
            <el-table-column label="创建人" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="150px" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import {listByMaterialId, listMaterialInventory} from "@/api/business/material";
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {listWarehouseAll} from "@/api/business/siteWarehouse";

export default {
  name: "Material",
  dicts:['sys_is_enable', 'putaway_info_state', 'picking_state', 'material_unit'],
  components: { Treeselect },
  data() {
    return {
      active: '1',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物资表格数据
      materialList: [],
      // 仓库列表
      warehouseList: [],
      // 物料入库明细表格数据
      materialPutawayInfoList: [],
      // 物料退货明细表格数据
      materialReturnsInfoList: [],
      // 物料出库明细表格数据
      materialPickingInfoList: [],
      // 物料退料明细表格数据
      materialBackInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vendorName: undefined,
        materialCode: undefined,
        materialName: undefined,
        materialSpecifications: undefined,
        warehouseName: undefined
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
  },
  methods: {
    /** 查询物资列表 */
    getList() {
      this.loading = true;
      listMaterialInventory(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 仓库列表 */
    getWarehouseList() {
      listWarehouseAll().then(response => {
        this.warehouseList = response.data;
      });
    },
    /** 物资分类更改 */
    // materialTypeIdChange() {
    //   this.$refs.form.clearValidate('materialTypeId')
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        materialTypeId: undefined,
        materialTypeName: undefined,
        warehouseLocationId: undefined,
        warehouseLocationType: undefined,
        vendorId: undefined,
        vendorName: undefined,
        materialCode: undefined,
        materialName: undefined,
        materialSpecifications: undefined,
        materialUnit: undefined,
        materialBrand: undefined,
        procurementSubject: undefined,
        compatibleWith: undefined,
        materialUrl: undefined,
        materialWarrantyPeriod: 1,
        referencePrice: 0,
        sellingPrice: 0,
        inventory: 0,
        isEnable: '1',
        isUniversal: '1',
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = `【${row.warehouseName}】【${row.vendorName}】【${row.materialName}】库存记录`;
      let {warehouseId, vendorId, materialId, type} = row
      this.getPutawayInfoList(warehouseId, vendorId, materialId)
      this.getReturnsInfoList(warehouseId, vendorId, materialId)
      this.getPickingInfoList(warehouseId, vendorId, materialId)
      this.getBackInfoList(warehouseId, vendorId, materialId)
    },
    /** 查询物料入库明细列表 */
    getPutawayInfoList(warehouseId, vendorId, materialId) {
      listByMaterialId({warehouseId, vendorId, materialId, type: 1}).then(response => {
        this.materialPutawayInfoList = response.data;
      });
    },
    /** 查询物料退货明细列表 */
    getReturnsInfoList(warehouseId, vendorId, materialId) {
      listByMaterialId({warehouseId, vendorId, materialId, type: 2}).then(response => {
        this.materialReturnsInfoList = response.data;
      });
    },
    /** 查询物料出库列表 */
    getPickingInfoList(warehouseId, vendorId, materialId) {
      listByMaterialId({warehouseId, vendorId, materialId, type: 3}).then(response => {
        this.materialPickingInfoList = response.data;
      });
    },
    /** 查询物料退料列表 */
    getBackInfoList(warehouseId, vendorId, materialId) {
      listByMaterialId({warehouseId, vendorId, materialId, type: 4}).then(response => {
        this.materialBackInfoList = response.data;
      });
    },
  }
};
</script>
