<template>
  <div>
    <div class="head">
      <div class="title">
        文本<span v-if="id!=0">编辑</span><span v-if="id==0">新建</span>
      </div>
      <div class="buttons">
        <a-button type="primary" @click="back()">返回</a-button>
      </div>
    </div>

    <div>
      <a-form-model ref="editForm" :model="model" :rules="rules">
        <a-row :gutter="colsFull">
          <a-form-model-item label="名称" prop="title" :labelCol="labelColFull" :wrapperCol="wrapperColFull">
            <a-input v-model="model.title" />
          </a-form-model-item>
        </a-row>
        <a-row :gutter="colsFull" v-if="id > 0">
          <a-form-model-item label="引用" prop="name" :labelCol="labelColFull" :wrapperCol="wrapperColFull">
            {{model.name}}
          </a-form-model-item>
        </a-row>
        <a-row :gutter="colsFull">
          <a-form-model-item label="目录" prop="folder" :labelCol="labelColFull" :wrapperCol="wrapperColFull">
            <a-input v-model="model.folder">
              <a-tree-select
                  slot="addonAfter"
                  v-model="model.folder"
                  style="width: 400px"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="dirTreeData"
                  placeholder="请选择"
                  tree-default-expand-all
                  :replaceFields="dirFieldMap"
              >
              </a-tree-select>
            </a-input>
          </a-form-model-item>
        </a-row>

      <a-row :gutter="colsFull">
        <a-form-model-item class="center">
          <a-button @click="save" type="primary">保存</a-button>
          <a-button @click="reset" style="margin-left: 10px;">重置</a-button>
        </a-form-model-item>
      </a-row>
    </a-form-model>
    </div>
  </div>
</template>

<script>
import {getExcel, saveText} from "../../../../api/manage";
import {checkDirIsData} from "../../../../api/utils";

export default {
  name: 'TestEdit',
  data() {
    return {
      colsFull: 24,
      colsHalf: 12,
      labelColFull: { lg: { span: 4 }, sm: { span: 4 } },
      wrapperColFull: { lg: { span: 16 }, sm: { span: 16 } },
      labelColHalf: { lg: { span: 8}, sm: { span: 8 } },
      labelColHalf2: { lg: { span: 4}, sm: { span: 4 } },
      wrapperColHalf: { lg: { span: 12 }, sm: { span: 12 } },
      rules: {
        title: [
          { required: true, message: '名称不能为空', trigger: 'change' },
        ],
        folder: [
          { validator: checkDirIsData, trigger: 'change' },
        ],
      },

      id: 0,
      model: { folder: 'data/'},
      dirTreeData: [],
      dirFieldMap: {children:'children', title:'name', key:'name', value: 'name' }
    };
  },

  computed: {
  },
  created () {
    this.id = parseInt(this.$route.params.id)
    console.log(this.id)
    this.loadData()
  },
  mounted () {

  },
  methods: {
    loadData () {
      if (!this.id) return

      getExcel(this.id).then(json => {
        console.log('getText', json)
        this.model = json.data
        this.dirTreeData = [json.res]
      })
    },
    save() {
      console.log('save')
      this.$refs.editForm.validate(valid => {
        console.log(valid, this.model)
        if (!valid) {
          console.log('validation fail')
          return
        }

        saveText(this.model).then(json => {
          console.log('saveText', json)
          this.back()
        })
      })
    },
    reset() {
      console.log('reset')
      this.$refs.editForm.reset()
    },
    back() {
      this.$router.push({path: '/data/buildin/excel/list'});
    },
  }
}
</script>

<style lang="less" scoped>
</style>
