<template>
  <div class="tenant">
    <div class="content">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :selectConfig="selectConfig"
        highlight-current-row
        :action-list="[
          {
            text: '添加节点',
            action: () => openModal(),
          },
        ]"
        @get-list="getList"
      >
      </byTable>
    </div>

    <el-dialog
      :title="modalType == 'add' ? '添加节点' : '编辑节点'"
      v-if="dialogVisible"
      v-model="dialogVisible"
      width="500"
      v-loading="loadingDialog"
    >
      <byForm
        :formConfig="formConfig"
        :formOption="formOption"
        v-model="formData.data"
        :rules="rules"
        ref="submit"
      >
        <!-- <template #balance>
            <div style="width: 100%">
              <el-button type="primary" @click="clickBalance">添加</el-button>
            </div>
          </template> -->
      </byForm>
      <template #footer>
        <el-button @click="dialogVisible = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="large"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
import useUserStore from "@/store/modules/user";
import { ElMessage, ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();
const documentaryType = ref([]);
const documentaryBind = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: "",
    keyword: "",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "跟单类型",
      prop: "type",
      data: documentaryType.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "跟单类型",
        prop: "type",
        width: 160,
      },
      render(type) {
        let text = "";
        if (documentaryType.value && documentaryType.value.length > 0) {
          let data = documentaryType.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "节点名称",
        prop: "name",
        width: 240,
      },
    },
    {
      attrs: {
        label: "节点排序",
        prop: "sort",
        width: 140,
      },
    },
    {
      attrs: {
        label: "绑定类型",
        prop: "bind",
        width: 160,
      },
      render(type) {
        let text = "";
        if (documentaryBind.value && documentaryBind.value.length > 0) {
          let data = documentaryBind.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "备注",
        prop: "remark",
      },
    },
    {
      attrs: {
        label: "操作",
        width: "120",
        align: "center",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "修改",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              update(row);
            },
          },
          {
            attrs: {
              label: "删除",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              ElMessageBox.confirm(
                "此操作将永久删除该数据, 是否继续?",
                "提示",
                {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
                }
              ).then(() => {
                proxy
                  .post("/documentary/delete", {
                    id: row.id,
                  })
                  .then(() => {
                    ElMessage({
                      message: "删除成功",
                      type: "success",
                    });
                    getList();
                  });
              });
            },
          },
        ];
      },
    },
  ];
});
const getDict = () => {
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      dictCode: "documentary_type",
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        documentaryType.value = res.rows.map((item) => {
          return {
            label: item.dictValue,
            value: item.dictKey,
          };
        });
      }
    });
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      dictCode: "documentary_bind",
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        documentaryBind.value = res.rows.map((item) => {
          return {
            label: item.dictValue,
            value: item.dictKey,
          };
        });
      }
    });
};
getDict();
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/documentary/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const modalType = ref("add");
const dialogVisible = ref(false);
const loadingDialog = ref(false);
const submit = ref(null);
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const formConfig = computed(() => {
  return [
    {
      type: "select",
      prop: "type",
      label: "跟单类型",
      data: documentaryType.value,
    },
    {
      type: "input",
      prop: "name",
      label: "节点名称",
    },
    {
      type: "number",
      prop: "sort",
      label: "节点排序",
      precision: 0,
      max: 9999,
      controls: false,
    },
    {
      type: "select",
      prop: "bind",
      label: "绑定类型",
      data: documentaryBind.value,
    },
    {
      type: "input",
      prop: "remark",
      label: "备注",
      itemType: "textarea",
    },
  ];
});
const rules = ref({
  type: [{ required: true, message: "请选择跟单类型", trigger: "change" }],
  name: [{ required: true, message: "请输入节点名称", trigger: "blur" }],
  sort: [{ required: true, message: "请输入节点排序", trigger: "blur" }],
  // bind: [{ required: true, message: "请输入绑定功能", trigger: "blur" }],
});
const formData = reactive({
  data: {},
});
const openModal = () => {
  modalType.value = "add";
  formData.data = {};
  loadingDialog.value = false;
  dialogVisible.value = true;
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    loadingDialog.value = true;
    proxy.post("/documentary/" + modalType.value, formData.data).then(
      () => {
        ElMessage({
          message: modalType.value == "add" ? "添加成功" : "编辑成功",
          type: "success",
        });
        dialogVisible.value = false;
        getList();
      },
      (err) => {
        console.log(err);
        loadingDialog.value = false;
      }
    );
  });
};
const update = (row) => {
  modalType.value = "edit";
  loadingDialog.value = true;
  proxy.post("/documentary/detail", { id: row.id }).then((res) => {
    res.type = res.type + "";
    res.bind = res.bind + "";
    formData.data = res;
    loadingDialog.value = false;
  });
  dialogVisible.value = true;
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
