<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="BOM型号" prop="bomNumber">
        <el-select v-model="boxType" placeholder="请选择箱子类型">
          <el-option label="30D双层箱" value="30D_double_layer_box"></el-option>
          <el-option label="30D单层箱" value="30D_single_layer_box"></el-option>
          <el-option label="30S双层箱" value="30S_double_layer_box"></el-option>
          <el-option label="30S单层箱" value="30S_single_layer_box"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="BOM型号" prop="bomNumber">
        <el-input
          v-model="queryParams.bomNumber"
          placeholder="请输入BOM型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


<!--      <el-form-item label="行程" prop="V1">
        <el-input
          v-model="queryParams.V1"
          placeholder="请输入行程"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备总长" prop="V2">
        <el-input
          v-model="queryParams.V2"
          placeholder="请输入设备总长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地脚位置1" prop="V3">
        <el-input
          v-model="queryParams.V3"
          placeholder="请输入地脚位置1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="箱体装配长度" prop="V5">
        <el-input
          v-model="queryParams.V5"
          placeholder="请输入箱体装配长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="箱体地脚位置1" prop="V6">
        <el-input
          v-model="queryParams.V6"
          placeholder="请输入箱体地脚位置1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="铝箱长度1" prop="V8">
        <el-input
          v-model="queryParams.V8"
          placeholder="请输入铝箱长度1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="铝箱1重量" prop="G1">
        <el-input
          v-model="queryParams.G1"
          placeholder="请输入铝箱1重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导向条长度1" prop="V10">
        <el-input
          v-model="queryParams.V10"
          placeholder="请输入导向条长度1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导向条1单重1" prop="G5">
        <el-input
          v-model="queryParams.G5"
          placeholder="请输入导向条1单重1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导向条1单重2" prop="G8">
        <el-input
          v-model="queryParams.G8"
          placeholder="请输入导向条1单重2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导向条长度2" prop="V12">
        <el-input
          v-model="queryParams.V12"
          placeholder="请输入导向条长度2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导向条2单重1" prop="G6">
        <el-input
          v-model="queryParams.G6"
          placeholder="请输入导向条2单重1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导向条2单重2" prop="G9">
        <el-input
          v-model="queryParams.G9"
          placeholder="请输入导向条2单重2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="30D.02.4" prop="V41">
        <el-input
          v-model="queryParams.V41"
          placeholder="请输入30D.02.4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="V42">
        <el-input
          v-model="queryParams.V42"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="V43">
        <el-input
          v-model="queryParams.V43"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="V44">
        <el-input
          v-model="queryParams.V44"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="V45">
        <el-input
          v-model="queryParams.V45"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="V46">
        <el-input
          v-model="queryParams.V46"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="V47">
        <el-input
          v-model="queryParams.V47"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="V48">
        <el-input
          v-model="queryParams.V48"
          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="['system:variable: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:variable: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:variable: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:variable:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport1"
          v-hasPermi="['system:details:import']"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="variableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" v-if="true"/>
      <el-table-column label="BOM型号" align="center" prop="bomNumber" />
      <el-table-column label="v1" align="center" prop="v1" />
      <el-table-column label="V2" align="center" prop="v2" />
      <el-table-column label="V22" align="center" prop="v22" />
      <el-table-column label="V3" align="center" prop="v3" />
      <el-table-column label="V23" align="center" prop="v23" />
      <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="V11" align="center" prop="v11" />
      <el-table-column label="G8" align="center" prop="g8" />
      <el-table-column label="V12" align="center" prop="v12" />
      <el-table-column label="G6" align="center" prop="g6" />
      <el-table-column label="V13" align="center" prop="v13" />
      <el-table-column label="V14" align="center" prop="v14" />
      <el-table-column label="G7" align="center" prop="g7" />
      <el-table-column label="V15" align="center" prop="v15" />
      <el-table-column label="G9" align="center" prop="g9" />
      <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="V49" align="center" prop="v49" />
      <el-table-column label="V50" align="center" prop="v50" />
      <el-table-column label="V51" align="center" prop="v51" />
      <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="handleImport(scope.row)"
            v-hasPermi="['system:variable:import']"
          >导入</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:variable:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:variable: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="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的物料数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改bom变量对话框 -->
    <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="BOM型号" prop="bomNumber">
          <el-input v-model="form.bomNumber" placeholder="请输入BOM型号" />
        </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="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单重1" />
        </el-form-item>
        <el-form-item label="G8" prop="G8">
          <el-input v-model="form.G8" placeholder="请输入导向条1单重2" />
        </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单重1" />
        </el-form-item>
        <el-form-item label="G9" prop="G9">
          <el-input v-model="form.G9" placeholder="请输入导向条2单重2" />
        </el-form-item>
        <el-form-item label="V41" prop="V41">
          <el-input v-model="form.V41" placeholder="请输入30D.02.4" />
        </el-form-item>
        <el-form-item label="V42" prop="V42">
          <el-input v-model="form.V42" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="V43" prop="V43">
          <el-input v-model="form.V43" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="V44" prop="V44">
          <el-input v-model="form.V44" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="V45" prop="V45">
          <el-input v-model="form.V45" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="V46" prop="V46">
          <el-input v-model="form.V46" placeholder="请输入" />
        </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 { listVariable, getVariable, delVariable, addVariable, updateVariable } from "@/api/system/variable";
