<template>
  <div class="pageIndexClass">
    <div>
      <byTable :source="sourceList.data" :pagination="sourceList.pagination" :config="config" :loading="loading" highlight-current-row
               :selectConfig="selectConfig" :action-list="[
              {
                text: '添加AppSecret',
                action: () => openModal('add'),
                disabled: false,
              },
        ]" @get-list="getList">

        <template #name="{ item }">
          <div>
            <span class="el-click">{{ item.name }}</span>
          </div>
        </template>

      </byTable>
    </div>
    <el-dialog :title="modalType == 'add' ? '添加AppSecret' : '编辑AppSecret'" v-model="dialogVisible" width="600px" destroy-on-close>
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="formDom" v-loading="submitLoading">
      </byForm>
      <template #footer>
        <el-button @click="dialogVisible = false" size="defualt" v-debounce>取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="defualt" v-debounce :loading="submitLoading">
          确 定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
const { proxy } = getCurrentInstance();
const loading = ref(false);
const submitLoading = ref(false);
const sourceList = ref({
  data: [],
  pagination: {
    total: 3,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
  },
});
const treeData = ref([]);
const dialogVisible = ref(false);
const modalType = ref("add");
const selectConfig = computed(() => []);
const config = computed(() => {
  return [
    {
      attrs: {
        label: "部门名称",
        prop: "deptName",
        width: 130,
      },
    },
    {
      attrs: {
        label: "应用名称",
        prop: "companyName",
        "min-width": 150,
      },
    },
    {
      attrs: {
        label: "AppKey",
        prop: "appKey",
        width: 300,
      },
    },
    {
      attrs: {
        label: "AppSecret",
        prop: "appSecret",
        width: 300,
      },
    },
    {
      attrs: {
        label: "accessToken",
        prop: "accessToken",
        width: 300,
      },
    },
    {
      attrs: {
        label: "refreshToken",
        prop: "refreshToken",
        width: 300,
      },
    },
    // {
    //   attrs: {
    //     label: "备注",
    //     prop: "remark",
    //   },
    // },
    {
      attrs: {
        label: "操作",
        width: "120",
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "修改",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              getDtl(row);
            },
          },
          {
            attrs: {
              label: "删除",
              type: "danger",
              text: true,
            },
            el: "button",
            click() {
              proxy
                .msgConfirm()
                .then((res) => {
                  proxy
                    .post("/jushuitanConfig/delete", {
                      id: row.id,
                    })
                    .then((res) => {
                      proxy.msgTip("删除成功", 1);
                      getList();
                    });
                })
                .catch((err) => {});
            },
          },
        ];
      },
    },
  ];
});
const formData = reactive({
  data: {},
});
const formOption = reactive({
  inline: true,
  labelWidth: 110,
  itemWidth: 100,
});
const formDom = ref(null);
const formConfig = computed(() => {
  return [
    {
      type: "treeSelect",
      prop: "deptId",
      label: "部门",
      data: treeData.value,
      propsTreeLabel: "deptName",
      propsTreeValue: "deptId",
      itemWidth: 100,
    },
    {
      type: "input",
      prop: "companyName",
      label: "应用名称",
      itemWidth: 100,
      disabled: false,
    },
    {
      type: "input",
      prop: "appKey",
      label: "AppKey",
      itemWidth: 100,
      disabled: false,
    },
    {
      type: "input",
      prop: "appSecret",
      label: "AppSecret",
      itemWidth: 100,
      disabled: false,
    },
    {
      type: "input",
      prop: "accessToken",
      label: "accessToken",
      itemWidth: 100,
      disabled: false,
    },
    {
      type: "input",
      prop: "refreshToken",
      label: "refreshToken",
      itemWidth: 100,
      disabled: false,
    },
    {
      type: "input",
      itemType: "textarea",
      prop: "remark",
      label: "备注",
      itemWidth: 100,
      disabled: false,
    },
  ];
});
const rules = ref({
  deptId: [{ required: true, message: "请选择部门", trigger: "change" }],
  companyName: [{ required: true, message: "请输入公司名称", trigger: "blur" }],
  appKey: [{ required: true, message: "请输入AppKey", trigger: "blur" }],
  appSecret: [{ required: true, message: "请输入AppSecret", trigger: "blur" }],
  accessToken: [{ required: true, message: "accessToken", trigger: "blur" }],
  refreshToken: [
    { required: true, message: "请输入refreshToken", trigger: "blur" },
  ],
});

const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy
    .post("/jushuitanConfig/page", sourceList.value.pagination)
    .then((res) => {
      sourceList.value.data = res.rows;
      sourceList.value.pagination.total = res.total;
      setTimeout(() => {
        loading.value = false;
      }, 200);
    });
};

const openModal = () => {
  dialogVisible.value = true;
  modalType.value = "add";
  formData.data = {
    companyName: "",
    appKey: "",
    appSecret: "",
    remark: "",
  };
};

const submitForm = () => {
  formDom.value.handleSubmit((valid) => {
    submitLoading.value = true;
    proxy.post("/jushuitanConfig/" + modalType.value, formData.data).then(
      (res) => {
        proxy.msgTip("操作成功", 1);
        dialogVisible.value = false;
        submitLoading.value = false;
        getList();
      },
      (err) => {
        submitLoading.value = false;
      }
    );
  });
};

const getDtl = (row) => {
  modalType.value = "edit";
  proxy.post("/jushuitanConfig/detail", { id: row.id }).then((res) => {
    formData.data = res;
    dialogVisible.value = true;
  });
};

const arrayRecursion = (arr) => {
  arr.forEach((item) => {
    if (item.children && item.children.length > 0) {
      item.disabled = true;
      arrayRecursion(item.children);
    } else {
      item.disabled = false;
    }
  });
  return arr;
};

const getTreeData = () => {
  proxy
    .get("/tenantDept/list", {
      pageNum: 1,
      pageSize: 9999,
      keyword: "",
      tenantId: proxy.useUserStore().user.tenantId,
    })
    .then((res) => {
      treeData.value = proxy.handleTree(res.data, "deptId");
      arrayRecursion(treeData.value);
    });
};
getTreeData();
getList();
</script>

<style lang="scss" scoped>
.content {
  padding: 20px;
}
</style>
