<template>
  <ele-modal
    width="30%"
    :visible.sync="visible"
    custom-class="ele-dialog-form"
    :before-close="cancel"
    :title="`设置${title}`"
    :maxable="true"
  >
    <el-form
      label-width="80px"
      :model="addForm"
      ref="form"
      class="ele-form-search"
    >
      <el-form-item :label="title" required>
        <DeptSelect v-model="addForm.id" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit" :loading="loading"
        >确 定</el-button
      >
    </span>
  </ele-modal>
</template>

<script>
  import DeptSelect from '@/components/CommomSelect/dept-select.vue';
  import { updateOwnershipDepartment, updateUnitUse } from '@/api/main/index';
  export default {
    components: { DeptSelect },
    data() {
      return {
        title: '所属部门',
        visible: false,
        loading: false,
        addForm: {
          id: ''
        },
        type: 1,
        ids: []
      };
    },
    methods: {
      cancel() {
        this.addForm.id = '';
        this.ids = [];
        this.visible = false;
      },
      open(type, ids) {
        this.ids = ids;
        this.type = type;
        this.title = type == 1 ? '所属部门' : '所属单位';
        this.visible = true;
      },
      submit() {
        if (!this.addForm.id) {
          return this.$message.warning(`请先选择${this.title}`);
        }
        let params = {
          id: this.ids
        };
        if (this.type == 1) {
          params.ownershipGroupId = this.addForm.id;
        } else {
          params.postId = this.addForm.id;
        }
        const api = this.type == 1 ? updateOwnershipDepartment : updateUnitUse;
        this.loading = true;
        api(params)
          .then((res) => {
            this.loading = false;
            if (res) {
              this.$message.success('操作成功');
              this.cancel();
              this.$emit('success',true);
            }
          })
          .catch((err) => {
            this.loading = false;
          });
      }
    }
  };
</script>
