<template>
  <div class="template-add-dialog">
    <el-dialog
      width="40%"
      :visible.sync="visible"
      :title="!dataForm.id ? '新增模板分类' : '修改模板分类'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="closeDialog"
    >
      <el-form
        ref="dataForm"
        :model="dataForm"
        :rules="dataRule"
        label-width="auto"
      >
        <el-form-item prop="content" label="分类名称">
          <el-input
            v-model="dataForm.content"
            placeholder="分类名称"
            size="small"
          />
        </el-form-item>
        <!-- 排序 -->
        <el-form-item prop="sort" label="排序">
          <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle">{{ $t('confirm') }}</el-button>
        <!-- <el-button type="primary" @click="exportContent">引入</el-button> -->
      </template>
    </el-dialog>
  </div>

</template>

<script>
export default {
  props: {
    systemInfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      visible: false,
      modelType: '',
      dataForm: {
        pid: 0,
        // 类型 1:科室，2:个人
        type: '',
        sort: 0,
        content: ''
      }
    }
  },
  computed: {
    dataRule() {
      return {
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.id) {
          !this.modelType && this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get('/bd/template/getTemplateById', {
        params: {
          id: this.dataForm.id
        }
      }).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = res.data
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs.dataForm.validate((valid) => {
        if (!valid) {
          return false
        }
        this.dataForm.type = parseFloat(this.dataForm.type)
        let url = ''
        if (this.dataForm.id) {
          url = '/bd/template/updateBdTemplate'
        } else {
          url = '/bd/template/addBdTemplate'
        }
        this.$http.post(url, { ...this.dataForm })
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }).catch(() => {})
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
    }
  }
}
</script>

<style lang="scss">
.template-add-dialog{
  .el-form-item {
    margin-bottom:8px
  }
  .el-select {
    width: 100%;
  }
  .el-form-item__error {
    top: 27% !important;
    right: 10px !important;
    left: auto;
  }
  .el-input-number {
    width: 100%;
  }
  .el-input__inner {
    text-align: left;
  }
}
</style>
