<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="80%"
      top="4%"
    >
      <el-form ref="form" v-loading="loading" status-icon :model="formItem" label-width="120px">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item :label="$t('ec.tree.parent')" prop="pid">
              <!--              <span v-if="parent && parent.id">{{ parent.name }}</span>-->
              <ec-select-tree
                ref="parentTree"
                v-model="formItem.pid"
                :title="$t('ec.tree.chooseParent')"
                check-strictly
                url="api/v1/area/sysrArea/data"
                :params="{ excludeId: record.id }"
                attr-name="name"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.area.type')"
              prop="type"
              :rules="[
                {required: true, message: $t('ec.area.type') + $t('ec.table.emptyError'), trigger:'blur'}
              ]"
            >
              <ec-select v-model="formItem.type" dict-type="areaType" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.name')"
              prop="name"
              :rules="[
                {required: true, message: $t('ec.table.name') + $t('ec.table.emptyError'), trigger:'blur'}
              ]"
            >
              <el-input v-model="formItem.name" type="text" :placeholder="$t('ec.table.name')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.shortName')"
              prop="shortName"
            >
              <el-input v-model="formItem.shortName" type="text" :placeholder="$t('ec.table.shortName')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.area.pinyin')"
              prop="pinyin"
            >
              <el-input v-model="formItem.pinyin" type="text" :placeholder="$t('ec.area.pinyin')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.area.shortPinyin')"
              prop="jianpin"
            >
              <el-input v-model="formItem.jianpin" type="text" :placeholder="$t('ec.area.shortPinyin')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.area.lng')"
              prop="lng"
            >
              <el-input v-model="formItem.lng" type="text" :placeholder="$t('ec.area.lng')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.area.lat')"
              prop="lat"
            >
              <el-input v-model="formItem.lat" type="text" :placeholder="$t('ec.area.lat')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.area.cityCode')"
              prop="cityCode"
            >
              <el-input v-model="formItem.cityCode" type="text" :placeholder="$t('ec.area.cityCode')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.area.zipCode')"
              prop="zipCode"
            >
              <el-input v-model="formItem.zipCode" type="text" :placeholder="$t('ec.area.zipCode')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.sort')"
              prop="sortNum"
            >
              <el-input-number v-model="formItem.sortNum" :placeholder="$t('ec.table.sort')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.remarks')"
              prop="remarks"
            >
              <el-input v-model="formItem.remarks" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ $t('ec.table.close') }}</el-button>
        <el-button v-if="method !== 'view'" type="primary" :loading="saveLoading" @click="save">{{ $t('ec.table.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { getById, save, update } from 'ecip-web/api/vue/area/sysrAreaApi'

export default {
  name: 'VueAreaSysrAreaForm',
  data() {
    return {
      visible: false,
      loading: false,
      saveLoading: false,
      title: '',
      method: '',
      parent: {},
      record: {},
      formItem: {
        pid: '',
        type: '',
        name: '',
        shortName: '',
        pinyin: '',
        jianpin: '',
        lng: '',
        lat: '',
        cityCode: '',
        zipCode: '',
        sort: 30,
        remarks: ''
      }
    }
  },
  methods: {
    init(method, record, parent) {
      this.visible = true
      this.loading = false
      this.method = method
      this.record = record
      this.parent = parent || {}
      if (method === 'add') {
        this.title = this.$t('ec.table.add')
      } else if (method === 'edit') {
        this.title = this.$t('ec.table.edit') + record.name
      } else if (method === 'view') {
        this.title = this.$t('ec.table.view') + record.shortName
      }
      this.$nextTick(async() => {
        // 清空初始化
        this.$refs.form.clearValidate()
        Object.assign(this.$data.formItem, this.$options.data().formItem)
        if (parent && parent.id) {
          this.formItem.pid = parent.id
        }
        if (method === 'edit' || method === 'view') { // 修改或者查看
          try {
            this.loading = true
            const { data } = await getById(record.id)
            this.formItem = Object.assign({}, this.formItem, data)
          } finally {
            this.loading = false
          }
        }
      })
    },
    save() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            this.saveLoading = true
            if (this.method === 'edit') {
              await update(this.record.id, this.formItem)
            } else {
              await save(this.formItem)
            }
            this.visible = false
            this.$notify.success(this.$t('ec.message.saveSuccess'))
            this.$emit('refreshData')
          } finally {
            this.saveLoading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
