<template>
  <div class="addSheet-wrap">
    <div class="wrap-top">
      <ul class="form-ul">
        <li style="margin-right:30px;">
          表格名称：
          <el-input
            v-model="sheetForm.name"
            placeholder="请输入表格名称"
            clearable
            style="width:300px">
          </el-input>
        </li>
        <li>
          表格分类
          
          <el-select 
            v-model="sheetForm.classify" 
            placeholder="请选择表格分类"
            clearable
          >
							<el-option
								v-for="item in classifyArr"
								:key="item.ExcelClassifyId"
								:label="item.ExcelClassifyName"
								:value="item.ExcelClassifyId"
              />
						</el-select>
        </li>
      </ul>
      <div>
        <el-button type="primary" size="medium" @click="saveSheetHandle">保存</el-button>
        <el-button type="primary" size="medium" plain @click="backHandle">返回</el-button>
      </div>
    </div>
    
    <div class="main">
      <Sheet ref="sheetRef"/>
    </div>
  </div>
</template>

<script>
import * as sheetInterface from '@/api/modules/sheetApi.js';
import Sheet from './components/SheetExcel.vue';
import { getSheetImage } from './common/option';
export default {
  components: { Sheet },
  data() {
    return {
      sheetForm: {},
      classifyArr: []
    }
  },
  methods: {

    backHandle() {
      window.close();
    },

    /* 获取分类 */
    getClassify() {
      sheetInterface.excelClassifyOne().then(res => {
        if(res.Ret !==200) return
        
        this.classifyArr = res.Data.AllNodes || [];
      })
    },


    /* 保存表格 */
    saveSheetHandle: _.debounce(async function() {
      const { name,classify } = this.sheetForm;
      luckysheet.exitEditMode()
      let data = luckysheet.getAllSheets()[0]
      if(!name || !classify) return this.$message.warning(name ? '请选择表格分类' : '请输入表格名称')
      if(!data.celldata.length) return this.$message.warning('请输入表格内容');

      this.loading = this.$loading({
				target:'.addSheet-wrap',
				lock: true,
				text: '保存中...',
				spinner: 'el-icon-loading',
				background: 'rgba(255, 255, 255, 0.6)'
			});
      let img = getSheetImage(data);
			const form  = new FormData();
			form.append('Image', img);
			const { Data } = await sheetInterface.uploadImg(form)

      data.luckysheet_select_save = [];
      const res = await sheetInterface.sheetAdd({
        ExcelName: name,
        ExcelClassifyId: classify,
        ExcelImage: Data.ResourceUrl,
        Content: JSON.stringify(data)
      })
      this.loading.close()
      if(res.Ret !== 200) return

      this.$message.success(res.Msg);

      const { ExcelInfoId, UniqueCode } = res.Data;
      
      this.$router.replace({
        path: '/sheetList',
        query: {
          code: UniqueCode,
          id: ExcelInfoId
        }
      })
    },300),
    
  },
  mounted() {
    this.getClassify();
  }
}
</script>
<style scoped lang="scss">
*{ box-sizing: border-box; }
.addSheet-wrap {
  min-height: calc(100vh - 120px);
  .wrap-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 20px;
    background: #fff;
    border: 1px solid #ececec;
    border-radius: 4px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.05);
    display: flex;
    z-index: 1;
    .form-ul {
      flex: 1;
      display: flex;
    }
  }
  .main {
    position: relative;
    min-height: 700px;
  }
}
</style>