<template>
  <el-dialog
    ref="dialog"
    :title="(isEdit ? '编辑' : '新增') + '机构'"
    width="480px"
    :visible.sync="visible"
    @close="closeDialog"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-position="right"
      label-width="140px"
    >
      <el-row>
        <el-form-item label="机构名称" prop="name">
          <el-input
            class="pull_length"
            v-model="form.name"
            placeholder="机构名称"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="机构编码" prop="code">
          <el-input
            class="pull_length"
            v-model="form.code"
            placeholder="机构编码"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="负责人" prop="contactName">
          <el-input
            class="pull_length"
            v-model="form.contactName"
            placeholder="负责人"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="联系方式" prop="contactPhone">
          <el-input
            class="pull_length"
            v-model="form.contactPhone"
            placeholder="联系方式"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="logo" prop="logo">
          <UploadFile v-model="form.logo" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="是否启用模考" prop="enableSimulate">
          <el-radio-group class="pull_right_sm" v-model="form.enableSimulate">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="是否视频回放" prop="enableMonitorRecord">
          <el-radio-group
            class="pull_right_sm"
            v-model="form.enableMonitorRecord"
          >
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="是否使用活体检测" prop="enableLiveness">
          <el-radio-group class="pull_right_sm" v-model="form.enableLiveness">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="状态" prop="enable">
          <el-radio-group class="pull_right_sm" v-model="form.enable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row class="d-flex justify-content-center">
        <el-button type="primary" @click="submitForm" :loading="loading"
          >保 存</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { searchOrg, saveOrg } from "@/api/system-org";
import UploadFile from "./UploadFile";
export default {
  name: "OrgManagementDialog",
  props: {
    org: Object,
  },
  components: { UploadFile },
  computed: {
    isEdit() {
      return this.org.id;
    },
  },
  data() {
    return {
      visible: false,
      form: {},
      rules: {},
      loading: false,
    };
  },
  watch: {
    org: {
      immediate: true,
      handler: async function () {
        if (this.isEdit) {
          const res = await searchOrg(this.org.id);
          this.form = res.data.data[0];
        } else {
          this.form = {
            name: "",
            code: "",
            contactName: "",
            contactPhone: "",
            logo: null,
            enableSimulate: 0,
            enableMonitorRecord: 0,
            enableLiveness: 0,
            enable: 0,
          };
        }
      },
    },
  },
  methods: {
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    async submitForm() {
      let data = this.form;
      if (this.isEdit) {
        data = { ...data, id: this.org.id };
      }
      try {
        this.loading = true;
        await saveOrg(data);
        this.$emit("reload");
        this.$notify({ title: "保存成功", type: "success" });
        this.closeDialog();
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style></style>
