<template>
  <div class="pageIndexClass">
    <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="800" v-loading="loadingOperation">
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit">
        <template #templateContent>
          <div style="width: 100%">
            <Editor :value="formData.data.templateContent" @updateValue="updateContent" />
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="dialogVisible = false" size="default">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="default">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

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

const { proxy } = getCurrentInstance();
const loading = ref(false);
const companyList = ref([]);
const typeData = ref([
  {
    dictKey: 1,
    dictValue: "销售合同",
  },
  {
    dictKey: 2,
    dictValue: "采购合同",
  },
]);

const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    templateType: "",
    keyword: "",
  },
});
const selectConfig = computed(() => {
  return [
    {
      label: "模板类型",
      prop: "templateType",
      data: typeData.value,
    },
    // {
    //   label: "公司名称",
    //   prop: "corporationId",
    //   data: companyList.value,
    // },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "模板类型",
        prop: "templateType",
      },
      render(val) {
        return proxy.dictKeyValue(val, typeData.value);
      },
    },
    {
      attrs: {
        label: "模板名称",
        prop: "templateName",
      },
    },
    // {
    //   attrs: {
    //     label: "联系人",
    //     prop: "contactName",
    //   },
    // },
    // {
    //   attrs: {
    //     label: "联系电话",
    //     prop: "contactNumber",
    //   },
    // },
    {
      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("/contractTemplate/delete", {
                    id: row.id,
                  })
                  .then(() => {
                    ElMessage({
                      message: "删除成功",
                      type: "success",
                    });
                    getList();
                  });
              });
            },
          },
        ];
      },
    },
  ];
});
const getDict = () => {
  proxy.post("/corporation/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    companyList.value = res.rows.map((item) => {
      return {
        label: item.name,
        value: item.id,
      };
    });
  });
};
const getList = async (req) => {
  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);
    });
};
// getDict();
getList();
const modalType = ref("add");
const dialogVisible = ref(false);
const loadingOperation = ref(false);
const submit = ref(null);
const formData = reactive({
  data: {
    countryId: "44",
  },
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const formConfig = computed(() => {
  return [
    {
      type: "title1",
      title: "基础信息",
    },
    {
      type: "select",
      label: "模板类型",
      prop: "templateType",
      data: typeData.value,
    },
    {
      type: "input",
      prop: "templateName",
      label: "模板名称",
      itemType: "text",
      placeholder: "请输入模板名称",
    },
    // {
    //   type: "select",
    //   label: "公司名称",
    //   prop: "corporationId",
    //   data: companyList.value,
    // },
    {
      type: "title1",
      title: "模板内容",
    },
    // {
    //   type: "input",
    //   prop: "contactName",
    //   label: "业务联系人",
    //   itemType: "text",
    //   placeholder: "请输入联系人",
    //   itemWidth: 50,
    // },
    // {
    //   type: "input",
    //   prop: "contactNumber",
    //   label: "",
    //   itemType: "text",
    //   placeholder: "请输入联系电话",
    //   itemWidth: 50,
    // },
    {
      type: "slot",
      prop: "templateContent",
      slotName: "templateContent",
      label: "模板内容",
    },
  ];
});
let rules = ref({
  templateName: [
    { required: true, message: "请输入模板名称", trigger: "blur" },
  ],
  templateType: [
    { required: true, message: "请选择模板类型", trigger: "change" },
  ],
  templateContent: [
    { required: true, message: "请输入模板内容", trigger: "blur" },
  ],
});
const openModal = () => {
  modalType.value = "add";
  formData.data = {
    templateContent: "",
  };
  loadingOperation.value = false;
  dialogVisible.value = true;
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    loadingOperation.value = true;
    proxy.post("/contractTemplate/" + modalType.value, formData.data).then(
      () => {
        ElMessage({
          message: modalType.value == "add" ? "添加成功" : "编辑成功",
          type: "success",
        });
        dialogVisible.value = false;
        getList();
      },
      (err) => {
        console.log(err);
        loadingOperation.value = false;
      }
    );
  });
};
const update = (row) => {
  modalType.value = "edit";
  loadingOperation.value = true;
  proxy.post("/contractTemplate/detail", { id: row.id }).then((res) => {
    formData.data = res;
    loadingOperation.value = false;
    dialogVisible.value = true;
  });
};
const updateContent = (val) => {
  formData.data.templateContent = val;
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
.ql-editor {
  padding: 0px;
}
</style>
