<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="存储位置" prop="warehouseId">
        <WarehouseSelect v-model="queryParams.warehouseId" placeholder="请选择仓库/库区/库位" clearable @change="getList" />
      </el-form-item>
      <MaterialSelect :itemType.sync="queryParams.itemType" :itemId.sync="queryParams.itemId" @change="getList" />
      <el-form-item label="变动时间" prop="changeTime">
        <el-date-picker 
          clearable
          v-model="queryParams.changeTime"
          type="daterange"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际库存变动时间">
        </el-date-picker>
      </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">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" v-if="true"/>
      <el-table-column label="仓库/库区/库位ID" align="center" prop="warehouseId" />
      <el-table-column label="物品ID" align="center" prop="itemId">
        <template slot-scope="scope">
          <RawMaterialInfo v-if="scope.row.itemType == 'raw_material'" :material-id="scope.row.itemId" />
          <ProductInfo v-else-if="scope.row.itemType == 'product' || scope.row.itemType == 'semi'" :product-id="scope.row.itemId" />
        </template>
      </el-table-column>
      <el-table-column label="物品类型" align="center" prop="itemType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stock_item_type" :value="scope.row.itemType" />
        </template>
      </el-table-column>
      <el-table-column label="变动数量" align="center" prop="changeQty" />
      <el-table-column label="变动后的库存数量" align="center" prop="afterQty" />
      <el-table-column label="变动类型" align="center" prop="changeType" />
      <el-table-column label="实际库存变动时间" align="center" prop="changeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.changeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库/库区/库位ID" prop="warehouseId" disabled>
          <el-input v-model="form.warehouseId" placeholder="请输入仓库/库区/库位ID" disabled />
        </el-form-item>
        <el-form-item label="物品ID" prop="itemId" disabled>
          <el-input v-model="form.itemId" placeholder="请输入物品ID" disabled />
        </el-form-item>
        <el-form-item label="变动数量" prop="changeQty" disabled>
          <el-input v-model="form.changeQty" placeholder="请输入变动数量" disabled />
        </el-form-item>
        <el-form-item label="变动后的库存数量" prop="afterQty" disabled>
          <el-input v-model="form.afterQty" placeholder="请输入变动后的库存数量" disabled />
        </el-form-item>
        <el-form-item label="实际库存变动时间" prop="changeTime" disabled>
          <el-date-picker clearable
            v-model="form.changeTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择实际库存变动时间" disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStockLog, getStockLog, updateStockLog } from "@/api/wms/stockLog";
import WarehouseSelect from '@/components/KLPService/WarehouseSelect/index.vue';
import RawMaterialSelect from '@/components/KLPService/RawMaterialSelect/index.vue';
import ProductSelect from '@/components/KLPService/ProductSelect/index.vue';
import SemiSelect from '@/components/KLPService/SemiSelect/index.vue';
import RawMaterialInfo from '@/components/KLPService/Renderer/RawMaterialInfo.vue';
import ProductInfo from '@/components/KLPService/Renderer/ProductInfo.vue';
import MaterialSelect from '@/components/KLPService/MaterialSelect/index.vue';

export default {
  name: "StockLog",
  components: {
    WarehouseSelect,
    RawMaterialSelect,
    ProductSelect,
    RawMaterialInfo,
    ProductInfo,
    SemiSelect,
    MaterialSelect
  },
  dicts: ['stock_item_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存流水表格数据
      stockLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: undefined,
        itemId: undefined,
        itemType: undefined,
        changeQty: undefined,
        afterQty: undefined,
        changeType: undefined,
        changeTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const itemId = this.$route.query.itemId;
    const itemType = this.$route.query.itemType;
    if (itemId && itemType) {
      this.queryParams.itemId = itemId;
      this.queryParams.itemType = itemType;
    }
    this.getList();
  },
  methods: {
    /** 查询库存流水列表 */
    getList() {
      this.loading = true;
      const { changeTime, ...payload } = {
        ...this.queryParams,
        startTime: this.queryParams.changeTime ? this.queryParams.changeTime[0] + ' 00:00:00' : undefined,
        endTime: this.queryParams.changeTime ? this.queryParams.changeTime[1] + ' 23:59:59' : undefined
      }
      listStockLog(payload).then(response => {
        this.stockLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        warehouseId: undefined,
        itemId: undefined,
        itemType: undefined,
        changeQty: undefined,
        afterQty: undefined,
        changeType: undefined,
        changeTime: undefined,
        remark: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getStockLog(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改库存流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateStockLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('klp/stockLog/export', {
        ...this.queryParams
      }, `stockLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
