# encoding:utf-8
# 商品复制
module RailsAdmin
  module Config
    module Actions     
      class CopyProduct < RailsAdmin::Config::Actions::Base
        register_instance_option :visible? do
          if not bindings[:object].blank?
            authorized?
          else
            authorized?
          end
        end
        # We want the action on members, not the Users collection
        register_instance_option :member do
          true
        end

        register_instance_option :link_icon do
          'icon-refresh'
        end
        # You may or may not want pjax for your action
        register_instance_option :pjax? do
          false
        end

        register_instance_option :controller do
          Proc.new do
            if !@object.blank?
              @copy_product = Product.new
              @copy_product.name = @object.name
              @copy_product.ptype = @object.ptype
              @copy_product.merchant_id = @object.merchant_id
              @copy_product.category_id = @object.category_id
              @copy_product.detail = @object.detail
              @copy_product.price = @object.price
              @copy_product.robo_balance_price = @object.robo_balance_price
              @copy_product.buy_price = @object.buy_price
              @copy_product.user_sale_price = @object.user_sale_price
              @copy_product.count = @object.count
              @copy_product.recommend = @object.recommend
              @copy_product.status = 0
              @copy_product.virtual_sold_count = @object.virtual_sold_count
              @copy_product.purchase_limit_count = @object.purchase_limit_count
              @copy_product.share_content = @object.share_content
              @copy_product.share_img = @object.share_img
              @copy_product.deliver_stop_at = @object.deliver_stop_at
              @copy_product.deliver_start_at = @object.deliver_start_at
              @copy_product.seckill_start = @object.seckill_start
              @copy_product.seckill_end = @object.seckill_end
              @copy_product.seckill_price = @object.seckill_price
              @copy_product.specification = @object.specification
              @copy_product.no_delivery_area = @object.no_delivery_area
              @copy_product.video_state = @object.video_state
              @copy_product.video_url = @object.video_url
              @copy_product.show_flag = 0
              @copy_product.relate_product_id = @object.id
              begin
                @copy_product.save
              rescue Exception => e
                p "************ copy product save fail . #{e.to_s}"
              end
              
              # 复制商品图片
              pictures = ProductPicture.find_by_sql("select * from product_pictures where pic_type=0 and product_id = #{@object.id} limit 1")
              # 创建商品图片
              pictures.each do |u|
                ProductPicture.create({
                    :product_id => @copy_product.id,
                    :img => u.img,
                    :pic_type => u.pic_type,
                    :sort => 0,
                  })
              end              
              redirect_to :back, notice: "已复制!"
              # redirect_to "https://d5ctest.oss-cn-shanghai.aliyuncs.com/temp/20180910.xlsx"
            end
          end
        end
      end
    end
  end
end