'use strict';

import Base from './base.js';

export default class extends Base {
  /**
   * 获取用户的收货地址
   * @return {Promise} []
   */
  async listAction(){
    let addressList = await this.model('address').where({user_id: 1}).select();
    let itemKey = 0;
    for ( let addressItem of addressList) {
        addressList[itemKey].province_name = await this.model('region').getRegionName(addressItem.province_id);
        addressList[itemKey].city_name = await this.model('region').getRegionName(addressItem.city_id);
        addressList[itemKey].district_name = await this.model('region').getRegionName(addressItem.district_id);
        addressList[itemKey].full_region = addressList[itemKey].province_name + addressList[itemKey].city_name + addressList[itemKey].district_name;

        itemKey += 1;
    }


    return this.success(addressList);
  }

  /**
   * 获取收货地址的详情
   * @return {Promise} []
   */
  async detailAction(){
    let addressId = this.get('id');
    let addressInfo = await this.model('address').where({user_id: 1, id: addressId}).find();
    if ( !think.isEmpty(addressInfo)) {
      addressInfo.province_name = await this.model('region').getRegionName(addressInfo.province_id);
      addressInfo.city_name = await this.model('region').getRegionName(addressInfo.city_id);
      addressInfo.district_name = await this.model('region').getRegionName(addressInfo.district_id);
      addressInfo.full_region = addressInfo.province_name + addressInfo.city_name + addressInfo.district_name;
    }

    return this.success(addressInfo);
  }

  /**
   * 添加或更新收货地址
   * @returns {Promise.<Promise|PreventPromise|void>}
   */
  async saveAction(){

    let addressId = this.post('id');

    let addressData = {
      name: this.post('name'),
      mobile: this.post('mobile'),
      province_id: this.post('province_id'),
      city_id: this.post('city_id'),
      district_id: this.post('district_id'),
      address: this.post('address'),
      user_id: 1,
      is_default: this.post('is_default') == true ? 1 : 0
    };

    if (think.isEmpty(addressId)) {
      addressId = await this.model('address').add(addressData);
    } else {
      await this.model('address').where({id: addressId, user_id: 1}).update(addressData);
    }

    //如果设置为默认，则取消其它的默认
    if (this.post('is_default') == true) {
        await this.model('address').where({id: ['<>', addressId], user_id: 1}).update({
            is_default: 0
        });
    }

    let addressInfo = await this.model('address').where({id: addressId}).find();

    return this.success(addressInfo);
  }

  /**
   * 删除指定的收货地址
   * @returns {Promise.<Promise|PreventPromise|void>}
   */
  async deleteAction(){
    let addressId = this.post('id');

    await this.model('address').where({id: addressId, user_id: 1}).delete();

    return this.success('删除成功');
  }

}