import upload from "svg-sprite-loader/examples/custom-runtime-generator/build/main";
import {getToken} from "@/utils/auth";

export default {
  name: "Variable",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // bom变量表格数据
      variableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bomNumber: undefined,
        V1: undefined,
        V2: undefined,
        V22: undefined,
        V3: undefined,
        V23: undefined,
        V5: undefined,
        V6: undefined,
        V8: undefined,
        G1: undefined,
        V9: undefined,
        G2: undefined,
        V10: undefined,
        G5: undefined,
        V11: undefined,
        G8: undefined,
        V12: undefined,
        G6: undefined,
        V13: undefined,
        V14: undefined,
        G7: undefined,
        V15: undefined,
        G9: undefined,
        V41: undefined,
        V42: undefined,
        V43: undefined,
        V44: undefined,
        V45: undefined,
        V46: undefined,
        V47: undefined,
        V48: undefined,
        V49: undefined,
        V50: undefined,
        V51: undefined,
      },
      // 表单参数
      form: {},
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        //url: process.env.VUE_APP_BASE_API + "/system/variable/importData"

      },
      // 表单校验
      rules: {
        id: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        bomNumber: [
          { required: true, message: "BOM型号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询bom变量列表 */
    getList() {
      this.loading = true;
      listVariable(this.queryParams).then(response => {
        this.variableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 导入按钮操作 */
    handleImport(e) {
      const data = e;
      this.upload.title = "明细导入";
      this.upload.open = true;
      const url = process.env.VUE_APP_BASE_API + "/system/variable/importData" +"/"+data.id
      console.log(url);
      this.upload.url = url
    },
    handleImport1() {
      const data = this.ids.join(",")
      this.upload.title = "明细导入";
      this.upload.open = true;
      const url = process.env.VUE_APP_BASE_API + "/system/variable/importData" +"/"+data
      this.upload.url = url
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/variable/importTemplate', {}, `details_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        bomNumber: undefined,
        V1: undefined,
        V2: undefined,
        V3: undefined,
        V5: undefined,
        V6: undefined,
        V8: undefined,
        G1: undefined,
        V9: undefined,
        G2: undefined,
        V10: undefined,
        G5: undefined,
        G8: undefined,
        V12: undefined,
        G6: undefined,
        V14: undefined,
        G7: undefined,
        G9: undefined,
        V41: undefined,
        V42: undefined,
        V43: undefined,
        V44: undefined,
        V45: undefined,
        V46: undefined,
        V47: undefined,
        V48: 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 = "添加bom变量";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getVariable(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改bom变量";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateVariable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addVariable(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('是否确认删除bom变量编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delVariable(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/variable/export', {
        ...this.queryParams
      }, `variable_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
