<template>
  <div class="app-container">
    <div
      class="background-color-fff padding-20 border-radius-8px margin-bottom-20"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="用户id" prop="userId">
          <el-input
            v-model="queryParams.userId"
            placeholder="请输入用户id"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="收货人" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入收货人"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="收货电话" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入收货电话"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div
      class="background-color-fff padding-20 border-radius-8px margin-bottom-20"
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['service:userAddress:add']"
          >
            新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['service:userAddress:edit']"
          >
            修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['service:userAddress:remove']"
          >
            删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['service:userAddress:export']"
          >
            导出
          </el-button>
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </div>

    <div
      class="background-color-fff padding-20 border-radius-8px margin-bottom-20"
    >
      <el-table
        v-loading="loading"
        :data="userAddressList"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
        <el-table-column label="用户id" align="center" prop="userId" />
        <el-table-column label="收货人" align="center" prop="name" />
        <el-table-column label="收货电话" align="center" prop="phone" />
        <el-table-column label="用户性别" align="center" prop="sex">
          <template #default="scope">
            {{
              scope.row.sex === "2"
                ? "未知"
                : scope.row.sex === "1"
                  ? "男"
                  : "女"
            }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="省份id" align="center" prop="provinceId" />
                <el-table-column label="省份名称" align="center" prop="provinceName" />
                <el-table-column label="市级id" align="center" prop="cityId" />
                <el-table-column label="市级名称" align="center" prop="cityName" />
                <el-table-column label="区/县级id" align="center" prop="areaId" />
                <el-table-column label="区/县级名称" align="center" prop="areaName" /> -->
        <el-table-column label="详细地址" align="center" prop="address" />
        <el-table-column label="楼栋门牌号" align="center" prop="houseNum" />
        <!-- <el-table-column label="经度" align="center" prop="lng" />
                <el-table-column label="纬度" align="center" prop="lat" /> -->
        <el-table-column
          label="状态：0禁用，1启用"
          align="center"
          prop="status"
        >
          <template #default="scope">
            <el-tag :type="scope.row.status === 0 ? 'danger' : 'success'">
              {{ scope.row.status === 0 ? "禁用" : "启用" }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="创建人" align="center" prop="creater" />
                <el-table-column label="修改人" align="center" prop="updater" /> -->
        <el-table-column
          label="默认收获地址【0非默认，1默认】"
          align="center"
          prop="defaultAddress"
        >
          <template #default="scope">
            <el-tag :type="scope.row.defaultAddress === 0 ? 'info' : 'success'">
              {{ scope.row.defaultAddress === 0 ? "非默认" : "默认" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['service:userAddress:edit']"
            >
              修改
            </el-button>
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['service:userAddress:remove']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改用户收货地址对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="userAddressRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="收货人" prop="name">
          <el-input v-model="form.name" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="收货电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入收货电话" />
        </el-form-item>
        <el-form-item label="省份id" prop="provinceId">
          <el-input v-model="form.provinceId" placeholder="请输入省份id" />
        </el-form-item>
        <el-form-item label="省份名称" prop="provinceName">
          <el-input v-model="form.provinceName" placeholder="请输入省份名称" />
        </el-form-item>
        <el-form-item label="市级id" prop="cityId">
          <el-input v-model="form.cityId" placeholder="请输入市级id" />
        </el-form-item>
        <el-form-item label="市级名称" prop="cityName">
          <el-input v-model="form.cityName" placeholder="请输入市级名称" />
        </el-form-item>
        <el-form-item label="区/县级id" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入区/县级id" />
        </el-form-item>
        <el-form-item label="区/县级名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入区/县级名称" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="楼栋门牌号" prop="houseNum">
          <el-input v-model="form.houseNum" placeholder="请输入楼栋门牌号" />
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="创建人" prop="creater">
          <el-input v-model="form.creater" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="修改人" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item
          label="默认收获地址【0非默认，1默认】"
          prop="defaultAddress"
        >
          <el-input
            v-model="form.defaultAddress"
            placeholder="请输入默认收获地址【0非默认，1默认】"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="UserAddress">
import {
  listUserAddress,
  getUserAddress,
  delUserAddress,
  addUserAddress,
  updateUserAddress,
} from "@/api/service/userAddress";

const { proxy } = getCurrentInstance();

const userAddressList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    name: null,
    phone: null,
    sex: null,
    provinceId: null,
    provinceName: null,
    cityId: null,
    cityName: null,
    areaId: null,
    areaName: null,
    address: null,
    houseNum: null,
    lng: null,
    lat: null,
    status: null,
    creater: null,
    updater: null,
    defaultAddress: null,
  },
  rules: {
    lng: [{ required: true, message: "经度不能为空", trigger: "blur" }],
    lat: [{ required: true, message: "纬度不能为空", trigger: "blur" }],
    status: [
      {
        required: true,
        message: "状态：0禁用，1启用不能为空",
        trigger: "change",
      },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户收货地址列表 */
function getList() {
  loading.value = true;
  listUserAddress(queryParams.value).then((response) => {
    userAddressList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    name: null,
    phone: null,
    sex: null,
    provinceId: null,
    provinceName: null,
    cityId: null,
    cityName: null,
    areaId: null,
    areaName: null,
    address: null,
    houseNum: null,
    lng: null,
    lat: null,
    status: null,
    creater: null,
    createTime: null,
    updater: null,
    updateTime: null,
    defaultAddress: null,
  };
  proxy.resetForm("userAddressRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户收货地址";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getUserAddress(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改用户收货地址";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["userAddressRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateUserAddress(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUserAddress(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除用户收货地址编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delUserAddress(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "service/userAddress/export",
    {
      ...queryParams.value,
    },
    `userAddress_${new Date().getTime()}.xlsx`,
  );
}

getList();
</script>
