<template>
  <page-header-wrapper title="地址管理">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="姓名">
                <a-input v-model="queryParam.name" placeholder="检索收件人姓名" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="电话">
                <a-input v-model="queryParam.mobile" placeholder="检索收件人电话" />
              </a-form-item>
            </a-col>
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button class="ml8" @click="() => ((this.queryParam = {}), $refs.table.refresh(true))">重置</a-button>
            <a-button class="ml8" type="danger" @click="$router.push({ name: 'AddressCreate' })">新增</a-button>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(row) => row.address_id"
        showPagination="auto"
        bordered
        size="default"
      >
        <span slot="default" slot-scope="text, record">
          <a-switch :checked="record.default" @change="changeDefault($event,record)" />
        </span>
        <div slot="actions" slot-scope="text, record">
          <a @click="$router.push({ name: 'AddressEdit',params: { addressId: record.address_id } })">修改</a>
          <a-divider type="vertical" />
          <a-popconfirm title="是否确认删除该收货地址?" ok-text="Yes" cancel-text="No" @confirm="delConfirm(record)">
            <a>删除</a>
          </a-popconfirm>
        </div>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable, UserInfo } from '@/components'
import {
  getAddrList,
  deleteAddr,
  defaultAddress
} from '@/api/address'

const columns = [
  {
    title: '编号',
    dataIndex: 'address_id',
    scopedSlots: { customRender: 'address_id' },
    width: 70,
    align: 'center'
  },
  {
    title: '收件人信息',
    dataIndex: 'user',
    key: 'user',
    scopedSlots: { customRender: 'user' },
    children: [
      {
        title: '收件人姓名',
        dataIndex: 'name',
        key: 'name',
        scopedSlots: { customRender: 'name' },
        align: 'center',
        width: '120px'
      },
      {
        title: '收件人电话',
        dataIndex: 'mobile',
        key: 'mobile',
        align: 'center',
        width: '130px'
      }
    ]
  },
  {
    title: '详细地址',
    dataIndex: 'full_address',
    scopedSlots: { customRender: 'full_address' },
    key: 'full_address',
    align: 'center'
  },
  {
    title: '默认地址',
    dataIndex: 'default',
    scopedSlots: { customRender: 'default' },
    key: 'default',
    align: 'center',
    width: '120px'
  },
  {
    title: '创建时间',
    dataIndex: 'created_at',
    align: 'center',
    sorter: true,
    width: '120px'
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'actions' },
    align: 'center',
    width: '120px'
  }
]

export default {
  name: 'Index',
  components: {
    STable,
    UserInfo
  },
  data () {
    return {
      columns: columns,
      dataList: [],
      queryParam: {
        name: '',
        mobile: ''
      },
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getAddrList(requestParameters)
      }
    }
  },
  watch: {
    '$route' (to, from) {
      if (from.name === 'AddressCreate') {
        this.$refs.table.refresh(true)
      } else if (from.name === 'AddressEdit') {
        this.$refs.table.loadData()
      }
    }
  },

  methods: {
    // 修改默认
    changeDefault (event, item) {
      if (event) {
        defaultAddress(item.address_id).then((res) => {
          this.$message.success('修改成功')
          this.$refs.table.refresh(true)
        })
      }
    },
    // 删除
    delConfirm (item) {
      deleteAddr(item.address_id).then((res) => {
        this.$message.success('删除成功')
        this.$refs.table.loadData()
      })
    },
    logistic (order) {
      this.$router.push({
        name: 'OrderLogistic',
        params: { orderId: order.order_no }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ml8 {
  margin-left: 8px;
}
</style>
