<!-- @format -->

<template>
  <div class="main">
    <div class="left">
      <div class="ns-table-title">部门管理</div>
      <a-row>
        <a-col :span="12" class="tree">
          <ns-button style="margin: 10px" type="primary" @click="addApartment">新增部门</ns-button>
          <ns-button type="primary" @click="addApartmentSon">新增子部门</ns-button>
          <ns-button style="margin: 10px" type="primary" @click="deleteApartment">删除</ns-button>
          <a-tree :tree-data="apartmentTreeData" @select="SelectApartmentTree">
            <template #title="{ title }">
              {{ title }}
            </template>
          </a-tree>
        </a-col>
        <a-col :span="12" class="list">
          <a-tabs v-model:activeKey="activeKey">
            <a-tab-pane key="1" tab="部门信息">
              <ns-form :schemas="formSchema" :model="formData" formLayout="vertical" class="form" />
            </a-tab-pane>
            <a-tab-pane key="2" tab="部门权限">
              <a-tree
                :tree-data="apartmentAdminTreeData"
                v-model:checkedKeys="ApartcheckedKeys"
                checkable
                @select="ApartmentSelect">
                <template #title="{ title }">
                  {{ title }}
                </template>
              </a-tree>
            </a-tab-pane>
          </a-tabs>
          <ns-button style="margin: 20px" type="primary" @click="CancelApartment">取消</ns-button>
          <ns-button type="primary" @click="ApartmentSure">{{
            disabled ? '编辑' : '确定'
          }}</ns-button>
        </a-col>
      </a-row>
    </div>
    <div class="right">
      <div class="ns-table-title">角色管理</div>
      <a-row>
        <a-col :span="12" class="tree">
          <ns-button style="margin: 10px" type="primary" @click="addUser">新增角色</ns-button>
          <ns-button type="primary" @click="addUserSon">新增子角色</ns-button>
          <ns-button style="margin: 10px" type="primary" @click="deleteUser">删除</ns-button>
          <a-tree :tree-data="userTreeData" @select="SelectUserTree">
            <template #title="{ title }">
              {{ title }}
            </template>
          </a-tree>
        </a-col>
        <a-col :span="12" class="list">
          <a-tabs v-model:activeKey="activeKey2">
            <a-tab-pane key="1" tab="角色信息">
              <ns-form
                :schemas="formSchema2"
                :model="formData2"
                formLayout="vertical"
                class="form" />
            </a-tab-pane>
            <a-tab-pane key="2" tab="角色权限">
              <a-tree
                :tree-data="userAdminTreeData"
                v-model:checkedKeys="UsercheckedKeys"
                checkable
                @select="UserSelect">
                <template #title="{ title }">
                  {{ title }}
                </template>
              </a-tree>
            </a-tab-pane>
          </a-tabs>
          <ns-button style="margin: 20px" type="primary" @click="CancelUser">取消</ns-button>
          <ns-button type="primary" @click="UserSure">{{ disabled2 ? '编辑' : '确定' }}</ns-button>
        </a-col>
      </a-row>
    </div>
  </div>
