<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="serialNumber">
        <el-input v-model="queryParams.serialNumber" placeholder="请输入序号" 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="model">
        <el-input v-model="queryParams.model" placeholder="请输入型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="类别" prop="materialType">
        <el-select v-model="queryParams.materialType" placeholder="类别" clearable @keyup.enter.native="handleQuery">
          <el-option label="传感器元件-DC" value="DC"></el-option>
          <el-option label="配电元件-DP" value="DP"></el-option>
          <el-option label="控制器元件-DK" value="DK"></el-option>
          <el-option label="通讯元件-DT" value="DT"></el-option>
          <el-option label="驱动器-DQ" value="DQ"></el-option>
          <el-option label="电机-DD" value="DD"></el-option>
          <el-option label="辅料元件-DF" value="DF"></el-option>
          <el-option label="线缆-DX" value="DX"></el-option>
          <el-option label="连接器元件-DL" value="DL"></el-option>
          <el-option label="耗材元件-DH" value="DH"></el-option>
          <el-option label="机械相关-DJ" value="DJ"></el-option>
          <el-option label="电子元件-DZ" value="DZ"></el-option>
          <el-option label="部件-DB" value="DB"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="品牌" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入品牌" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="queryParams.remarks" 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:materials: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:materials: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:materials: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:materials:export']">导出</el-button>
      </el-col>
   <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-check" size="mini"  @click="pushToK3"
          v-permission="['system:materials:addToK3']">推送金蝶</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-upload" size="mini" @click="handleImportTime"
         v-permission="['system:materials:importData']">导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="showExportButton" type="primary" size="mini" @click="exportImportedData"
         v-permission="['system:materials:exportData']"
        >导出数据</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

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

      <el-table-column label="物料编码" align="left" prop="materialCode" />
      <el-table-column label="物料名称" align="left" prop="materialName" />
      <el-table-column label="型号" align="left" prop="model" />
      <el-table-column label="类别" align="center" prop="materialType" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="物料状态" align="center" prop="materialValue">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.material_status" :value="scope.row.materialValue"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />

      <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:materials:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:materials: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="型号" prop="model">
          <el-autocomplete v-model="form.model" :fetch-suggestions="handleModelSearch" :trigger-on-focus="false"
            @select="handleModelSelect" placeholder="请输入型号" clearable style="width: 100%">
            <template slot-scope="{ item }">
              <div class="material-suggestion-item">
                <div class="material-info">
                  <span>{{ item.model }}</span>
                  <el-tag size="mini" :type="item.materialValue === '1' ? 'success' : 'danger'"
                    style="margin-left: 8px">
                    {{ item.materialValue === '1' ? '有' : '无' }}
                  </el-tag>
                </div>
                <div class="sub-info">
                  {{ item.materialCode }} | {{ item.materialName }} | {{ item.brand }}
                </div>
              </div>
            </template>
          </el-autocomplete>
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="品牌" prop="brand">
              <el-input v-model="form.brand" placeholder="请输入品牌" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="物料名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 添加类别下拉框 -->
        <el-form-item label="类别" prop="materialType">
          <el-select v-model="form.materialType" placeholder="请选择类别">
            <el-option label="传感器元件-DC" value="DC"></el-option>
            <el-option label="配电元件-DP" value="DP"></el-option>
            <el-option label="控制器元件-DK" value="DK"></el-option>
            <el-option label="通讯元件-DT" value="DT"></el-option>
            <el-option label="驱动器-DQ" value="DQ"></el-option>
            <el-option label="电机-DD" value="DD"></el-option>
            <el-option label="辅料元件-DF" value="DF"></el-option>
            <el-option label="线缆-DX" value="DX"></el-option>
            <el-option label="连接器元件-DL" value="DL"></el-option>
            <el-option label="耗材元件-DH" value="DH"></el-option>
            <el-option label="机械相关-DJ" value="DJ"></el-option>
            <el-option label="电子元件-DZ" value="DZ"></el-option>
            <el-option label="部件-DB" value="DB"></el-option>
          </el-select>
        </el-form-item>

        <!-- 添加单位输入框 -->
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>

        <!-- 添加备注输入框 -->
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="form.remarks" 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>

    <!-- 导入对话框 -->
    <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="handleTimeFileUploadProgress" :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">
          <span>仅允许导入xls、xlsx格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTimeFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterials, getMaterials, delMaterials, addMaterials, updateMaterials,addToK3 } from "@/api/system/materials";
