<template>
  <div class="bg">
    <el-card style="width: 100%;">
        <div slot="header" class="clearfix">
            <span>收货地址</span>
            <el-button size="small" style="float: right;" type="primary" @click="openDialog('add', {})">新增地址</el-button>
        </div>

        <el-table :data="addresses" border style="width: 100%;">
            <el-table-column prop="receiver" label="收货人" width="120"></el-table-column>
            <el-table-column prop="region" label="所在地区" width="120"></el-table-column>
            <el-table-column prop="detailAddress" label="详细地址" width="300"></el-table-column>
            <el-table-column prop="phone" label="手机号码" width="150"></el-table-column>
            <el-table-column prop="fixedPhone" label="固定电话" width="150"></el-table-column>
            <el-table-column prop="alias" label="地区别名" width="120"></el-table-column>
            <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

          <!-- 分页 -->
        <div style="text-align: right; margin-top: 20px;">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchParam.currentPage"
            :page-sizes="[5, 10, 20]"
            :page-size="searchParam.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="searchParam.total"
          ></el-pagination>
        </div>


        
    </el-card>


    <el-dialog :title="`${address.type === 'add' ? '新增' : '编辑'}收货地址`" :visible.sync="dialogVisible" width="700px">
      <el-form :model="address" label-width="100px" size="small">
        <el-form-item label="*收货人">
          <el-input v-model="address.receiver" placeholder="请填写收货人姓名,限制40个字符"></el-input>
        </el-form-item>
        <el-form-item label="*所在地区">
          <el-select v-model="address.region" placeholder="请选择所在地区">
            <el-option label="北京" value="北京"></el-option>
            <el-option label="上海" value="上海"></el-option>
            <el-option label="广州" value="广州"></el-option>
            <el-option label="深圳" value="深圳"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="*详细地址">
          <el-input v-model="address.detailAddress" placeholder="请填写详细地址,限制500个字符"></el-input>
        </el-form-item>
        <el-form-item label="*手机号码">
          <el-input v-model="address.phone" placeholder="请填写手机号码,限制11位数字"></el-input>
        </el-form-item>
        <el-form-item label="固定电话">
          <el-input v-model="address.fixedPhone" placeholder="请填写固定号码,限制40个字符"></el-input>
        </el-form-item>
        <el-form-item label="地区别名">
          <el-input v-model="address.alias" placeholder="请填写常用名称,限制40个字符"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="saveAddress">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'UserInfo',
  data() {
    return {
        user: {},
        searchParam: {pageSize: 5, currentPage: 1,total: 10},
        dialogVisible: false,
        addresses: [
          {
            id: 1,
            receiver: '张三',
            region: '北京',
            detailAddress: '北京市朝阳区xx街道xx小区xx号楼xx单元',
            phone: '13800138000',
            fixedPhone: '010-12345678',
            alias: '家'
          },
          {
            id: 2,
            receiver: '李四',
            region: '上海',
            detailAddress: '上海市浦东新区xx街道xx小区xx号楼xx单元',
            phone: '13900139000',
            fixedPhone: '021-12345678',
            alias: '公司'
          },
          {
            id: 3,
            receiver: '王五',
            region: '广州',
            detailAddress: '广州市天河区xx街道xx小区xx号楼xx单元',
            phone: '13700137000',
            fixedPhone: '020-12345678',
            alias: '朋友家'
          }
        ],
        address: {
          type: 'add',
          id: '',
          receiver: '',
          region: '',
          detailAddress: '',
          phone: '',
          fixedPhone: '',
          alias: ''
        }
    }
  },
  methods:{
        // 分页大小改变
    handleSizeChange(val) {
        this.searchParam.pageSize = val;
    },
    
    // 当前页改变
    handleCurrentChange(val) {
        this.searchParam.currentPage = val;
    },
    openDialog (type, item) {
      this.address = {
        type: type,
        id: item.id || "",
        receiver: item.receiver || "",
        region: item.region || "",
        detailAddress: item.detailAddress || "",
        phone: item.phone || "",
        fixedPhone: item.fixedPhone || "",
        alias: item.alias || ""
      }
      this.dialogVisible = true;
    },

    saveAddress() {
          if (this.address.id) {
            // 编辑地址
            const index = this.addresses.findIndex(item => item.id === this.address.id);
            if (index !== -1) {
              this.addresses[index] = { ...this.address };
            }
          } else {
            // 新增地址
            this.address.id = Date.now();
            this.addresses.push({ ...this.address });
          }
          this.$message({
            message: '保存成功',
            type: 'success'
          });
          this.dialogVisible = false;
        },
        handleEdit(row) {
          this.address = { ...row };
          this.dialogVisible = true;
        },
        handleDelete(row) {
          this.$confirm('此操作将永久删除该地址, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const index = this.addresses.findIndex(item => item.id === row.id);
            if (index !== -1) {
              this.addresses.splice(index, 1);
            }
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
        }
  }
  
}
</script>
<style lang="scss" scoped>

</style>