<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="900px"
      @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="90px" @submit.native.prevent>
        <el-row  :gutter="15">
          <el-col :span="12">
            <el-form-item label="关联项" prop="treeName"
                          :rules="[
                          {required: true, message:'关联项不能为空', trigger:'change'}
                 ]">
              <el-input v-model="inputForm.treeName" @focus="chooseTree" :readonly="true" placeholder="请选择关联项">
                <el-button slot="append" icon="el-icon-search" @click="chooseTree"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编号" prop="number"
                          :rules="[
                 ]">
              <el-input :disabled="true" v-model="inputForm.number" placeholder="编号自动生成"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="功能" prop="features"
                          :rules="[
                          {required: true, message:'功能不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.features" placeholder="请填写功能"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="useFlag"
                          :rules="[
                          {required: true, message:'请选择是否启用', trigger:'change'}
                 ]">
              <el-radio-group v-model="inputForm.useFlag">
                <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" >{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="description"
                          :rules="[
                 ]">
              <el-input type="textarea" :rows="4" maxlength="500" v-model="inputForm.description" 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>
      <HelpTreeListForm ref="helpTreeListForm" @getData="getData"></HelpTreeListForm>
    </el-dialog>
  </div>
</template>

<script>
  import HelpService from '@/api/help/HelpService'
  import HelpTreeListForm from './helpTree/HelpTreeListForm'

  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          treeName: '',
          treeId: '',
          number: '',
          features: '',
          useFlag: '',
          description: ''
        }
      }
    },
    helpService: null,
    created () {
      this.helpService = new HelpService()
    },
    components: {
      HelpTreeListForm
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          id: '',
          treeName: '',
          treeId: '',
          number: '',
          features: '',
          useFlag: '',
          description: ''
        }
        if (method === 'add') {
          this.title = `新建帮助`
        } else if (method === 'edit') {
          this.title = '修改帮助'
        } else if (method === 'view') {
          this.title = '帮助详情'
        }
        this.inputForm.id = id
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.helpService.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.helpService.save(this.inputForm).then(({data}) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.inputForm = {
          id: '',
          treeName: '',
          treeId: '',
          number: '',
          features: '',
          useFlag: '',
          description: ''
        }
        this.$refs.inputForm.resetFields()
        this.visible = false
      },
      // 打开选择组件
      chooseTree () {
        this.$refs.helpTreeListForm.init()
      },
      getData (row) {
        this.inputForm.treeName = this.commonJS.isNotEmpty(row.name) ? row.name : ''
        this.inputForm.treeId = this.commonJS.isNotEmpty(row.id) ? row.id : ''
      }
    }
  }
</script>
