<template>
  <div>
    <el-card class="box-card">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :searchConfig="searchConfig"
        highlight-current-row
        :action-list="[
          {
            text: '添加快递',
            action: () => clickModal(),
          },
        ]"
        @get-list="getList"
        @clickReset="clickReset">
        <template #address="{ item }">
          <div>{{ item.province }},{{ item.city }},{{ item.district }},{{ item.address }}</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"> </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 { ref } from "vue";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    expressageCode: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "expressageCode",
      label: "快递编码",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "快递名称",
        prop: "expressage",
        width: 160,
      },
    },
    {
      attrs: {
        label: "打印机",
        prop: "printer",
        width: 140,
      },
    },
    {
      attrs: {
        label: "快递网点",
        prop: "branch",
        width: 140,
      },
    },
    {
      attrs: {
        label: "网点编码",
        prop: "branchCode",
        width: 140,
      },
    },
    {
      attrs: {
        label: "快递编码",
        prop: "expressageCode",
        width: 140,
      },
    },
    {
      attrs: {
        label: "联系人",
        prop: "name",
        width: 120,
      },
    },
    {
      attrs: {
        label: "电话",
        prop: "phone",
        width: 140,
      },
    },
    {
      attrs: {
        label: "发货地址",
        slot: "address",
        "min-width": 240,
      },
    },
    {
      attrs: {
        label: "取号模式",
        prop: "takeNum",
        width: 120,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["take_num"]);
      },
    },
    {
      attrs: {
        label: "操作",
        width: 80,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "编辑",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickUpdate(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("/expressDelivery/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: "140px",
  itemWidth: 100,
  rules: [],
  labelPosition: "right",
});
const formData = reactive({
  data: {},
});
const formConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "name",
      label: "联系人",
      itemType: "text",
    },
    {
      type: "input",
      prop: "phone",
      label: "电话",
      itemType: "text",
    },
    {
      type: "input",
      prop: "province",
      label: "省",
      itemType: "text",
    },
    {
      type: "input",
      prop: "city",
      label: "市",
      itemType: "text",
    },
    {
      type: "input",
      prop: "district",
      label: "区",
      itemType: "text",
    },
    {
      type: "input",
      prop: "address",
      label: "详细地址",
      itemType: "text",
    },
    {
      type: "input",
      prop: "expressage",
      label: "快递物流",
      itemType: "text",
    },
    {
      type: "input",
      prop: "expressageCode",
      label: "快递编码",
      itemType: "text",
    },
    {
      type: "input",
      prop: "branch",
      label: "快递网点",
      itemType: "text",
    },
    {
      type: "input",
      prop: "branchCode",
      label: "网点编码",
      itemType: "text",
    },
    {
      type: "input",
      prop: "templateUrl",
      label: "标准面单模板URL",
      itemType: "text",
    },
    {
      type: "input",
      prop: "customTemplateUrl",
      label: "自定义面单模板URL",
      itemType: "text",
    },
    {
      type: "select",
      prop: "takeNum",
      label: "取号模式",
      data: proxy.useUserStore().allDict["take_num"],
    },
    {
      type: "input",
      prop: "printer",
      label: "绑定打印机",
      itemType: "text",
    },
  ];
});
const rules = ref({
  name: [{ required: true, message: "请输入联系人", trigger: "blur" }],
  phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
  takeNum: [{ required: true, message: "请选择取号模式", trigger: "change" }],
});
const clickModal = () => {
  modalType.value = "add";
  formData.data = {};
  openDialog.value = true;
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    proxy.post("/expressDelivery/" + modalType.value, formData.data).then(() => {
      ElMessage({
        message: modalType.value == "add" ? "添加成功" : "编辑成功",
        type: "success",
      });
      openDialog.value = false;
      getList();
    });
  });
};
const clickUpdate = (row) => {
  modalType.value = "edit";
  proxy.post("/expressDelivery/detail", { id: row.id }).then((res) => {
    formData.data = res;
    openDialog.value = true;
  });
};
</script>

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