<template>
  <div>
    <el-card class="box-card">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        highlight-current-row
        :action-list="[
          {
            text: '添加合同模板',
            action: () => clickModal(),
          },
        ]"
        @get-list="getList"
        @clickReset="clickReset">
        <template #contactPerson="{ item }">
          <div>
            {{ item.contactPerson1 }} {{ item.contactNumber1 }}, {{ item.contactPerson2 }} {{ item.contactNumber2 }}, {{ item.contactPerson3 }}
            {{ item.contactNumber3 }}
          </div>
        </template>
      </byTable>
    </el-card>

    <el-dialog :title="modalType == 'add' ? '添加合同模板' : '编辑合同模板'" v-if="openDialog" v-model="openDialog" width="60%">
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit">
        <template #content>
          <div style="width: 100%">
            <Editor :value="formData.data.content" @updateValue="updateValue" ref="editor" />
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="openDialog = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="large" v-preReClick>确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import byForm from "/src/components/byForm/index";
import { ElMessage, ElMessageBox } from "element-plus";
import Editor from "/src/components/Editor/index.vue";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
  },
});
const loading = ref(false);
const config = computed(() => {
  return [
    {
      attrs: {
        label: "合同模板名称",
        prop: "name",
        align: "center",
      },
    },
    {
      attrs: {
        label: "操作",
        width: 120,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "编辑",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickUpdate(row);
            },
          },
          {
            attrs: {
              label: "删除",
              type: "danger",
              text: true,
            },
            el: "button",
            click() {
              clickDelete(row);
            },
          },
        ];
      },
    },
  ];
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/contractTemplate/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const modalType = ref("add");
const openDialog = ref(false);
const submit = ref(null);
const formOption = reactive({
  inline: true,
  labelWidth: "120px",
  itemWidth: 100,
  rules: [],
  labelPosition: "right",
});
const formData = reactive({
  data: {},
});
const formConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "name",
      label: "合同模板名称",
      itemType: "text",
    },
    {
      type: "slot",
      slotName: "content",
      label: "合同模板内容",
    },
  ];
});
const rules = ref({
  name: [{ required: true, message: "请输入合同模板名称", trigger: "blur" }],
  content: [{ required: true, message: "请输入合同模板内容", trigger: "blur" }],
});
const clickModal = () => {
  modalType.value = "add";
  formData.data = {
    content: "",
  };
  openDialog.value = true;
};
const updateValue = (val) => {
  formData.data.content = val;
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    proxy.post("/contractTemplate/" + modalType.value, formData.data).then(() => {
      ElMessage({
        message: modalType.value == "add" ? "添加成功" : "编辑成功",
        type: "success",
      });
      openDialog.value = false;
      getList();
    });
  });
};
const clickUpdate = (row) => {
  modalType.value = "edit";
  openDialog.value = true;
  proxy.post("/contractTemplate/detail", { id: row.id }).then((res) => {
    formData.data = res;
    proxy.$refs.editor.changeHtml(formData.data.content);
  });
};
const clickDelete = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/contractTemplate/delete", { id: row.id }).then(() => {
        ElMessage({ message: "删除成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
</script>

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