<template>
  <el-dialog
    :title="'酒店上下架'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-radio v-model="radio" label="1">上架</el-radio>
      <el-radio v-model="radio" label="2">下架</el-radio>
      <el-form-item label="导入酒店" prop="isRoomMap">
        <el-upload
          class="upload-demo"
          action=""
          :on-change="handleChange"
          :on-remove="handleRemove"
          :on-exceed="handleExceed"
          :limit="limitUpload"
          accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
          :auto-upload="false"
        >
          <el-button size="small" type="primary">点击导入酒店</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      limitUpload: 1,
      visible: false,
      dataForm: {
        id: 0,
        hotelIds:[],
        hotelId: '',
        dtId: '',
        storeId: '',
        groupId: '',
        blonPlantform: '',
        isDel: '',
        isUp: ''
      },
      radio: '1'
    }
  },
  methods: {

    //上传文件时处理方法
    handleChange(file, fileList) {
      this.fileTemp = file.raw;
      this.importfxx(this.fileTemp);
    },
    //超出最大上传文件数量时的处理方法
    handleExceed() {
      this.$message({
        type: "warning",
        message: "超出最大上传文件数量的限制！",
      });
    },
    //移除文件的操作方法
    handleRemove(file, fileList) {
      this.fileTemp = null;
    },
    importfxx(obj) {
      let _this = this;
      let inputDOM = this.$refs.inputer;
      // 通过DOM取文件数据

      this.file = event.currentTarget.files[0];

      var rABS = false; //是否将文件读取为二进制字符串
      var f = this.file;

      var reader = new FileReader();
      //if (!FileReader.prototype.readAsBinaryString) {
      FileReader.prototype.readAsBinaryString = function (f) {
        let binary = "";
        const rABS = false; //是否将文件读取为二进制字符串
        const pt = this;
        let wb; //读取完成的数据
        let outdata;
        const reader = new FileReader();
        reader.onload = function (e) {
          const bytes = new Uint8Array(reader.result);
          const length = bytes.byteLength;
          for (let i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i]);
          }
          //此处引入，用于解析excel
          const XLSX = require("xlsx");
          if (rABS) {
            wb = XLSX.read(btoa(fixdata(binary)), {
              //手动转化
              type: "base64",
            });
          } else {
            wb = XLSX.read(binary, {
              type: "binary",
            });
          }
          outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
          //outdata就是读取的数据（不包含标题行即表头，表头会作为对象的下标）
          //此处可对数据进行处理
          let arr = [];
          outdata.map((v) => {
            arr.push(v["酒店id"]);
          });
          _this.dataForm.hotelIds = arr;
          console.info(_this.dataForm.hotelIds)
        };
        reader.readAsArrayBuffer(f);
      };
      if (rABS) {
        reader.readAsArrayBuffer(f);
      } else {
        reader.readAsBinaryString(f);
      }
    },

    // 检 查
    select() {
      // 去重
      this.dataForm.hotelIds = this.dataForm.hotelIds.filter((item, index) => {
        return this.dataForm.hotelIds.indexOf(item) === index
      });
      this.dataListSelections = []
    },

    init(dtId , storeId, groupId , blonPlantform) {
      this.dataForm.dtId = dtId
      this.dataForm.storeId = storeId
      this.dataForm.groupId = groupId
      this.dataForm.blonPlantform = blonPlantform
      this.visible = true
      this.active = 0
    },

    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/op/taskstatus/updown`),
            method: 'post',
            data: this.$http.adornData({
              'status': this.radio,
              'hotelIds': this.dataForm.hotelIds
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
