<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
	  draggable
      width="500px"
      @close="close"
      @keyup.enter.native="doSubmit"
      v-model="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="businessType"
                          :rules="[{required: true, message: '业务类型不能为空', trigger: 'blur'}
                 ]">
              <el-select :disabled="this.inputForm.businessType !== '' & method !== 'edit'" v-model="inputForm.businessType" placeholder="请选择业务类型" style="width:100%;">
                <el-option
                  v-for="item in $dictUtils.getDictList('business_type')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="上级节点" prop="parentId">
              <SelectTree
                ref="areaTree"
                :props="{
                      value: 'id',             // ID字段名
                      label: 'name',         // 显示名称
                      children: 'children'    // 子级字段名
                    }"
                url="/reimbursementApproval/type/treeData?type=3"
                :value="inputForm.parentId"
                :clearable="true"
                :accordion="true"
                @getValue="(value) => {inputForm.parentId=value}"/>
            </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="no">
              <el-input v-model="inputForm.no" :disabled="true" show-word-limit></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
		<template #footer>
			 <span 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>
		</template>
    </el-dialog>
  </div>
</template>

<script>
  // import ReimbursementTypeService from '@/api/sys/ReimbursementTypeService'
  import ReimbursementTypeService from '@/api/consultancy/reimbursement/ReimbursementTypeService'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        disabled: false,
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          businessType: '',
          name: '',
          no: '',
          parentId: ''
        }
      }
    },
    // reimbursementTypeService: null,
    reimbursementTypeService: null,
    created () {
      // this.reimbursementTypeService = new ReimbursementTypeService()
      this.reimbursementTypeService = new ReimbursementTypeService()
    },
    components: {
      SelectTree
    },
    methods: {
      init (method, id, businessType) {
        this.method = method
        this.inputForm = {
          businessType: '',
          name: '',
          no: '',
          parentId: ''
        }
        if (method === 'add') {
          this.title = `新建报销类型`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改报销类型'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看报销类型'
        } else if (method === 'addChild') {
          this.title = '添加下级结构'
          this.inputForm.parentId = id
          this.inputForm.businessType = businessType
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$refs.inputForm.resetFields()
            this.reimbursementTypeService.findById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            })
            // this.reimbursementTypeService.findById(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.reimbursementTypeService.save(this.inputForm).then((data) => {
              if (data === false) {
                this.close()
                this.$message.error('报销内容名重复,请重新填写')
                this.$emit('refreshDataList')
                this.loading = false
              } else {
                this.close()
                this.$message.success(data)
                this.$emit('refreshDataList')
                this.loading = false
              }
            }).catch(() => {
              this.loading = false
            })
            // this.reimbursementTypeService.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>
