<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="800" v-loading="loadingOperation">
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit">
        <template #allAddress>
          <el-row :gutter="10" style="width: 100%">
            <el-col :span="8">
              <el-form-item prop="countryId">
                <el-select v-model="formData.data.countryId" placeholder="国家" @change="(val) => getCityData(val, '20', true)">
                  <el-option v-for="item in countryData" :label="item.chineseName" :value="item.id"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="provinceId">
                <el-select v-model="formData.data.provinceId" placeholder="省/洲" @change="(val) => getCityData(val, '30', true)">
                  <el-option v-for="item in provinceData" :label="item.name" :value="item.id"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="cityId">
                <el-select v-model="formData.data.cityId" placeholder="城市">
                  <el-option v-for="item in cityData" :label="item.name" :value="item.id"> </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="margin-top: 20px; width: 100%">
            <el-col :span="24">
              <el-form-item prop="address">
                <el-input v-model="formData.data.address" type="textarea"> </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
        <template #allAddressEnglish>
          <el-row :gutter="10" style="width: 100%">
            <el-col :span="8">
              <el-form-item prop="countryEnStr">
                <el-input v-model="formData.data.countryEnStr" placeholder="请输入国家 (英文)" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="provinceEnStr">
                <el-input v-model="formData.data.provinceEnStr" placeholder="请输入省/洲 (英文)" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="cityEnStr">
                <el-input v-model="formData.data.cityEnStr" placeholder="请输入城市 (英文)" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="margin-top: 20px; width: 100%">
            <el-col :span="24">
              <el-form-item prop="addressEn">
                <el-input v-model="formData.data.addressEn" type="textarea"> </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
        <template #templateContent>
          <div style="width: 100%">
            <Editor :value="formData.data.templateContent" @updateValue="updateContent"/>
          </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 { 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 sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    corporationId: "",
    keyword: "",
  },
});
const selectConfig = computed(() => {
  return [
    {
      label: "公司名称",
      prop: "corporationId",
      data: companyList.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "公司名称",
        prop: "corporationName",
      },
    },
    {
      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: "China",
  },
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const formConfig = computed(() => {
  return [
    {
      label: "基础信息",
    },
    {
      type: "input",
      prop: "templateName",
      label: "模板名称",
      itemType: "text",
      placeholder: "请输入模板名称",
    },
    {
      type: "select",
      label: "公司名称",
      prop: "corporationId",
      data: companyList.value,
    },
    {
      label: "联系信息",
    },
    {
      type: "input",
      prop: "contactName",
      label: "业务联系人",
      itemType: "text",
      placeholder: "请输入联系人",
      itemWidth: 30,
    },
    {
      type: "input",
      prop: "contactNumber",
      label: "  ",
      itemType: "text",
      placeholder: "请输入联系电话",
      itemWidth: 70,
    },
    {
      type: "input",
      prop: "corporationNumber",
      label: "公司电话",
      itemType: "text",
      placeholder: "请输入公司电话",
    },
    {
      type: "slot",
      slotName: "allAddress",
      label: "公司地址",
    },
    {
      type: "slot",
      slotName: "allAddressEnglish",
      label: "公司地址 (英文)",
    },
    {
      type: "slot",
      slotName: "templateContent",
      label: "合同模板",
    },
  ];
});
let rules = ref({
  templateName: [{ required: true, message: "请输入模板名称", trigger: "blur" }],
  corporationId: [{ required: true, message: "请选择公司", trigger: "change" }],
  countryId: [{ required: true, message: "请选择国家", trigger: "change" }],
  provinceId: [{ required: true, message: "请选择省/州", trigger: "change" }],
  countryEnStr: [{ required: true, message: "请输入国家 (英文)", trigger: "blur" }],
  provinceEnStr: [{ required: true, message: "请输入省/洲 (英文)", trigger: "blur" }],
  //   cityId: [{ required: true, message: "请选择城市", trigger: "change" }],
});
const countryData = ref([]);
const provinceData = ref([]);
const cityData = ref([]);
const getCityData = (id, type, isChange) => {
  proxy.post("/areaInfo/list", { parentId: id }).then((res) => {
    if (type === "20") {
      provinceData.value = res;
      if (isChange) {
        formData.data.provinceId = "";
        formData.data.cityId = "";
      }
    } else if (type === "30") {
      cityData.value = res;
      if (isChange) {
        formData.data.cityId = "";
      }
    } else {
      countryData.value = res;
    }
  });
};
getCityData("0");
const openModal = () => {
  modalType.value = "add";
  formData.data = {
    templateContent: "",
    countryId: "China",
  };
  getCityData(formData.data.countryId, "20");
  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;
    getCityData(formData.data.countryId, "20");
    getCityData(formData.data.provinceId, "30");
    loadingOperation.value = false;
    dialogVisible.value = true;
  });
};
const updateContent = (val) => {
  formData.data.templateContent = val
}
</script>

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