<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 :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="parentId">
              <SelectTree
                ref="areaTree"
                :props="{
                      value: 'id',             // ID字段名
                      label: 'name',         // 显示名称
                      children: 'children'    // 子级字段名
                    }"
                url="/public-modules-server/material/materialType/treeData?type=5"
                :value="inputForm.parentId"
                :clearable="true"
                :accordion="true"
				size="default"
                @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="sort">
              <el-input v-model="inputForm.sort" :disabled="true" show-word-limit></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
		<template #footer>
			<span class="dialog-footer">
			  <el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
			  <el-button 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 ReimbursementApprovalTypeService from '@/api/cw/reimbursementApproval/ReimbursementApprovalTypeService'
  import MaterialTypeService from '@/api/materialManagement/MaterialTypeService'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  // import OfficeService from '@/api/sys/OfficeService'
  export default {
    data () {
      return {
        disabled: false,
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          businessType: '',
          name: '',
          sort: '',
          parentId: ''
        }
      }
    },
    // reimbursementTypeService: null,
    reimbursementApprovalTypeService: null,
    materialTypeService: null,
    // OfficeService: null,
    created () {
      // this.reimbursementTypeService = new ReimbursementTypeService()
      this.reimbursementApprovalTypeService = new ReimbursementApprovalTypeService()
      this.materialTypeService = new MaterialTypeService()
      // this.officeService = new OfficeService()
    },
    components: {
      SelectTree
    },
    methods: {
      init (method, id, businessType) {
        this.method = method
        this.inputForm = {
          businessType: '',
          name: '',
          sort: '',
          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.materialTypeService.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.materialTypeService.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>