import dict from "@/utils/dict";
import DictData from "@/components/DictData";
import { getToken } from "@/utils/auth";
import upload from "svg-sprite-loader/examples/custom-runtime-generator/build/main";
import { Loading, Message } from "element-ui";
import { blobValidate, tansParams } from "@/utils/ruoyi";
import errorCode from "@/utils/errorCode";
import service from "@/utils/request";
DictData.install();
export default {
  name: "Materials",
  computed: {
    upload() {
      return upload
    }
  },
  dicts: ['material_status'],
  data() {
    return {
      // 控制导出按钮的显示
      showExportButton: false,
      // 保存导出数据
      exportDataList: [],
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/materials/importData"
      },
      modelSearchLoading: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电器物料管理表格数据
      materialsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        serialNumber: undefined,
        materialCode: undefined,
        materialName: undefined,
        model: undefined,
        materialType: undefined,
        unit: undefined,
        brand: undefined,
        materialValue: undefined,
        remarks: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialCode: [
          { required: true, message: "物料编码不能为空", trigger: "blur" }
        ],
        materialName: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        model: [
          { required: true, message: "型号不能为空", trigger: "blur" }
        ],
        materialType: [
          { required: true, message: "类别不能为空", trigger: "blur" }
        ],
        brand: [
          { required: true, message: "品牌不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    pushToK3() {
    addToK3() // 调用自定义的推送金蝶方法
      .then(response => {
        if (response.data.code === 200) {
          this.$message.success('物料已成功推送至金蝶！');
        } else {
          this.$message.error('推送金蝶失败：' + response.data.msg);
        }
      })
      .catch(error => {
        console.error('推送金蝶失败:', error);
        this.$message.error('推送金蝶失败，请重试。');
      });
},
    exportImportedData() {
      if (this.exportDataList.length > 0) {
        this.downloadJSON('system/materials/exportData', { list: this.exportDataList }, `materials_${new Date().getTime()}.xlsx`);
      } else {
        this.$message.warning('没有可导出的数据。');
      }
    },
    handleImportTime() {
      this.upload.title = "导入";
      this.upload.open = true;
    },
    handleTimeFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();

      // 检查响应是否包含有效数据
      if (response.list && response.list.length > 0) {
        this.$alert("导入成功！", "导入结果", { type: 'success' });

        // 显示导出按钮
        this.showExportButton = true; // 控制导出按钮的显示
        this.exportDataList = response.list; // 保存导出数据
      } else {
        this.$alert("导入的数据无效，请检查文件格式和内容。", "导入结果", { type: 'error' });
      }
    },
    downloadJSON(url, params, filename, config) {
      return service.post(url, params, {
        headers: { 'Content-Type': 'application/json' },
        responseType: 'blob',
        ...config
      }).then(async (data) => {
        const isBlob = blobValidate(data);
        if (isBlob) {
          const blob = new Blob([data]);
          saveAs(blob, filename);
        } else {
          const resText = await data.text();
          const rspObj = JSON.parse(resText);
          const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
          Message.error(errMsg);
        }
      }).catch((r) => {
        console.error(r);
        Message.error('下载文件出现错误，请联系管理员！');
      });
    },

    submitTimeFileForm() {
      this.$nextTick(() => {
        if (this.$refs.upload) {
          this.$refs.upload.submit();
        } else {
          console.error('Upload ref is undefined');
        }
      });
    },
    // 处理型号搜索
    handleModelSearch(queryString, callback) {
      if (queryString.length === 0) {
        callback([]);
        return;
      }

      this.modelSearchLoading = true;
      listMaterials({
        pageNum: 1,
        pageSize: 100,
        model: queryString
      }).then(response => {
        const suggestions = response.rows.map(item => ({
          materialCode: item.materialCode,
          materialName: item.materialName,
          materialType: item.materialType,
          model: item.model,
          brand: item.brand,
          materialValue: item.materialValue  // 添加物料状态字段
        }));

        // 如果没有找到物料，提示用户并生成编码
        if (suggestions.length === 0) {
          this.$message.warning('未找到对应的物料，请输入类别以生成新的物料编码。');
          this.generateMaterialCode(); // 直接调用，不传递参数
        }

        callback(suggestions);
      }).catch(error => {
        console.error('搜索物料失败:', error);
        callback([]);
      }).finally(() => {
        this.modelSearchLoading = false;
      });
    },

    generateMaterialCode() {
      const materialType = this.form.materialType; // 获取选中的类别
      if (!materialType) {
        this.$message.warning('请先选择类别。');
        return;
      }

      // 假设类别是字母，生成编码
      const prefix = materialType.charAt(0).toUpperCase(); // 获取类别的首字母
      const baseCode = `${prefix}0000000000`; // 基础编码
      this.form.materialCode = this.getNextMaterialCode(baseCode);
    },

    getNextMaterialCode(baseCode) {
      // 这里可以调用后端接口检查数据库中已有的编码
      // 假设返回的编码列表是 ["DC0000000001", "DC0000000002"]
      const existingCodes = ["DC0000000001", "DC0000000002"]; // 示例数据
      let nextCode = baseCode;

      // 找到下一个可用的编码
      for (let i = 1; i <= 9999999999; i++) {
        const newCode = `${nextCode.slice(0, -10)}${(parseInt(nextCode.slice(-10)) + i).toString().padStart(10, '0')}`;
        if (!existingCodes.includes(newCode)) {
          return newCode;
        }
      }
      return null; // 如果没有可用编码
    },

    // 处理型号选择
    handleModelSelect(item) {
      if (item) {
        // 自动填充表单字段
        this.form = {
          ...this.form,  // 保留其他字段
          materialCode: item.materialCode,
          materialType: item.materialType,
          materialName: item.materialName,
          model: item.model,
          brand: item.brand
        };
      }
    },

    dict,
    /** 查询电器物料管理列表 */
    getList() {
      this.loading = true;
      listMaterials(this.queryParams).then(response => {
        this.materialsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        serialNumber: undefined,
        materialCode: undefined,
        materialName: undefined,
        model: undefined,
        materialType: undefined,
        unit: undefined,
        brand: undefined,
        materialValue: undefined,
        remarks: 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 = "添加电气物料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getMaterials(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改电器物料管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateMaterials(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMaterials(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('是否确认删除电器物料管理编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delMaterials(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/materials/export', {
        ...this.queryParams
      }, `materials_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.material-suggestion-item {
  padding: 8px;
}

.material-info {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #303133;
}

.sub-info {
  margin-top: 4px;
  font-size: 12px;
  color: #909399;
}

/* 下拉框样式 */
:deep(.el-autocomplete-suggestion__wrap) {
  max-height: 280px;
}

:deep(.el-dialog) {
  margin-top: 15vh !important;
}

:deep(.el-form-item) {
  margin-bottom: 18px;
}
</style>
