
<template>
  <div>
    <!--对话框-->
    <el-dialog
      title="添加人员"
      :visible.sync="dialogVisible"
      width="900px"
      :close-on-click-modal="false"
      :before-close="close"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px"
        label-position="left"
      >
        <el-form-item
          label="人员类型"
          class="form-item-style"
          prop="personAdminType"
        >
          <el-radio-group
            v-model="form.personAdminType"
            @change="
              (val) => {
                changeType(val, 1);
              }
            "
          >
            <el-radio :label="1">机场公司人员</el-radio>
            <el-radio :label="2">机场公安局人员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="form.personAdminType == 1 ? '选择单位' : '选择部门'"
          class="form-item-style"
          prop="personAdminId"
        >
          <el-select
            filterable
            clearable
            v-model="form.personAdminId"
            placeholder="请选择"
            style="width: 100%"
            @change="
              (val) => {
                changrUnit(val, 1);
              }
            "
          >
            <el-option
              v-for="item in unit_list"
              :key="item.adminId"
              :label="item.username"
              :value="item.adminId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="选择人员"
          class="form-item-style"
          prop="userIdList"
        >
          <el-select
            multiple
            filterable
            clearable
            v-model="form.userIdList"
            placeholder="请选择人员"
            style="width: 100%"
          >
            <el-option
              v-for="item in user_list"
              :key="item.userId"
              :label="item.name"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close"> 取消 </el-button>
        <el-button type="primary" @click="addOrUpdate"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  auditAdminList,
  auditUserList,
  auditSecurityAdminList,
} from "@/api/audit";
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    form1: {
      type: Object,
      default: function () {
        return {};
      },
    },
    rowData: {
      type: Object,
      default: function () {
        return {};
      },
    },
    adminType: {
      type: Number,
      default: 1,
    },
  },
  components: {},
  filters: {},
  computed: {},
  data() {
    return {
      form: {
        personAdminType: 1,
        personAdminId: null,
        userIdList: [],
      },
      // 验证规则
      rules: {
        personAdminType: [
          { required: true, message: "请选择", trigger: "change" },
        ],
        personAdminId: [
          { required: true, message: "请选择", trigger: "change" },
        ],
        userIdList: [{ required: true, message: "请选择", trigger: "change" }],
      },

      unit_list: [],
      user_list: [],
    };
  },
  created() {
    this.form = Object.assign(this.form, this.form1);
    this.changeType(this.form.personAdminType);
    this.changrUnit(this.form.personAdminId);
  },
  watch: {
    dialogVisible: {
      handler: function (val) {
        this.$nextTick(() => {
          this.$refs["form"].clearValidate();
        });
      },
      immediate: false,
      deep: true,
    },
  },
  methods: {
    // 单位列表
    changeType(id, type) {
      console.log(id, type);
      if (type) {
        this.form.personAdminId = null;
        this.form.userIdList = [];
      }
      if (id) {
        if (this.adminType == 1) {
          auditAdminList(id).then((res) => {
            this.unit_list = res.data;
          });
        } else {
          auditSecurityAdminList(this.form.personAdminType==1?this.rowData.adminId:null).then((res) => {
            this.unit_list = res.data;
          });
        }
      }
    },
    changrUnit(id, type) {
      if (type) {
        this.form.userIdList = [];
      }
      if (id) {
        auditUserList(id).then((res) => {
          this.user_list = res.data;
        });
      }
    },
    addOrUpdate() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          this.$emit("getform", this.form, "user");
        }
      });
    },
    //关闭弹窗回调
    close() {
      this.$emit("close", false);
    },
  },
};
</script>
<style scoped lang="scss">
</style>
