<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="travelLength">
        <el-input
          v-model="queryParams.travelLength"
          placeholder="请输入行程变量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="箱体" prop="boxType">
        <el-select
          v-model="queryParams.boxType"
          placeholder="请选择箱体层数"
          clearable
          @change="handleQuery"
        >
          <el-option label="S(单层)" value="S" />
          <el-option label="D(双层)" value="D" />
          <el-option label="T(三层)" value="T" />
        </el-select>
      </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="['system:spec35r: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="['system:spec35r: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="['system:spec35r: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="['system:spec35r:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="spec35rList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="行程" align="center" prop="travelLength" />
      <el-table-column label="类型" align="center" prop="itemType" />
      <el-table-column label="箱体" align="center" prop="boxType" />
      <el-table-column label="V1" align="center" prop="V1" />
      <el-table-column label="V2" align="center" prop="V2" />
      <el-table-column label="V3" align="center" prop="V3" />
      <el-table-column label="V5" align="center" prop="V5" />
      <el-table-column label="V6" align="center" prop="V6" />
      <el-table-column label="V8" align="center" prop="V8" />
      <el-table-column label="G1" align="center" prop="G1" />
      <el-table-column label="V9" align="center" prop="V9" />
      <el-table-column label="G2" align="center" prop="G2" />
      <el-table-column label="V10" align="center" prop="V10" />
      <el-table-column label="G5" align="center" prop="G5" />
      <el-table-column label="V12" align="center" prop="V12" />
      <el-table-column label="G6" align="center" prop="G6" />
      <el-table-column label="V14" align="center" prop="V14" />
      <el-table-column label="G7" align="center" prop="G7" />
      <el-table-column label="V41" align="center" prop="V41" />
      <el-table-column label="V42" align="center" prop="V42" />
      <el-table-column label="V43" align="center" prop="V43" />
      <el-table-column label="V44" align="center" prop="V44" />
      <el-table-column label="V45" align="center" prop="V45" />
      <el-table-column label="V46" align="center" prop="V46" />
      <el-table-column label="V47" align="center" prop="V47" />
      <el-table-column label="V48" align="center" prop="V48" />
      <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="['system:spec35r:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:spec35r: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"
    />

    <!-- 添加或修改35R设备规格参数对话框 -->
    <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="travelLength">
          <el-input v-model="form.travelLength" placeholder="请输入行程变量" />
        </el-form-item>
        <el-form-item label="V1" prop="V1">
          <el-input v-model="form.V1" placeholder="请输入行程" />
        </el-form-item>
        <el-form-item label="V2" prop="V2">
          <el-input v-model="form.V2" placeholder="请输入设备总长" />
        </el-form-item>
        <el-form-item label="V3" prop="V3">
          <el-input v-model="form.V3" placeholder="请输入地脚位置1" />
        </el-form-item>
        <el-form-item label="V5" prop="V5">
          <el-input v-model="form.V5" placeholder="请输入箱体装配长度" />
        </el-form-item>
        <el-form-item label="V6" prop="V6">
          <el-input v-model="form.V6" placeholder="请输入箱体地脚位置1" />
        </el-form-item>
        <el-form-item label="V8" prop="V8">
          <el-input v-model="form.V8" placeholder="请输入铝箱长度1" />
        </el-form-item>
        <el-form-item label="G1" prop="G1">
          <el-input v-model="form.G1" placeholder="请输入铝箱1重量" />
        </el-form-item>
        <el-form-item label="V9" prop="V9">
          <el-input v-model="form.V9" placeholder="请输入铝箱长度2" />
        </el-form-item>
        <el-form-item label="G2" prop="G2">
          <el-input v-model="form.G2" placeholder="请输入铝箱2重量" />
        </el-form-item>
        <el-form-item label="V10" prop="V10">
          <el-input v-model="form.V10" placeholder="请输入导向条长度1" />
        </el-form-item>
        <el-form-item label="G5" prop="G5">
          <el-input v-model="form.G5" placeholder="请输入导向条1单重" />
        </el-form-item>
        <el-form-item label="V12" prop="V12">
          <el-input v-model="form.V12" placeholder="请输入导向条长度2" />
        </el-form-item>
        <el-form-item label="G6" prop="G6">
          <el-input v-model="form.G6" placeholder="请输入导向条2单重" />
        </el-form-item>
        <el-form-item label="V14" prop="V14">
          <el-input v-model="form.V14" placeholder="请输入导向条长度3" />
        </el-form-item>
        <el-form-item label="G7" prop="G7">
          <el-input v-model="form.G7" placeholder="请输入导向条3单重" />
        </el-form-item>
        <el-form-item label="V41" prop="V41">
          <el-input v-model="form.V41" placeholder="请输入轴挡" />
        </el-form-item>
        <el-form-item label="V42" prop="V42">
          <el-input v-model="form.V42" placeholder="请输入35E链条滚轮" />
        </el-form-item>
        <el-form-item label="V43" prop="V43">
          <el-input v-model="form.V43" placeholder="请输入35E垫圈" />
        </el-form-item>
        <el-form-item label="V44" prop="V44">
          <el-input v-model="form.V44" placeholder="请输入35R链节轴2" />
        </el-form-item>
        <el-form-item label="V45" prop="V45">
          <el-input v-model="form.V45" placeholder="请输入35E链节轴2" />
        </el-form-item>
        <el-form-item label="V46" prop="V46">
          <el-input v-model="form.V46" placeholder="请输入35E链板" />
        </el-form-item>
        <el-form-item label="V47" prop="V47">
          <el-input v-model="form.V47" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="V48" prop="V48">
          <el-input v-model="form.V48" 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 { listSpec35r, getSpec35r, delSpec35r, addSpec35r, updateSpec35r } from "@/api/system/spec35r";

export default {
  name: "Spec35r",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 35R设备规格参数表格数据
      spec35rList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        travelLength: undefined,
        itemType: undefined,
        axialType: undefined,
        boxType: undefined,
        V1: undefined,
        V2: undefined,
        V3: undefined,
        V5: undefined,
        V6: undefined,
        V8: undefined,
        G1: undefined,
        V9: undefined,
        G2: undefined,
        V10: undefined,
        G5: undefined,
        V12: undefined,
        G6: undefined,
        V14: undefined,
        G7: undefined,
        V41: undefined,
        V42: undefined,
        V43: undefined,
        V44: undefined,
        V45: undefined,
        V46: undefined,
        V47: undefined,
        V48: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键ID不能为空", trigger: "blur" }
        ],
        travelLength: [
          { required: true, message: "行程变量不能为空", trigger: "blur" }
        ],
        itemType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],

        boxType: [
          { required: true, message: "箱体不能为空", trigger: "change" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    normalizeRowKeys(row) {
      const keys = Object.keys(row);
      keys.forEach(k => {
        if (/^(v\d+|g\d+)$/i.test(k)) {
          const upper = k.toUpperCase();
          if (row[upper] === undefined) row[upper] = row[k];
        }
      });
      return row;
    },
    /** 查询35R设备规格参数列表 */
    getList() {
      this.loading = true;
      listSpec35r(this.queryParams).then(response => {
        this.spec35rList = (response.rows || []).map(r => this.normalizeRowKeys(r));
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        travelLength: undefined,
        itemType: undefined,
        axialType: undefined,
        boxType: undefined,
        V1: undefined,
        V2: undefined,
        V3: undefined,
        V5: undefined,
        V6: undefined,
        V8: undefined,
        G1: undefined,
        V9: undefined,
        G2: undefined,
        V10: undefined,
        G5: undefined,
        V12: undefined,
        G6: undefined,
        V14: undefined,
        G7: undefined,
        V41: undefined,
        V42: undefined,
        V43: undefined,
        V44: undefined,
        V45: undefined,
        V46: undefined,
        V47: undefined,
        V48: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: 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
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加35R设备规格参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSpec35r(id).then(response => {
        this.loading = false;
        this.form = this.normalizeRowKeys(response.data || {});
        this.open = true;
        this.title = "修改35R设备规格参数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSpec35r(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSpec35r(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除35R设备规格参数编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delSpec35r(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/spec35r/export', {
        ...this.queryParams
      }, `spec35r_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
