<template>
  <el-dialog
    title="备品备件选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="false"
    width="1100px"
    center
    :before-close="cancel"
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="备品备件号" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入备品备件号"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="备件类型" prop="spareType">
        <el-select
          v-model="queryParams.spareType"
          placeholder="请选择备件类型"
          @keyup.enter.native="handleQuery"
          @change="$forceUpdate()"
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="item in options"
            :key="item.spareType"
            :label="item.label"
            :value="item.spareType"
          ></el-option>
        </el-select>
      </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-row :gutter="10" class="mb8">
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="sparePartsLedgerList"
        @selection-change="handleSelectionChange"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!-- 序号 -->
        <el-table-column
          type="index"
          width="90"
          align="center"
          :index="indexMethod"
          label="序号"
        />
        <el-table-column
          label="备品备件号"
          align="center"
          prop="materialCode"
          width="120"
        />
        <el-table-column
          label="备品备件描述"
          align="center"
          prop="materialDesc"
          width="120"
        />
        <el-table-column
          label="备品备件类型"
          align="center"
          prop="spareType"
          width="120"
        />
        <el-table-column label="规格型号" align="center" prop="spareMode" />
        <el-table-column label="库存总数量" align="center" prop="amount" />
        <el-table-column
          label="库存冻结数量"
          align="center"
          prop="storageAmount"
        />
        <el-table-column
          label="库存可用数量"
          align="center"
          prop="availableQuantity"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.amount - scope.row.storageAmount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="领用数量" align="center" prop="spareQuantity">
          <template slot-scope="scope">
            <el-input
              size="small"
              v-model="scope.row.spareQuantity"
              @change="handleEdit(scope.$index, scope.row)"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
  
  <script>
import {
  listSparePartsLedger,
  getSparePartsLedger,
  delSparePartsLedger,
  addSparePartsLedger,
  updateSparePartsLedger,
} from "@/api/device/sparePartsLedger";

export default {
  name: "SparePartsLedger",
  data() {
    return {
      //展示
      showFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 备品备件台账管理表格数据
      sparePartsLedgerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storageId: null,
        whCode: null,
        regionCode: null,
        waCode: null,
        storageType: null,
        wlCode: null,
        materialCode: null,
        materialDesc: null,
        amount: null,
        storageAmount: null,
        occupyAmount: null,
        lpn: null,
        productBatch: null,
        receiveDate: null,
        productDate: null,
        userDefined1: null,
        userDefined2: null,
        userDefined3: null,
        userDefined4: null,
        userDefined5: null,
        userDefined6: null,
        userDefined7: null,
        userDefined8: null,
        userDefined9: null,
        userDefined10: null,
        gmtCreate: null,
        lastModifiedBy: null,
        gmtModified: null,
        activeFlag: null,
        factoryCode: null,
        sapFactoryCode: null,
        wlName: null,
        spareUseLife: null,
        spareName: null,
        spareMode: null,
        spareManufacturer: null,
        spareSupplier: null,
        spareReplacementCycle: null,
        spareMeasurementUnit: null,
        spareConversionUnit: null,
        spareConversionRatio: null,
        spareInventoryFloor: null,
        spareInventoryUpper: null,
      },
      options: [
        {
          spareType: "专用",
          label: "专用",
        },
        {
          spareType: "通用",
          label: "通用",
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storageId: [
          { required: true, message: "唯一序列不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleEdit(index, row) {
      console.log("row:", index, row);
      console.log("storageId:", row.storageId); //获取到变化行的name值
    },
    // 生成表头序号
    indexMethod(index) {
      return index + 1;
    },
    /** 查询备品备件台账管理列表 */
    getList() {
      this.loading = true;
      listSparePartsLedger(this.queryParams).then((response) => {
        this.sparePartsLedgerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.showFlag = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storageId: null,
        whCode: null,
        regionCode: null,
        waCode: null,
        storageType: null,
        wlCode: null,
        materialCode: null,
        materialDesc: null,
        amount: null,
        storageAmount: null,
        occupyAmount: null,
        lpn: null,
        productBatch: null,
        receiveDate: null,
        productDate: null,
        userDefined1: null,
        userDefined2: null,
        userDefined3: null,
        userDefined4: null,
        userDefined5: null,
        userDefined6: null,
        userDefined7: null,
        userDefined8: null,
        userDefined9: null,
        userDefined10: null,
        createBy: null,
        gmtCreate: null,
        lastModifiedBy: null,
        gmtModified: null,
        activeFlag: null,
        factoryCode: null,
        sapFactoryCode: null,
        wlName: null,
        delFlag: null,
        spareUseLife: null,
        spareName: null,
        spareMode: null,
        spareManufacturer: null,
        spareSupplier: null,
        spareReplacementCycle: null,
        spareMeasurementUnit: null,
        spareConversionUnit: null,
        spareConversionRatio: null,
        spareInventoryFloor: null,
        spareInventoryUpper: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.storageId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 提交按钮 */
    submitForm() {
      console.log("data:", this.sparePartsLedgerList);
      console.log("选择的数据", this.$refs.multipleTable.selection);
      if (this.$refs.multipleTable.selection.length > 1) {
        this.$message({
          message: "请勿选择多种备件！",
          type: "warning",
        });
        // const number =
        //   this.$refs.multipleTable.selection.amount -
        //   this.$refs.multipleTable.selection.storageAmount;
        // console.log("number", number);
        // if (this.$refs.multipleTable.selection.spareQuantity > number) {
        //   this.$message({
        //     message: "您填入的领用数量大于库存可用数量！",
        //     type: "warning",
        //   });
        // }
      } else {
        this.$emit("onSelected", this.$refs.multipleTable.selection);
        this.showFlag = false;
      }
    },
  },
};
</script>
  