<template>
  <el-dialog :title="position && position.name + $t('ec.position.user')" :visible.sync="visible"   top="4%">
    <el-form v-loading="loading">
      <ec-user-modal v-model="userList"  multiple :str-result="false" />
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('ec.table.cancel') }}</el-button>
      <el-button type="primary" :loading="saveLoading" @click="save">{{ $t('ec.table.confirm') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { findUserByPosition, saveByPosition } from 'ecip-web/api/vue/position/sysrPositionApi'

export default {
  name: 'PositionUserModal',
  data() {
    return {
      visible: false,
      loading: false,
      saveLoading: false,
      position: {},
      userList: []
    }
  },
  methods: {
    async init(position) {
      this.visible = true
      this.position = position
      this.userList = []
      this.$nextTick(() => {
        if (position && position.id) {
          this.getPositionUser(position.id)
        } else {
        }
      })
    },
    async getPositionUser(id) {
      try {
        this.loading = true
        const {data} = await findUserByPosition(id)
        this.userList = data
      } finally {
        this.loading = false
      }
    },
    save() {
      this.$confirm(this.$t('ec.position.relateConfirm') + ':' + this.position.name, this.$t('ec.message.prompt'), {
        confirmButtonText: this.$t('ec.message.confirm'),
        cancelButtonText: this.$t('ec.message.cancel'),
        type: 'warning'
      }).then(async () => {
        try {
          this.saveLoading = true
          await saveByPosition({positionId: this.position.id}, this.userList)
          this.$notify.success(this.$t('ec.position.relateSuccess'))
          this.visible = false
        } finally {
          this.saveLoading = false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
