package com.liquidnet.client.admin.web.controller.zhengzai.kylin;

import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.core.page.TableDataInfo;
import com.liquidnet.client.admin.common.enums.BannersDisplayLocation;
import com.liquidnet.client.admin.common.enums.BannersTargetType;
import com.liquidnet.client.admin.zhengzai.kylin.service.impl.KylinBannersServiceImpl;
import com.liquidnet.service.kylin.dao.BannerDetailsListDao;
import com.liquidnet.service.kylin.dto.param.BannersParam;
import com.liquidnet.service.kylin.dto.param.BannersSearchParam;
import com.liquidnet.service.kylin.dto.vo.KylinBannersVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 轮播图 后端管理控制器
 * </p>
 *
 * @author jiangxiulong
 * @since 2021-05-01
 */

@Controller
@RequestMapping("/kylin/banners")
public class KylinBannersAdminController extends BaseController {

    private String prefix = "zhengzai/kylin/banners";

    @Value("${liquidnet.url-banner.provinces}")
    private String provinces;
    @Value("${liquidnet.url-banner.url1}")
    private String url1;
    @Value("${liquidnet.url-banner.url2}")
    private String url2;
    @Value("${liquidnet.url-banner.url5}")
    private String url5;
    @Value("${liquidnet.url-banner.url6}")
    private String url6;
    @Value("${liquidnet.url-banner.url7}")
    private String url7;
    @Value("${liquidnet.url-banner.url8}")
    private String url8;
    @Value("${liquidnet.url-banner.url13}")
    private String url13;
    @Value("${liquidnet.url-banner.url14}")
    private String url14;
    @Value("${liquidnet.url-banner.url20}")
    private String url20;
    @Value("${liquidnet.url-banner.url21}")
    private String url21;
    @Value("${liquidnet.url-banner.url22}")
    private String url22;
    @Value("${liquidnet.url-banner.url23}")
    private String url23;
    @Value("${liquidnet.url-banner.url24}")
    private String url24;
    @Value("${liquidnet.url-banner.url26}")
    private String url26;

    @Autowired
    private KylinBannersServiceImpl kylinBannersServiceImpl;

    @RequiresPermissions("kylin:banners:view")
    @GetMapping()
    public String banners() {
        return prefix + "/banners";
    }

    @GetMapping("/create")
    public String create(ModelMap mmap) {
        List<Object> bannersTargetType = BannersTargetType.getResultType();
        List<Object> bannersDisplayLocation = BannersDisplayLocation.getResultType();
        mmap.put("BannersTargetType", bannersTargetType);
        mmap.put("BannersDisplayLocation", bannersDisplayLocation);

        mmap.put("provinces", provinces);
        mmap.put("url1", url1);
        mmap.put("url2", url2);
        mmap.put("url5", url5);
        mmap.put("url6", url6);
        mmap.put("url7", url7);
        mmap.put("url8", url8);
        mmap.put("url13", url13);
        mmap.put("url14", url14);
        mmap.put("url20", url20);
        mmap.put("url21", url21);
        mmap.put("url22", url22);
        mmap.put("url23", url23);
        mmap.put("url24", url24);
        mmap.put("url26", url26);

        return prefix + "/create";
    }

    @RequiresPermissions("kylin:banners:create")
    @PostMapping("create")
    @ResponseBody
    public AjaxResult createSave(@RequestBody @Validated BannersParam bannersParam) {
        Boolean res = kylinBannersServiceImpl.create(bannersParam);
        if (res) {
            return success();
        } else {
            return error("添加banner失败");
        }
    }

    @GetMapping("/update/{bannersId}")
    public String update(@PathVariable("bannersId") String bannersId, ModelMap mmap) {
        KylinBannersVo result = null;
        result = kylinBannersServiceImpl.detail(bannersId);
        mmap.put("KylinBannersVo", result);
        return prefix + "/update";
    }

    @RequiresPermissions("kylin:banners:update")
    @PostMapping("update")
    @ResponseBody
    public AjaxResult updateSave(@RequestBody BannersParam bannersParam) {
        boolean res = kylinBannersServiceImpl.update(bannersParam);
        if (res) {
            return success();
        } else {
            return error("修改banner失败");
        }
    }

    /*@RequiresPermissions("kylin:banners:detail")
    @GetMapping("/details/{bannersId}")
    public String detail(@PathVariable("bannersId") String bannersId, ModelMap mmap) {
        KylinBannersVo result = null;
        result = kylinBannersServiceImpl.detail(bannersId);
        mmap.put("KylinBannersVo", result);
        return prefix + "/details";
    }*/

    @RequiresPermissions("kylin:banners:list")
    @PostMapping("list")
    @ResponseBody
    public TableDataInfo bannerList(BannersSearchParam bannersSearchParam) {
        PageInfo<BannerDetailsListDao> result = kylinBannersServiceImpl.bannerList(bannersSearchParam);
        return getDataTable(result.getList());
    }

    @RequiresPermissions("kylin:banners:delete")
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult delete(BannersParam bannersParam) {
        String bannersId = bannersParam.getBannersId();
        boolean result = kylinBannersServiceImpl.delete(bannersId);
        if (result) {
            return success("删除成功");
        } else {
            return error("删除失败");
        }
    }

}