</template>
<script lang="ts">
  import { createVNode, defineComponent, reactive, ref, watch } from 'vue';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { http } from '/nerv-lib/util/http';
  import { uuid } from '@antv/x6/lib/util/string/uuid';
  export default defineComponent({
    name: 'AuthorityManageIndex',
    setup() {
      let formData = ref({});
      let formData2 = ref({});
      const activeKey = ref('1');
      const activeKey2 = ref('1');
      const disabled = ref(true);
      const disabled2 = ref(true);
      const ApartcheckedKeys = ref<string[]>([]);
      const UsercheckedKeys = ref<string[]>([]);

      watch(ApartcheckedKeys, () => {
        console.log('checkedKeys', ApartcheckedKeys.value);
      });
      watch(UsercheckedKeys, () => {
        console.log('checkedKeys', UsercheckedKeys.value);
      });

      const opMap: any = {
        type: 'add',
        fuc: () => {},
        record: {},
      };

      const apartmentTreeData = ref([
        {
          title: '铁路总局',
          key: '0-0',
          children: [
            { title: '济阳站', key: '0-0-0' },
            { title: '临沂站', key: '0-0-1' },
          ],
        },
      ]);

      const userTreeData = ref([
        {
          title: '产品总监',
          key: '0-0',
          children: [
            { title: '产品经理', key: '0-0-0' },
            { title: '产品助理', key: '0-0-1' },
          ],
        },
      ]);

      const apartmentAdminTreeData = [
        {
          title: '首页',
          key: '0-0',
          children: [
            {
              title: '控制台',
              key: '0-0-0',
              children: [
                { title: 'leaf', key: '0-0-0-0' },
                { title: 'leaf', key: '0-0-0-1' },
              ],
            },
            {
              title: '用户管理',
              key: '0-0-1',
              children: [{ key: '0-0-1-0', title: 'sss' }],
            },
          ],
        },
      ];

      const userAdminTreeData = ref([
        {
          title: '产品总监',
          key: '0-0',
          children: [
            { title: '产品经理1', key: '0-0-0' },
            { title: '产品经理2', key: '0-0-1' },
          ],
        },
      ]);

      const addApartment = () => {
        disabled.value = false;
        opMap.type = 'add';
      };

      const addApartmentSon = () => {
        disabled.value = false;
        opMap.type = 'addson';
      };

      const addUser = () => {
        disabled2.value = false;
      };

      const addUserSon = () => {
        disabled.value = false;
      };

      const deleteApartment = () => {
        Modal.confirm({
          title: '是否确认删除',
          icon: createVNode(ExclamationCircleOutlined),
          content: createVNode('div', { style: 'color:red;' }, ''),
          onOk() {},
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });
      };

      const deleteUser = () => {
        Modal.confirm({
          title: '是否确认删除',
          icon: createVNode(ExclamationCircleOutlined),
          content: createVNode('div', { style: 'color:red;' }, ''),
          onOk() {},
          onCancel() {
            console.log('Cancel');
          },
          class: 'test',
        });
      };

      const SelectApartmentTree = (selectedKeys: any, info: any) => {
        disabled.value = false;
        console.log(selectedKeys, 'selectedKeys');
        console.log(info, 'info');
      };
      const SelectUserTree = (selectedKeys: any, info: any) => {
        disabled2.value = false;
        console.log(selectedKeys, 'selectedKeys');
        console.log(info, 'info');
      };

      const CancelApartment = () => {
        disabled.value = true;
      };

      const CancelUser = () => {
        disabled2.value = true;
      };

      const ApartmentSelect = (selectedKeys: any, info: any) => {
        console.log(selectedKeys, 'selectedKeys');
        console.log(info, 'info');
      };

      const UserSelect = (selectedKeys: any, info: any) => {
        console.log(selectedKeys, 'selectedKeys');
        console.log(info, 'info');
      };

      const ApartmentSure = () => {
        disabled.value = !disabled.value;
        opMap.fuc && opMap.fuc(formData.value);
        // console.log(formData.value.name, 'formData');
        console.log(opMap.type, 'opMap.type');
        if (opMap.type === 'add') {
          apartmentTreeData.value[0].children.push({
            title: formData.value.name,
            key: 'a1',
          });
        } else {
          console.log('ss');
        }
      };
      const UserSure = () => {
        disabled2.value = !disabled2.value;
        opMap.fuc && opMap.fuc(formData2.value);
        console.log(formData2.value, 'formData2');
        userTreeData.value[0].children.push({
          title: formData2.value.departname,
          key: 'a2',
        });
      };

      const formSchema = reactive([
        {
          field: 'field111',
          component: 'NsChildForm',
          componentProps: {
            schemas: [
              {
                label: '部门名称',
                field: 'name',
                component: 'NsInput',
                componentProps: {
                  disabled: disabled,
                  placeholder: '请输入部门名称',
                },
                rules: [
                  {
                    required: false,
                    message: '请输入部门名称',
                  },
                ],
              },
              {
                label: '上级部门',
                field: 'department',
                component: 'NsSelect',
                componentProps: {
                  disabled: disabled,
                  placeholder: '请选择上级部门',
                  options: [
                    {
                      label: '部门1',
                      value: 1,
                    },
                    {
                      label: '部门2',
                      value: 2,
                    },
                  ],
                },
              },
              {
                label: '部门编码',
                field: 'code',
                component: 'NsInput',
                componentProps: {
                  disabled: disabled,
                  placeholder: '请输入部门编码',
                },
                rules: [
                  {
                    required: false,
                    message: '请输入部门编码',
                  },
                ],
              },
              {
                label: '排序',
                field: 'order',
                component: 'NsInput',
                componentProps: {
                  disabled: disabled,
                  placeholder: '请输入排序',
                },
                rules: [
                  {
                    required: false,
                    message: '请输入排序',
                  },
                ],
              },
              {
                field: 'remark',
                label: '备注',
                component: 'NsTextarea',
                componentProps: {
                  disabled: disabled,
                  placeholder: '请输入',
                },
              },
            ],
          },
        },
      ]);
      const formSchema2 = reactive([
        {
          field: 'field111',
          component: 'NsChildForm',
          componentProps: {
            schemas: [
              {
                label: '角色名称',
                field: 'departname',
                component: 'NsInput',
                componentProps: {
                  disabled: disabled2,
                  placeholder: '请输入角色名称',
                },
                rules: [
                  {
                    required: false,
                    message: '请输入角色名称',
                  },
                ],
              },
              {
                label: '上级领导',
                field: 'department',
                component: 'NsSelect',
                componentProps: {
                  disabled: disabled2,
                  placeholder: '请选择上级领导',
                  options: [
                    {
                      label: '领导1',
                      value: 1,
                    },
                    {
                      label: '领导2',
                      value: 2,
                    },
                  ],
                },
              },
              {
                label: '节点编码',
                field: 'code',
                component: 'NsInput',
                componentProps: {
                  disabled: disabled2,
                  placeholder: '请输入节点编码',
                },
                rules: [
                  {
                    required: false,
                    message: '请输入节点编码',
                  },
                ],
              },
              {
                label: '是否部门领导',
                field: 'isleader',
                component: 'NsRadioGroup',
                componentProps: {
                  disabled: disabled2,
                  radioType: 'radio',
                  options: [
                    { label: '是', value: 1 },
                    { label: '否', value: 2 },
                  ],
                  rules: [
                    {
                      required: false,
                      message: '请选择是否部门领导',
                    },
                  ],
                },
              },
              {
                field: 'remark',
                label: '备注',
                component: 'NsTextarea',
                componentProps: {
                  disabled: disabled2,
                  placeholder: '请输入',
                },
              },
            ],
          },
        },
      ]);
      return {
        disabled,
        disabled2,
        formSchema,
        formSchema2,
        formData,
        formData2,
        apartmentTreeData,
        userTreeData,
        SelectApartmentTree,
        SelectUserTree,
        activeKey,
        activeKey2,
        ApartmentSure,
        UserSure,
        addApartment,
        addApartmentSon,
        deleteApartment,
        deleteUser,
        addUser,
        addUserSon,
        CancelApartment,
        CancelUser,
        apartmentAdminTreeData,
        ApartmentSelect,
        ApartcheckedKeys,
        userAdminTreeData,
        UsercheckedKeys,
        UserSelect,
      };
    },
  });
</script>
<style scoped>
  .main {
    display: flex;
    flex: 1;
  }
  .left {
    width: 50%;
    height: calc(100vh-50px);
    border-right: 5px solid rgb(229, 235, 240);
  }
  .tree {
    width: 400px;
    height: 89vh;
    border-right: 2px solid rgb(229, 235, 240);
  }
  .right {
    width: 50%;
  }

  .top {
    height: 50vh;
    border-bottom: 5px solid rgb(229, 235, 240);
  }
  .ns-table-title {
    text-align: left;
    height: 46px;
    line-height: 46px;
    font-size: 18px;
    font-weight: bold;
    user-select: text;
    padding-left: 16px;
    width: 100%;
    border-bottom: 2px solid rgb(229, 235, 240);
  }
  .table {
    width: 2000px;
  }
  .admin {
    text-align: left;
    height: 42px;
    line-height: 42px;
    font-size: 16px;
    font-weight: bold;
    user-select: text;
    padding-left: 16px;
    width: calc(100% + 32px);
  }
  .form {
    margin: 20px;
  }
</style>
