<template>
  <div>
    <el-dialog
      v-el-drag-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="60%"
    >
      <el-form ref="form" v-loading="loading" class="form-full" :model="formItem" label-width="120px" label-suffix=" : ">
        <el-row :gutter="15">
          <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.code')"
              prop="code"
              :rules="[
                { required: true, message: $t('ec.table.code') + $t('ec.table.emptyError'), trigger: 'blur' }
              ]"
            >
              <el-input v-model="formItem.code" type="text" :placeholder="$t('ec.table.code')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12"  v-if="enableTenant===true">
            <el-form-item
              :label="$t('ec.user.tenant')"
              prop="tenantId"
              :rules="[
              ]"
            >
              <ec-tenant-by-pro v-model="formItem.tenantId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.positionType')"
              prop="positionType"
              :rules="[
              ]"
            >
              <ec-select v-model="formItem.positionType" dict-type="positionType" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.sort')"
              prop="sort"
            >
              <el-input-number v-model="formItem.sort" type="number" :placeholder="$t('ec.table.sort')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="$t('ec.table.remarks')"
              prop="remarks"
              :rules="[
              ]"
            >
              <el-input v-model="formItem.remarks" type="textarea" :autosize="{minRows: 1, maxRows: 5}" :placeholder="$t('ec.table.remarks')" />
            </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 { findByPrimaryKey, save, update } from 'ecip-web/api/vue/position/sysrPositionApi'
const enableTenant = window._CONFIG['enableTenant'] === undefined ? true : window._CONFIG['enableTenant']
export default {
  name: 'WebPositionSysrPositionForm',
  data() {
    return {
      enableTenant,
      visible: false,
      loading: false, // 页面加载遮罩
      saveLoading: false, //  保存加载
      method: '',
      title: '',
      record: {},
      formItem: {
        code: '',
        name: '',
        positionType: '',
        tenantId: '',
        sort: '',
        remarks: ''
      }
    }
  },
  methods: {
    async init(method, record) {
      Object.assign(this.$data, this.$options.data())

      this.method = method
      this.record = record
      if (method === 'add') {
        this.title = this.$t('ec.table.add')
      } else if (method === 'edit') {
        this.title = this.$t('ec.table.edit')
      } else if (method === 'view') {
        this.title = this.$t('ec.table.view')
      }
      this.visible = true
      this.loading = false
      await this.$nextTick()
      this.$refs.form.clearValidate()
      if (method === 'edit' || method === 'view') { // 修改或者查看
        this.loading = true
        const { data } = await findByPrimaryKey(record.id)
        this.formItem = Object.assign({}, this.formItem, data)
        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.$notify.success(this.$t('ec.message.saveSuccess'))
            this.$emit('refreshData')
            this.visible = false
          } finally {
            this.saveLoading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
