<script lang="ts" setup>

import { ElMessage } from 'element-plus'
import { newOrganization,editOrganization } from '@/api/system/organization' 
import { noEmpty } from '../check'

const props = defineProps<{
  data?: any[]
}>()

const emits = defineEmits(['update'])

const state = ref(false)
const type = ref<any>(null)
const params = reactive({
  parentId: '',
  name: '',
  alias: '',
  code: '',
  adminName: '',
  sort: null,
  type: null,
  level: '',
  inGroup: '',
  plateId: null,
  remarks: '',
  id:''
})

const formRef = ref(null)
const rules = reactive<any>({
  name: [{ required: true, validator: noEmpty }],
  alias: [{ required: true, validator: noEmpty }],
  code: [{ required: true, validator: noEmpty }],
  adminName: [{ required: true, validator: noEmpty }],
  sort: [{ required: true, validator: noEmpty }],
  level: [{ required: true, validator: noEmpty }],
  type: [{ required: true, validator: noEmpty }],
  inGroup: [{ required: true, validator: noEmpty }],
  plateId: [{ required: true, validator: noEmpty }],
  remarks: ''
})

const popName = computed(() => {
  return {
    check: '查看', edit: '编辑', new: '新增'
  }[type.value]
})

const diaOpen = (xtype, e?) => {
  if (e) {
    for (let key in params) {
      if (e[key] || e[key] === 0) {
        params[key] = e[key]
      }
    }
  }
  type.value = xtype
  state.value = true
}

const onClosed = () => {
  // 数据清除
  for (let key in params) {
    params[key] = ''
  }
}

// 提交
const onSubmit = async () => {

  const pid = Array.isArray(params.parentId) ? params.parentId.slice(-1)[0] : params.parentId

  const fmtPms = {
    ...params,
    parentId: pid || '0' // 顶层补位
  }

  let res ;
   if (type.value == 'edit') {

   res =  await editOrganization(fmtPms)

  } else {
    res = await newOrganization(fmtPms)
  }
// = await newOrganization(fmtPms)
  // editOrganization

  if (res) {
    ElMessage.success(type.value === 'edit' ? '修改成功！' : '创建成功！')
    state.value = false
    emits('update')
  }
  
}

const goClose = () => {
  state.value = false
}

defineExpose({
  diaOpen
})
</script>

<template>

<el-dialog
  class="org-model"
  v-model="state"
  width="1112"
  :destroy-on-close="true"
  :align-center="true"
  @closed="onClosed"
>

  <div class="title">
    <div class="text">{{ popName }}组织</div>
    <Icon @click="goClose" class="close" icon="ep:close" />
  </div>

  <div :class="['content', { 'disabled': type === 'check' }]">

    <el-form ref="formRef" :model="params" :rules :show-message="false">
      <div class="block-container">
        <el-form-item prop="parentId" label="上级组织：">
          <el-cascader
            class="def"
            v-model="params.parentId"
            :options="data"
            :props="{
              value: 'id',
              label: 'name',
              checkStrictly: true
            }"
            :teleported="false"
            @change="handleChange"
          />
        </el-form-item>

        <el-form-item prop="name" label="组织名称：">
          <el-input v-model="params.name" placeholder="请输入..." />
        </el-form-item>

        <el-form-item prop="alias" label="组织别名：">
          <el-input v-model="params.alias" placeholder="请输入..." />
        </el-form-item>

        <el-form-item prop="code" label="组织编码：">
          <el-input v-model="params.code" placeholder="请输入..." />
        </el-form-item>

        <el-form-item prop="adminName" label="管理处：">
          <el-input v-model="params.adminName" placeholder="请输入..." />
        </el-form-item>

        <el-form-item prop="sort" label="排序：">
          <el-input type='number' v-model="params.sort" placeholder="请输入..." />
        </el-form-item>

        <el-form-item prop="type" label="组织类型：">
          <el-input v-model="params.type" placeholder="请输入..." />
        </el-form-item>

        <el-form-item prop="level" label="组织级别：">
          <el-input type='number' v-model="params.level" placeholder="请输入..." />
        </el-form-item>

        <el-form-item prop="inGroup" label="集团内外：">
          <el-input v-model="params.inGroup" placeholder="请输入..." />
        </el-form-item>

        <el-form-item prop="plateId" label="所属版块：">
          <el-input v-model="params.plateId" placeholder="请输入..." />
        </el-form-item>

        <el-form-item label="备注：">
          <el-input type="textarea" rows="5" v-model="params.remarks" placeholder="请输入..." />
        </el-form-item>

      </div>
    </el-form>

  </div>

  <template #footer>
    <div class="footer">
      <el-button v-if="type === 'check'" type="primary" @click="goClose">关闭</el-button>
      <template v-else>
        <el-button @click="goClose">取消</el-button>
        <el-button @click="onSubmit" type="primary">确定</el-button>
      </template>
    </div>
  </template>

  </el-dialog>

</template>

<style lang="scss" scoped>
$base_url: '@/assets/images/home/';

.title {
  height: 48px;
  display: flex;
  align-content: center;
  align-items: center;
  color: white;
  padding: 0 20px;
  font-size: 18px;
  font-weight: 600;
  justify-content: space-between;
  .close {
    cursor: pointer;
  }
}

.content {
  padding: 0 16px;
  // height: 600px;

  .block-container {
    padding-right: 24px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin-top: 20px;
  }

  &.disabled {
    :deep(.base-block) {
      pointer-events: none;
    }
    :deep(.road-setting) {
      pointer-events: none;
    }
    :deep(.el-input) {
      background: rgba($color: #ffffff, $alpha: 0.2);
    }
  }
}

.footer {
  // background: rgba(69, 116, 182, 0.36);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 20px 24px;
  color: white;
}
</style>

<style lang="scss">
.org-model {
  padding: 0;
  box-sizing: border-box;
  // border: 1px solid rgba(255, 255, 255, 0.5);
  background: #0d3561;
  border-radius: 4px;

  .el-dialog__header {
    display: none;
  }
  .el-dialog__footer {
    padding-top: 0;
  }
  .def-btn {
    background: #1270ff;
    border: 1px solid rgba(155, 193, 255, 0.74);
    height: 28px;
    padding: 8px 10px;
    color: white;
    &:hover {
      border: 1px solid rgba(255, 255, 255, 0.8);
    }
  }
}
</style>
