<template>
  <div class="page_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="page_content flex_cloumn">
      <div class="content_top apaas_scroll">
        <div class="content_main">
          <systemForm
            ref="systemFormRef"
            :form-type="route.query.id ? true : false"
            :id="route.query.id"
            @action="getSystemFormData"></systemForm>
        </div>
      </div>
      <div class="content_bottom">
        <div>
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submit">保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRoute, useRouter } from "vue-router";
import systemForm from "./system-form.vue";
import { reactive, ref, onBeforeUnmount, onMounted } from "vue";
import axios from "@/request/http.js";
import CryptoJS from "crypto-js";
import { ElMessage } from "element-plus";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
const route = useRoute();
const router = useRouter();
const systemFormRef = ref(null);
//业务系统表单
const formData = reactive({
  organization_id: "",
  business_code: "",
  business_name: "",
  business_desc: "",
  logo: "",
  system_role_id: "",
  system_account: "",
  system_phone: "",
  password: "",
  confirm_password: "",
  access_address: "",
  develop_id: "",
  state: 1,
});

//业务系统表单检验触发事件 data为null 校验失败
const getSystemFormData = (data) => {
  if (data) {
    Object.assign(formData, data);
  } else {
  }
};
//提交表单
const submit = () => {
  systemFormRef.value.validateForm().then((res) => {
    if (!res) return;
    if (route.query.id) {
      let params = {
        ...systemFormRef.value.systemForm,
        logo:
          systemFormRef.value.systemForm.logo && systemFormRef.value.systemForm.logo.length > 0
            ? systemFormRef.value.systemForm.logo[0].url
            : "",
      };
      axios.put(`/v1/api/user/${route.query.id}`, params).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success(res.data.msg);
          cancel();
        } else {
          ElMessage.error(res.data.data);
        }
      });
    } else {
      let params = {
        ...systemFormRef.value.systemForm,
        logo:
          systemFormRef.value.systemForm.logo && systemFormRef.value.systemForm.logo.length > 0
            ? systemFormRef.value.systemForm.logo[0].url
            : "",
        password: CryptoJS.AES.encrypt(systemFormRef.value.systemForm.password, "swuE9cmCZQwrkYRV").toString(),
      };
      delete params.confirm_password;
      axios.post(`/v1/api/user/add`, params).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success(res.data.msg);
          cancel();
        } else {
          ElMessage.error(res.data.data);
        }
      });
    }
  });
};
//取消
const cancel = () => {
  router.push({
    path: "/authority/user",
    query: {
      organization_id: systemFormRef.value.systemForm.organization_id,
    },
  });
};

const getDetail = () => {
  axios.get(`/v1/api/user/${route.query.id}`).then((res) => {
    if (res.data.code == 200) {
      const form = res.data.data;
      systemFormRef.value.setForm({
        organization_id: form.organization_id,
        select_role: form.system_role,
        logo: form.logo ? [{ url: form.logo }] : [],
        name: form.name,
        system_account: form.system_account,
        phone: form.phone,
        state: form.state,
      });
    } else {
      ElMessage.error(res.data.data);
    }
  });
};

onBeforeUnmount(() => {});
onMounted(() => {
  if (route.query.id) {
    getDetail();
  }
});
</script>

<style scoped>
.flex_cloumn {
  display: flex;
  flex-direction: column;
}
.content_top {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: auto;
}
/* .content_top::-webkit-scrollbar {
  width: 5px;
  height: 0px;
}
.content_top::-webkit-scrollbar-thumb {
  background: #dedede;
  border-radius: 10px;
  height: 0px;
}
.content_top::-webkit-scrollbar-track {
  background: transparent;
  border-radius: 2px;
} */

.content_bottom {
  height: 68px;
  line-height: 68px;
  text-align: right;
  padding: 0 20px;
  border-top: 1px solid #e6e9ef;
}
.content_process {
  border-bottom: 1px solid #e6e9ef;
  padding: 38px 16%;
}
.content_main {
  flex: 1;
  padding: 30px;
  box-sizing: border-box;
}
.process_desc {
  background-color: #fff;
  display: inline-block;
  position: absolute;
  padding: 0 5px;
  font-size: 18px;
  color: #202531;
  font-weight: bold;
}
.process_desc img {
  height: 25px;
  width: 25px;
  vertical-align: middle;
}
.process_end {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.process_end div {
  text-align: center;
}
.step_icon {
  color: #fff;
  font-weight: 600;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.icon_box {
  display: inline-block;
  background-color: #2b4695;
  width: 48px;
  height: 48px;
  border-radius: 99px;
  position: relative;
  border: 3px solid #b0bee8;
  vertical-align: middle;
}
.circle {
  display: inline-block;
  background-color: #a9b1c7;
  width: 24px;
  height: 24px;
  border-radius: 99px;
  position: relative;
  border: 4px solid #e6e9ef;
  vertical-align: middle;
}
.content_process :deep() .el-step.is-horizontal .el-step__line {
  height: 4px;
}
.content_process :deep() .el-step__head.is-finish .el-step__line {
  background: linear-gradient(to right, #2b4695 50%, #e6e9ef 50%);
}
.process_complete :deep() .el-step__head.is-finish .el-step__line {
  background: linear-gradient(to right, #2b4695 100%, #e6e9ef 0%);
}
</style>
