<template>
  <div class="app-container" ref="appPanel">
    <div ref="toolPanel">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:pan:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:pan:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:pan:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="multiple"
            @click="handleEnable"
            v-hasPermi="['system:pan:edit']"
          >启用
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDisabled"
            v-hasPermi="['system:pan:remove']"
          >禁用
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:pan:export']"
          >导出
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
          >刷新
          </el-button>
        </el-col>
        <right-toolbar v-if="rightToolbarShow" :showSearch.sync="showSearch" @queryTable="getList"
                       showColumnsType="transfer" :columns="columns" :businessCode="businessCodeTrk"
                       :tableNo="tableNo"></right-toolbar>
      </el-row>
    </div>
    <div class="search-container" ref="searchPanel">
      <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="仓库编号" prop="storageId">
          <el-input
            v-model="queryParams.storageShortName"
            placeholder="请输入仓库编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料编号" prop="goodsId">
          <el-input
            v-model="queryParams.goodsId"
            placeholder="请输入物料编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="库存数量" prop="stockNum">
          <el-input
            v-model="queryParams.stockNum"
            placeholder="请输入库存数量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="盘点数量" prop="checkNum">
          <el-input
            v-model="queryParams.checkNum"
            placeholder="请输入盘点数量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="goodsName">
          <el-input
            v-model="queryParams.goodsName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作时间" prop="recordDate">
          <el-date-picker clearable
                          v-model="queryParams.recordDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <div ref="mainTablePanel" :style="{height: mainTableDivHeight}">
        <el-table v-loading="loading" border size="mini" :height="mainTableHeight" :data="panList"
                  @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="物料名称" align="center" prop="goodsName" v-if="columns[12].visible"/>
          <el-table-column label="物料编号" align="center" prop="goodsId" v-if="columns[4].visible"/>
          <el-table-column label="仓库名称" align="center" prop="storageShortName" v-if="columns[32].visible"/>
          <el-table-column label="库位" align="center" prop="locationId" v-if="columns[3].visible"/>
          <el-table-column label="库存数量" align="center" prop="stockNum" v-if="columns[5].visible"/>
          <el-table-column label="盘点数量" align="center" prop="checkNum" v-if="columns[6].visible"/>
          <el-table-column label="盘点结果" align="center" prop="remark" v-if="columns[7].visible"/>
          <el-table-column label="操作人" align="center" prop="recordPerson" v-if="columns[26].visible"/>
          <el-table-column label="操作时间" align="center" prop="recordDate" width="180" v-if="columns[27].visible">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.recordDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>

          <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="handleDelete(scope.row)"
                v-hasPermi="['system:pan:edit']"
              >修改库存
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagePanel">
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>

    </div>

    <!-- 添加或修改盘点明细对话框 -->
    <el-dialog id="dialog-container" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <div class="edit-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="85px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="仓库编号" prop="storageId">
                <el-input v-model="form.storageId" placeholder="请输入仓库编号"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="库区编号" prop="storageAreaId">
                <el-input v-model="form.storageAreaId" placeholder="请输入库区编号"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="库位编号" prop="locationId">
                <el-input v-model="form.locationId" placeholder="请输入库位编号"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="物料编号" prop="goodsId">
                <el-input v-model="form.goodsId" placeholder="请输入物料编号"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="库存数量" prop="stockNum">
                <el-input v-model="form.stockNum" placeholder="请输入库存数量"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="盘点数量" prop="checkNum">
                <el-input v-model="form.checkNum" placeholder="请输入盘点数量"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="版本号" prop="version">
                <el-input v-model="form.version" placeholder="请输入版本号"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="单据编号" prop="doctNumber">
                <el-input v-model="form.doctNumber" placeholder="请输入单据编号"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="入库时间" prop="wareDate">
                <el-input v-model="form.wareDate" placeholder="请输入入库时间"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="条形码" prop="barcode">
                <el-input v-model="form.barcode" placeholder="请输入条形码"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="物料名称" prop="goodsName">
                <el-input v-model="form.goodsName" placeholder="请输入物料名称"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
            </el-col>
            <el-col :span="6">
              <el-form-item label="生产批号" prop="batchNo">
                <el-input v-model="form.batchNo" placeholder="请输入生产批号"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="测量单位" prop="scaleUnit">
                <el-input v-model="form.scaleUnit" placeholder="请输入测量单位"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="物料SKU" prop="goodsSku">
                <el-input v-model="form.goodsSku" placeholder="请输入物料SKU"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="单位
