<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" label-width="100px" >

        <el-row>
          <el-col :span="12">
            <el-form-item label="上级产品">
              <el-input v-model="filterQuery.upProduct " :disabled="isUp" style="width: 80%" placeholder="请输入多级产品" clearable></el-input>
              <el-button style="margin-left: 10px" :disabled="isUp" type="primary" @click="addUpProduct()">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="下级产品">
              <el-input v-model="filterQuery.lowProduct" style="width: 80%" placeholder="请输入下级产品" clearable></el-input>
              <el-button style="margin-left: 10px" :disabled="isLow" type="primary" @click="addLowProduct()">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
              <el-upload
              class="upload-xml"
              :action="action"
              accept=".xml"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :before-remove="beforeRemove"
              :on-success="handleSuccess"
              :on-progress="handleProgress"
              :on-error="handleError"
              :before-upload="beforeUpload"
              :limit="5"
              :on-exceed="handleExceed"
              :file-list="fileList"
              :data="extraData">
              <el-button size="small" type="primary">上传并保存</el-button>
              <div slot="tip" class="el-upload__tip">只能上传xml文件，且不超过5M</div>
            </el-upload>
          </el-col>
        </el-row>
      </el-form>


      <el-table v-loading="loading" :data="list" style="width: 100%"  border highlight-current-row>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="UDI码" prop="code"></el-table-column>
        <el-table-column label="产品编码" prop="nameCode"></el-table-column>
        <el-table-column label="产品名称" prop="cpmctymc"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh"></el-table-column>
        <el-table-column label="序列号" prop="serialNo"></el-table-column>
        <el-table-column label="生产企业" prop="manufactory"></el-table-column>
        <el-table-column label="计量单位" prop="measname"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="handleCurrentChange"
      ></pagination>
    </el-card>
  </div>
</template>
<script>
import {checkLowProduct, checkUpProduct, getDelectList, getList} from "@/api/inout/ioCodeRel";

export default {
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    idQuery: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      showSearch: true,
      newSpDistributionVisible: false,
      filterQuery: {
        upProduct:'',
        lowProduct:'',
        page: 1,
        limit: 10,
      },
      isLow:true,
      isUp:false,
      upCode:null,
      formName:null,
      loading:false,
      list:[],
      formData:null,
      total:0,
      extraData:{"uuid": "upload-xml"},
      uploadLoading: false,
      action: process.env.VUE_APP_BASE_API + "/udiwms/relCode/batch/xmlUpload",
      fileList: []
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        page: 1,
        limit: 10,
      };
      this.getList();
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },
    getList() {
      if (this.filterQuery.upProduct!=null){
        this.filterQuery.parentCode=this.filterQuery.upProduct
      }
      getDelectList(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    addUpProduct(){
      checkUpProduct({code:this.filterQuery.upProduct}).then((res)=>{
        if(res.code==20000){
          this.isLow=false;
          this.isUp=true;
        }else{
          this.$message.error(res.message);
        }
      }).catch(()=>{
        this.$message.error("扫码错误！");
      })
    },
    addLowProduct(){
      checkLowProduct({lowCode:this.filterQuery.lowProduct,upCode:this.filterQuery.upProduct}).then((res)=>{
        if(res.code==20000){
            this.getList();
        }else{
          this.$message.error(res.message);
        }
      }).catch(()=>{
        this.$message.error("扫码错误！");
      })
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    handleSuccess(res, file, fileList) {
      fileList = fileList.filter(item => item.response.code === 20000);
      this.fileList = fileList;
      if (res.code === 20000) {
        this.$message.success(res.message);
      } else {
        this.$message.error(res.message);
      }
      this.uploadLoading.close();
    },
    beforeUpload(file) {
      const isXML = file.type === 'text/xml';
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isXML) {
        this.$message.error('上传文件只能是 XML 格式!');
      }
      if (!isLt5M) {
        this.$message.error('上传文件大小不能超过 5MB!');
      }
      return isXML && isLt5M;
    },
    handleProgress() {
      this.uploadLoading = this.$loading({
        lock: true,
        text: '文件上传中…',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.5)'
      });
    },
    handleError() {
      this.uploadLoading.close();
      this.$message.error('文件上传失败，请检查文件大小或文件格式');
    }
  },
  components: {},
  filters: {},
  mounted() {
  }
  ,
  created() {
    if(this.idQuery.id!=null){
      this.filterQuery.upProduct=this.idQuery.parentCode
      this.isLow=false;
      this.isUp=true;
      this.getList();
    }

  }
  ,
}
;
</script>
<style scoped>
 .upload-xml {
   margin-left: 100px;
 }

</style>

