<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="500px"
      @close="close"
      append-to-body
      @keyup.enter.native="doSubmit"
      :visible.sync="visible">
      <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
               label-width="100px" @submit.native.prevent>
        <el-row  :gutter="15">
          <el-col :span="21">
            <el-form-item label="上级" prop="parentName"
                          :rules="[
                 ]">
              <SelectTree
                ref="areaTree"
                :props="{
                    value: 'id',             // ID字段名
                    label: 'name',         // 显示名称
                    children: 'childrenList'    // 子级字段名
                  }"
                size="medium"
                url="/help_tree/getList"
                :value="inputForm.parentId"
                :clearable="true"
                :accordion="true"
                @getValue="(value, label) => {inputForm.parentId=value;inputForm.parentName=label}"/>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="名称" prop="name"
                          :rules="[
                          {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="排序" prop="sort"
                          :rules="[
                          {required: true, message:'排序不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="备注" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" maxlength="200" v-model="inputForm.remarks" placeholder="请填写备注"  show-word-limit   ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="close()" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import HelpTreeService from '@/api/help/HelpTreeService'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          type: '',
          sort: '',
          remarks: '',
          parentId: '',
          parentName: ''
        },
        parentData: []
      }
    },
    helpTreeService: null,
    created () {
      this.helpTreeService = new HelpTreeService()
    },
    components: {
      SelectTree
    },
    methods: {
      init () {
        this.inputForm = {
          name: '',
          sort: '',
          remarks: '',
          parentId: '',
          parentName: ''
        }
        this.title = '新建'
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          // this.helpTreeService.list().then(({data}) => {
          //   this.parentData = data
          // })
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            let numReg = /^[0-9]*$/
            let numRe = new RegExp(numReg)
            if (!numRe.test(this.inputForm.sort)) {
              this.loading = false
              this.$message.error('排序请输入数字')
              return
            }
            this.helpTreeService.save(this.inputForm).then(({data}) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.inputForm.resetFields()
        this.visible = false
      }
    }
  }
</script>