" prop="unit">
                <el-input v-model="form.unit" placeholder="请输入单位
"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="规格" prop="specification">
                <el-input v-model="form.specification" placeholder="请输入规格"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="物料分类" prop="goodsTypeId">
                <el-input v-model="form.goodsTypeId" placeholder="请输入物料分类"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="品牌" prop="brand">
                <el-input v-model="form.brand" placeholder="请输入品牌"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
            </el-col>
            <el-col :span="6">
              <el-form-item label="审核人" prop="auditMan">
                <el-input v-model="form.auditMan" placeholder="请输入审核人"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="审核时间" prop="auditDate">
                <el-date-picker clearable
                                v-model="form.auditDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择审核时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="操作人" prop="recordPerson">
                <el-input v-model="form.recordPerson" placeholder="请输入操作人"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="操作时间" prop="recordDate">
                <el-date-picker clearable
                                v-model="form.recordDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择操作时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="任务号" prop="taskId">
                <el-input v-model="form.taskId" placeholder="请输入任务号"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <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 {listPan, getPan, delPan, addPan, updatePan, disabled, enabled, changeStatus} from "@/api/system/panDetail";
import {listCOLUMN} from "@/api/system/column";

export default {
  name: "Pan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盘点明细表格数据
      panList: [],
      // 弹出层标题
      title: "",
      mainTableHeight: "0px",
      mainTableDivHeight: "0px",
      businessCodeTrk: "pan",
      rightToolbarShow: false,
      tableNo: 0,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        storageId: null,
        storageAreaId: null,
        locationId: null,
        goodsId: null,
        stockNum: null,
        checkNum: null,
        version: null,
        doctNumber: null,
        wareDate: null,
        barcode: null,
        goodsName: null,
        goodsStatus: null,
        batchNo: null,
        scaleUnit: null,
        goodsSku: null,
        unit: null,
        specification: null,
        goodsTypeId: null,
        brand: null,
        status: null,
        auditMan: null,
        auditDate: null,
        confirmStatus: null,
        recordPerson: null,
        recordDate: null,
        taskId: null,
        storageShortName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      columns: [
        {key: 0, label: `主键`, visible: true, columnName: "id"},
        {key: 1, label: `仓库编号`, visible: true, columnName: "storage_id"},
        {key: 2, label: `库区编号`, visible: true, columnName: "storage_area_id"},
        {key: 3, label: `库位编号`, visible: true, columnName: "location_id"},
        {key: 4, label: `物料编号`, visible: true, columnName: "goods_id"},
        {key: 5, label: `库存数量`, visible: true, columnName: "stock_num"},
        {key: 6, label: `盘点数量`, visible: true, columnName: "check_num"},
        {key: 7, label: `备注`, visible: true, columnName: "remark"},
        {key: 8, label: `版本号`, visible: true, columnName: "version"},
        {key: 9, label: `单据编号`, visible: true, columnName: "doct_number"},
        {key: 10, label: `入库时间`, visible: true, columnName: "ware_date"},
        {key: 11, label: `条形码`, visible: true, columnName: "barcode"},
        {key: 12, label: `物料名称`, visible: true, columnName: "goods_name"},
        {key: 13, label: `货物状态`, visible: true, columnName: "goods_status"},
        {key: 14, label: `生产批号`, visible: true, columnName: "batch_no"},
        {key: 15, label: `测量单位`, visible: true, columnName: "scale_unit"},
        {key: 16, label: `物料SKU`, visible: true, columnName: "goods_sku"},
        {key: 17, label: `单位`, visible: true, columnName: "unit"},
        {key: 18, label: `规格`, visible: true, columnName: "specification"},
        {key: 19, label: `物料分类`, visible: true, columnName: "goods_type_id"},
        {key: 20, label: `品牌`, visible: true, columnName: "brand"},
        {key: 21, label: `状态`, visible: true, columnName: "status"},
        {key: 22, label: `创建人`, visible: true, columnName: "create_by"},
        {key: 23, label: `审核人`, visible: true, columnName: "audit_man"},
        {key: 24, label: `审核时间`, visible: true, columnName: "audit_date"},
        {key: 25, label: `确认状态`, visible: true, columnName: "confirm_status"},
        {key: 26, label: `操作人`, visible: true, columnName: "record_person"},
        {key: 27, label: `操作时间`, visible: true, columnName: "record_date"},
        {key: 28, label: `任务号`, visible: true, columnName: "task_id"},
        {key: 29, label: `创建时间`, visible: true, columnName: "create_time"},
        {key: 30, label: `更新时间`, visible: true, columnName: "update_time"},
        {key: 31, label: `更新人`, visible: true, columnName: "update_by"},
        {key: 32, label: `仓库名称`, visible: true, columnName: "storage_short_Name"},
      ]
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.computedTableHeight();
  },
  watch: {
    'showSearch': function () {
      this.computedTableHeight();
    }
  },
  methods: {
    /** 查询用户展示字段定义管理列表 */
    getHideColumn() {
      var queryHideColumnParams = {
        pageNum: 1,
        pageSize: 100,
        businessCode: this.businessCodeTrk,
        tableNo: this.tableNo
      };
      listCOLUMN(queryHideColumnParams).then(response => {
        if (response.rows.length > 0) {
          var row = response.rows[0];
          var columnHide = row.columnHide.split(",");
          if (columnHide.length > 0) {
            for (let item in this.columns) {
              if (columnHide.indexOf(this.columns[item].columnName) > -1) {
                this.columns[item].visible = false;
              }
            }
          }
        }
        this.rightToolbarShow = true;
      });
    },
    computedTableHeight() {
      this.$nextTick(() => {
        var totalHeight = this.$refs.appPanel.clientHeight;
        var toolHeight = this.$refs.toolPanel.clientHeight;
        var searchHeight = this.$refs.searchPanel.clientHeight;

        var tablePanelHeight = totalHeight - toolHeight - searchHeight - 14 - 5;

        this.mainTableDivHeight = tablePanelHeight + "px";
        this.mainTableHeight = (tablePanelHeight - 25) + "px";

      })
    },
    /** 查询盘点明细列表 */
    getList() {
      this.loading = true;
      listPan(this.queryParams).then(response => {
        this.panList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getHideColumn();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storageId: null,
        storageAreaId: null,
        locationId: null,
        goodsId: null,
        stockNum: null,
        checkNum: null,
        remark: null,
        version: null,
        doctNumber: null,
        wareDate: null,
        barcode: null,
        goodsName: null,
        goodsStatus: null,
        batchNo: null,
        scaleUnit: null,
        goodsSku: null,
        unit: null,
        specification: null,
        goodsTypeId: null,
        brand: null,
        status: null,
        createBy: null,
        auditMan: null,
        auditDate: null,
        confirmStatus: null,
        recordPerson: null,
        recordDate: null,
        taskId: null,
        createTime: null,
        updateTime: null,
        updateBy: 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
      getPan(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) {
            updatePan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      console.log(row.stockId)
      this.$modal.confirm('是否确认修改盘点明细编号为"' + row.goodsName + '"的库存数据？').then(function () {
        return updatePan({stockId:row.stockId});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("修改成功");
      }).catch(() => {
      });
    },

    /** 用户状态修改 */
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '"id为"' + row.id + '"数据项？').then(function () {
        return changeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 启用按钮操作 */
    handleEnable(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认启用盘点明细编号为"' + ids + '"的数据项？').then(function () {
        return enabled(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("启用成功");
      }).catch(() => {
      });
    },
    /** 禁用按钮操作 */
    handleDisabled(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认禁用盘点明细编号为"' + ids + '"的数据项？').then(function () {
        return disabled(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("禁用成功");
      }).catch(() => {
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pan/export', {
        ...this.queryParams
      }, `pan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.app-container {
  height: calc(100vh - 84px);
}
</style>
