<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="TID" prop="tId">
        <el-input
          v-model="queryParams.tId"
          placeholder="请输入TID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="EPC" prop="epc">
        <el-input
          v-model="queryParams.epc"
          placeholder="请输入EPC"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次编号" prop="batchNumber">
        <el-input
          v-model="queryParams.batchNumber"
          placeholder="请输入批次编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="标签序号" prop="tagSequence">
        <el-input
          v-model="queryParams.tagSequence"
          placeholder="请输入标签序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="总数量" prop="totalQuantity">
        <el-input
          v-model="queryParams.totalQuantity"
          placeholder="请输入总数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="标签批次号" prop="tagBatch">
        <el-input
          v-model="queryParams.tagBatch"
          placeholder="请输入标签批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="modelCode">
        <el-input
          v-model="queryParams.modelCode"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加工时间">
        <el-date-picker
          v-model="daterangeProcessingTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="操作人员编号" prop="operatorId">
        <el-input
          v-model="queryParams.operatorId"
          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="['service:tagRecord: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="['service:tagRecord: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="['service:tagRecord: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="['service:tagRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tagRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键标识,RID" align="center" prop="rId" />
      <el-table-column label="TID" align="center" prop="tId" />
      <el-table-column label="EPC" align="center" prop="epc" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="批次编号" align="center" prop="batchNumber" />
      <el-table-column label="标签序号" align="center" prop="tagSequence" />
      <el-table-column label="总数量" align="center" prop="totalQuantity" />
      <el-table-column label="标签批次号" align="center" prop="tagBatch" />
      <el-table-column label="型号" align="center" prop="modelCode" />
      <el-table-column label="加工时间" align="center" prop="processingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.processingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人员编号" align="center" prop="operatorId" />
      <!-- <el-table-column label="预览字段1" align="center" prop="fields1" />
      <el-table-column label="预览字段2" align="center" prop="fields2" />
      <el-table-column label="预览字段3" align="center" prop="fields3" /> -->
      <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)"
            v-hasPermi="['service:tagRecord:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:tagRecord: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="TID" prop="tId">
          <el-input v-model="form.tId" placeholder="请输入TID" />
        </el-form-item>
        <el-form-item label="EPC" prop="epc">
          <el-input v-model="form.epc" placeholder="请输入EPC" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="批次编号" prop="batchNumber">
          <el-input v-model="form.batchNumber" placeholder="请输入批次编号" />
        </el-form-item>
        <el-form-item label="标签序号" prop="tagSequence">
          <el-input v-model="form.tagSequence" placeholder="请输入标签序号" />
        </el-form-item>
        <el-form-item label="总数量" prop="totalQuantity">
          <el-input v-model="form.totalQuantity" placeholder="请输入总数量" />
        </el-form-item>
        <el-form-item label="标签批次号" prop="tagBatch">
          <el-input v-model="form.tagBatch" placeholder="请输入标签批次号" />
        </el-form-item>
        <el-form-item label="型号" prop="modelCode">
          <el-input v-model="form.modelCode" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="加工时间" prop="processingTime">
          <el-date-picker clearable
            v-model="form.processingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择加工时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作人员编号" prop="operatorId">
          <el-input v-model="form.operatorId" placeholder="请输入操作人员编号" />
        </el-form-item>
        <el-form-item label="数据状态标记" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入数据状态标记" />
        </el-form-item>
        <el-form-item label="预览字段1" prop="fields1">
          <el-input v-model="form.fields1" placeholder="请输入预览字段1" />
        </el-form-item>
        <el-form-item label="预览字段2" prop="fields2">
          <el-input v-model="form.fields2" placeholder="请输入预览字段2" />
        </el-form-item>
        <el-form-item label="预览字段3" prop="fields3">
          <el-input v-model="form.fields3" placeholder="请输入预览字段3" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 { listTagRecord, getTagRecord, delTagRecord, addTagRecord, updateTagRecord } from "@/api/service/tagRecord";

export default {
  name: "TagRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签记录表格数据
      tagRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeProcessingTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tId: null,
        epc: null,
        password: null,
        orderCode: null,
        batchNumber: null,
        tagSequence: null,
        totalQuantity: null,
        tagBatch: null,
        modelCode: null,
        processingTime: null,
        operatorId: null,
        fields1: null,
        fields2: null,
        fields3: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tId: [
          { required: true, message: "TID不能为空", trigger: "blur" }
        ],
        epc: [
          { required: true, message: "EPC不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询标签记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeProcessingTime && '' != this.daterangeProcessingTime) {
        this.queryParams.params["beginProcessingTime"] = this.daterangeProcessingTime[0];
        this.queryParams.params["endProcessingTime"] = this.daterangeProcessingTime[1];
      }
      listTagRecord(this.queryParams).then(response => {
        this.tagRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rId: null,
        tId: null,
        epc: null,
        password: null,
        orderCode: null,
        batchNumber: null,
        tagSequence: null,
        totalQuantity: null,
        tagBatch: null,
        modelCode: null,
        processingTime: null,
        operatorId: null,
        delFlag: null,
        fields1: null,
        fields2: null,
        fields3: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProcessingTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加标签记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rId = row.rId || this.ids
      getTagRecord(rId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改标签记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rId != null) {
            updateTagRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTagRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rIds = row.rId || this.ids;
      this.$modal.confirm('是否确认删除标签记录编号为"' + rIds + '"的数据项？').then(function() {
        return delTagRecord(rIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('service/tagRecord/export', {
        ...this.queryParams
      }, `tagRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
