<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="SAP计划编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入SAP计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="materialBarcode">
        <el-input
          v-model="queryParams.materialBarcode"
          placeholder="请输入打印条码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打印原因" prop="printReason">
        <el-select v-model="queryParams.printReason" placeholder="请选择打印原因" clearable>
          <el-option label="报废重做" value="报废重做"></el-option>
          <el-option label="切换订单" value="切换订单"></el-option>
          <el-option label="PDA补码" value="PDA补码"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="打印时间">
        <el-date-picker
          v-model="daterangePrintTime"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
<!--      <el-form-item label="打印名称" prop="printName">-->
<!--        <el-input-->
<!--          v-model="queryParams.printName"-->
<!--          placeholder="请输入打印名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否打印" prop="isPrint">-->
<!--        <el-input-->
<!--          v-model="queryParams.isPrint"-->
<!--          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="['report:supplementBarCode: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="['report:supplementBarCode: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="['report:supplementBarCode: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="['report:supplementBarCode:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supplementBarCodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="objId" v-if="columns[0].visible"/>
      <el-table-column label="SAP计划编号" align="center" prop="orderCode" v-if="columns[1].visible"/>
      <el-table-column label="物料编码" align="center" prop="materialCode" v-if="columns[2].visible"/>
      <el-table-column label="物料名称" align="center" prop="materialName" width="230"  v-if="columns[3].visible"/>
      <el-table-column label="打印条码" align="center" prop="materialBarcode" width="110" v-if="columns[4].visible"/>
      <el-table-column label="打印时间" align="center" prop="printTime" width="180" v-if="columns[5].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.printTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打印类型" align="center" prop="printBartype" v-if="columns[6].visible"/>
      <el-table-column label="打印名称" align="center" prop="printName" v-if="columns[7].visible"/>
      <el-table-column label="打印原因" align="center" prop="printReason" width="100" v-if="columns[9].visible"/>
      <el-table-column label="记录时间" align="center" prop="recordTime" width="180" v-if="columns[10].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否打印" align="center" prop="isPrint" v-if="columns[8].visible"/>
      <el-table-column label="记录人" align="center" prop="createBy" v-if="columns[11].visible"/>
<!--      <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="['report:supplementBarCode:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['report:supplementBarCode: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="SAP计划编号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入SAP计划编号" />
        </el-form-item>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="打印条码" prop="materialBarcode">
          <el-input v-model="form.materialBarcode" placeholder="请输入打印条码" />
        </el-form-item>
        <el-form-item label="打印时间" prop="printTime">
          <el-date-picker clearable
            v-model="form.printTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择打印时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打印名称" prop="printName">
          <el-input v-model="form.printName" placeholder="请输入打印名称" />
        </el-form-item>
        <el-form-item label="是否打印" prop="isPrint">
          <el-input v-model="form.isPrint" 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 { listSupplementBarCode, getSupplementBarCode, delSupplementBarCode, addSupplementBarCode, updateSupplementBarCode } from "@/api/report/supplementBarCode";

export default {
  name: "SupplementBarCode",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 补打条码记录表格数据
      supplementBarCodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否打印时间范围
      daterangePrintTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        materialCode: null,
        materialName: null,
        materialBarcode: null,
        printReason: null,
        printTime: null,
        printBartype: null,
        printName: null,
        isPrint: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      columns: [
            { key: 0, label: `主键`, visible: false },
            { key: 1, label: `SAP计划编号`, visible: true },
            { key: 2, label: `物料编码`, visible: true },
            { key: 3, label: `物料名称`, visible: true },
            { key: 4, label: `打印条码`, visible: true },
            { key: 5, label: `打印时间`, visible: true },
            { key: 6, label: `打印类型`, visible: true },
            { key: 7, label: `打印名称`, visible: true },
            { key: 8, label: `是否打印`, visible: true },
            { key: 9, label: `打印原因`, visible: true },
            { key: 10, label: `记录时间`, visible: true },
            { key: 11, label: `记录人`, visible: true },
        ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询补打条码记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePrintTime && '' != this.daterangePrintTime) {
        this.queryParams.params["beginPrintTime"] = this.daterangePrintTime[0];
        this.queryParams.params["endPrintTime"] = this.daterangePrintTime[1];
      }
      listSupplementBarCode(this.queryParams).then(response => {
        this.supplementBarCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        objId: null,
        orderCode: null,
        materialCode: null,
        materialName: null,
        materialBarcode: null,
        printTime: null,
        printBartype: null,
        printName: null,
        isPrint: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePrintTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.objId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加补打条码记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const objId = row.objId || this.ids
      getSupplementBarCode(objId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改补打条码记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.objId != null) {
            updateSupplementBarCode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplementBarCode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const objIds = row.objId || this.ids;
      this.$modal.confirm('是否确认删除补打条码记录编号为"' + objIds + '"的数据项？').then(function() {
        return delSupplementBarCode(objIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('report/supplementBarCode/export', {
        ...this.queryParams
      }, `supplementBarCode_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
