<template>
  <div style="height: 200px;">
    <a-spin :spinning="loading" style="width: 100%;height: 100%;">
      <a-form-model ref="form" :model="formData" :rules="rules" class="from-table">
        <a-row>
          <a-col :span="4" class="bg-gray">
            <div class="required">单位</div>
          </a-col>
          <a-col :span="20">
            <a-form-model-item ref="appUnitId" prop="appUnitId">
              <unit-select v-model="formData.appUnitId" />
              <span style="color: rgb(56, 188, 213);margin-left:5px">※ 请输入单位名称查找到单位</span>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" style="text-align:center;">
            <a-button type="primary" @click="submit">保存</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </div>
</template>

<script>
import { isEmptyParams, getCardInfo } from "@/views/utils/common"
import moment from 'moment'
import unitSelect from '@/views/components/common/unitSelect'

export default {
  name: "unitEdit",
  components: {
    unitSelect
  },
  data () {
    return {
      formData: {
        id: null, appUnitId: null
      },
      rules: {
        appUnitId: [{ required: true, message: '请选择单位', trigger: 'change' }],
      },
      loading: false,
    }
  },
  props: {
    value: {
      type: String,
      default: () => {
        return null
      }
    },
    appUnitId: {
      type: String,
      default: () => {
        return null;
      },
    },
  },
  created () {
    this.getById()
  },
  methods: {
    moment,
    getById () {
      this.formData.id = this.value
      this.formData.appUnitId = this.appUnitId
    },
    submit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          let pars = isEmptyParams(this.formData)
          let par = { ...pars }
          this.$api.project.updateUnit(par).then(({ data = {} }) => {
            if (data) {
              this.$message.success('成功！')
              this.$emit('close', 'unitEdit')
            }
            this.loading = false
          }).catch(() => { this.loading = false })
        } else {
          return false
        }
      })
    }
  }
}
</script>