<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="stocktakeId">
        <el-input
          v-model="queryParams.stocktakeId"
          placeholder="请输入盘点单号 检索条件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物 字典，检索条件" prop="materialId">
        <el-input
          v-model="queryParams.materialId"
          placeholder="请输入货物 字典，检索条件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次 检索条件" prop="batchCode">
        <el-input
          v-model="queryParams.batchCode"
          placeholder="请输入批次 检索条件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库 检索条件" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库 检索条件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位 检索条件" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入库位 检索条件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统数量" prop="systemQuantity">
        <el-input
          v-model="queryParams.systemQuantity"
          placeholder="请输入系统数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际数量" prop="actualQuantity">
        <el-input
          v-model="queryParams.actualQuantity"
          placeholder="请输入实际数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="差异数量" prop="varianceQuantity">
        <el-input
          v-model="queryParams.varianceQuantity"
          placeholder="请输入差异数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="差异金额" prop="varianceAmount">
        <el-input
          v-model="queryParams.varianceAmount"
          placeholder="请输入差异金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点人" prop="countedBy">
        <el-input
          v-model="queryParams.countedBy"
          placeholder="请输入盘点人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点时间" prop="countedAt">
        <el-date-picker clearable
          v-model="queryParams.countedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择盘点时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否已调整0否1是" prop="adjusted">
        <el-input
          v-model="queryParams.adjusted"
          placeholder="请输入是否已调整0否1是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调整原因" prop="adjustmentReason">
        <el-input
          v-model="queryParams.adjustmentReason"
          placeholder="请输入调整原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调整人" prop="adjustedBy">
        <el-input
          v-model="queryParams.adjustedBy"
          placeholder="请输入调整人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调整时间" prop="adjustedAt">
        <el-date-picker clearable
          v-model="queryParams.adjustedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择调整时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开始时间 暂无用" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间 暂无用">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间 暂无用" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间 暂无用">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="应用数据1使用0删除 删除用" prop="isUsed">
        <el-input
          v-model="queryParams.isUsed"
          placeholder="请输入应用数据1使用0删除 删除用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sortNo">
        <el-input
          v-model="queryParams.sortNo"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="createUserCode">
        <el-input
          v-model="queryParams.createUserCode"
          placeholder="请输入创建日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序号" prop="updateUserCode">
        <el-input
          v-model="queryParams.updateUserCode"
          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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inventory:items:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['inventory:items:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['inventory:items:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['inventory:items:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="盘点单号 检索条件" align="center" prop="stocktakeId" />
      <el-table-column label="货物ID 字典，检索条件" align="center" prop="materialId" />
      <el-table-column label="批次ID 检索条件" align="center" prop="batchCode" />
      <el-table-column label="仓库ID 检索条件" align="center" prop="warehouseId" />
      <el-table-column label="库位ID 检索条件" align="center" prop="locationId" />
      <el-table-column label="系统数量" align="center" prop="systemQuantity" />
      <el-table-column label="实际数量" align="center" prop="actualQuantity" />
      <el-table-column label="差异数量" align="center" prop="varianceQuantity" />
      <el-table-column label="差异金额" align="center" prop="varianceAmount" />
      <el-table-column label="盘点状态1-计划中 1-盘点中 2-已完成 字典，检索条件" align="center" prop="stocktakeStatus" />
      <el-table-column label="盘点人" align="center" prop="countedBy" />
      <el-table-column label="盘点时间" align="center" prop="countedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.countedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已调整0否1是" align="center" prop="adjusted" />
      <el-table-column label="调整原因" align="center" prop="adjustmentReason" />
      <el-table-column label="调整人" align="center" prop="adjustedBy" />
      <el-table-column label="调整时间" align="center" prop="adjustedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.adjustedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间 暂无用" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间 暂无用" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点物料范围 暂无用" align="center" prop="materialRange" />
      <el-table-column label="盘点库位范围 暂无用" align="center" prop="locationRange" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="应用数据1使用0删除 删除用" align="center" prop="isUsed" />
      <el-table-column label="排序" align="center" prop="sortNo" />
      <el-table-column label="创建日期" align="center" prop="createUserCode" />
      <el-table-column label="排序号" align="center" prop="updateUserCode" />
      <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)"
            v-hasPermi="['inventory:items:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['inventory:items:remove']"
          >删除</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="盘点单号 检索条件" prop="stocktakeId">
          <el-input v-model="form.stocktakeId" placeholder="请输入盘点单号 检索条件" />
        </el-form-item>
        <el-form-item label="货物 字典，检索条件" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入货物 字典，检索条件" />
        </el-form-item>
        <el-form-item label="批次 检索条件" prop="batchCode">
          <el-input v-model="form.batchCode" placeholder="请输入批次 检索条件" />
        </el-form-item>
        <el-form-item label="仓库 检索条件" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库 检索条件" />
        </el-form-item>
        <el-form-item label="库位 检索条件" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入库位 检索条件" />
        </el-form-item>
        <el-form-item label="系统数量" prop="systemQuantity">
          <el-input v-model="form.systemQuantity" placeholder="请输入系统数量" />
        </el-form-item>
        <el-form-item label="实际数量" prop="actualQuantity">
          <el-input v-model="form.actualQuantity" placeholder="请输入实际数量" />
        </el-form-item>
        <el-form-item label="差异数量" prop="varianceQuantity">
          <el-input v-model="form.varianceQuantity" placeholder="请输入差异数量" />
        </el-form-item>
        <el-form-item label="差异金额" prop="varianceAmount">
          <el-input v-model="form.varianceAmount" placeholder="请输入差异金额" />
        </el-form-item>
        <el-form-item label="盘点人" prop="countedBy">
          <el-input v-model="form.countedBy" placeholder="请输入盘点人" />
        </el-form-item>
        <el-form-item label="盘点时间" prop="countedAt">
          <el-date-picker clearable
            v-model="form.countedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择盘点时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否已调整0否1是" prop="adjusted">
          <el-input v-model="form.adjusted" placeholder="请输入是否已调整0否1是" />
        </el-form-item>
        <el-form-item label="调整原因" prop="adjustmentReason">
          <el-input v-model="form.adjustmentReason" placeholder="请输入调整原因" />
        </el-form-item>
        <el-form-item label="调整人" prop="adjustedBy">
          <el-input v-model="form.adjustedBy" placeholder="请输入调整人" />
        </el-form-item>
        <el-form-item label="调整时间" prop="adjustedAt">
          <el-date-picker clearable
            v-model="form.adjustedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择调整时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间 暂无用" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间 暂无用">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间 暂无用" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间 暂无用">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="盘点物料范围 暂无用" prop="materialRange">
          <el-input v-model="form.materialRange" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="盘点库位范围 暂无用" prop="locationRange">
          <el-input v-model="form.locationRange" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="应用数据1使用0删除 删除用" prop="isUsed">
          <el-input v-model="form.isUsed" placeholder="请输入应用数据1使用0删除 删除用" />
        </el-form-item>
        <el-form-item label="排序" prop="sortNo">
          <el-input v-model="form.sortNo" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="创建日期" prop="createUserCode">
          <el-input v-model="form.createUserCode" placeholder="请输入创建日期" />
        </el-form-item>
        <el-form-item label="排序号" prop="updateUserCode">
          <el-input v-model="form.updateUserCode" placeholder="请输入排序号" />
        </el-form-item>
      </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>
  </div>
</template>

<script>
import { listItems, getItems, delItems, addItems, updateItems } from "@/api/inventory/stocktakes_items"

export default {
  name: "Items",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盘点单明细表格数据
      itemsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stocktakeId: null,
        materialId: null,
        batchCode: null,
        warehouseId: null,
        locationId: null,
        systemQuantity: null,
        actualQuantity: null,
        varianceQuantity: null,
        varianceAmount: null,
        stocktakeStatus: null,
        countedBy: null,
        countedAt: null,
        adjusted: null,
        adjustmentReason: null,
        adjustedBy: null,
        adjustedAt: null,
        startTime: null,
        endTime: null,
        materialRange: null,
        locationRange: null,
        isUsed: null,
        sortNo: null,
        createUserCode: null,
        updateUserCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询盘点单明细列表 */
    getList() {
      this.loading = true
      listItems(this.queryParams).then(response => {
        this.itemsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stocktakeId: null,
        materialId: null,
        batchCode: null,
        warehouseId: null,
        locationId: null,
        systemQuantity: null,
        actualQuantity: null,
        varianceQuantity: null,
        varianceAmount: null,
        stocktakeStatus: null,
        countedBy: null,
        countedAt: null,
        adjusted: null,
        adjustmentReason: null,
        adjustedBy: null,
        adjustedAt: null,
        startTime: null,
        endTime: null,
        materialRange: null,
        locationRange: null,
        remark: null,
        isUsed: null,
        sortNo: null,
        createTime: null,
        createUserCode: null,
        updateTime: null,
        updateUserCode: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加盘点单明细"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getItems(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改盘点单明细"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItems(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addItems(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除盘点单明细编号为"' + ids + '"的数据项？').then(function() {
        return delItems(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('inventory/items/export', {
        ...this.queryParams
      }, `items_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
