<template>
    <section class="content">
        <div class="box box-primary">
            <div class="box-body">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1 id="itemTitle"></h1>
                </section>
                <!-- .content -->
                <div id="table"></div>
                <div id="toolbar">
                    <div class="col-sm-12">
                        <label for="sheadwearId" class="col-sm-1 control-label">头饰ID:</label>
                        <div class="col-sm-2"><input type="text" class="form-control" name="headwearId" id="sheadwearId">
                        </div>

                        <label for="sheadwearName" class="col-sm-1 control-label">头饰名称:</label>
                        <div class="col-sm-2"><input type="text" class="form-control" name="headwearName"
                                id="sheadwearName"></div>

                        <label for="slimitType" class="col-sm-1 control-label">限定类型:</label>
                        <div class="col-sm-2">
                            <select name="limitType" id="slimitType" data-btn-class="btn-warning" class="form-control">
                                <option value="" selected="selected">全部</option>
                                <option value="1">贵族限定</option>
                                <option value="2">怪兽限定</option>
                                <option value="3">活动限定</option>
                                <option value="4">周星榜限定</option>
                            </select>
                        </div>

                        <label for="senable" class="col-sm-1 control-label">头饰状态:</label>
                        <div class="col-sm-2">
                            <select name="enable" id="senable" data-btn-class="btn-warning" class="form-control">
                                <option value="" selected="selected">全部</option>
                                <option value="1">上架</option>
                                <option value="0">下架</option>
                            </select>
                        </div>

                        <label for="partitionId" class="col-sm-1 control-label">地区:</label>
                            <div class="col-sm-2">
                                <select name="partitionId" id="partitionId" class="form-control"></select>
                            </div>
                        </div>

                    <div class="col-sm-12">
                        <button id="btnSearch" class="btn btn-default">
                            <i class="glyphicon glyphicon-search"></i>查询
                        </button>
                        <button id="btnAdd" class="btn btn-default opt-apply">
                            <i class="glyphicon glyphicon-adjust"></i>新增
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <div class="modal fade" id="headwearModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="modalLabel">增加头饰</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="addForm">
                        <input type="hidden" name="headwearId" id="headwearId" />
                        <div class="form-group">
                            <label for="partitionFlag" class="col-sm-4 control-label">选择分区</label>
                            <div class="col-sm-6">
                                <input type="checkbox" name="partitionFlag" value="1"/>英语区<br>
                                <input type="checkbox" name="partitionFlag" value="2"/>阿拉伯语区<br>
                                <input type="checkbox" name="partitionFlag" value="4"/>华语区<br>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="headwearName" class="col-sm-3 control-label">头饰名称:</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="headwearName" id="headwearName">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="headwearArName" class="col-sm-3 control-label">阿语头饰名称:</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="headwearName" id="headwearArName">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="headwearEnName" class="col-sm-3 control-label">英语头饰名称:</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="headwearName" id="headwearEnName">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">使用萝卜购买</label>
                            <div class="col-sm-9">
                                <label class="radio-inline"><input type="radio" name="radishSale" value="0"
                                        checked>否</label>
                                <label class="radio-inline"><input type="radio" name="radishSale" value="1">是</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="radishPrice" class="col-sm-3 control-label">首次价格:</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" name="radishPrice" id="radishPrice">
                            </div>
                            <span class="col-sm-3">萝卜</span>
                        </div>

                        <div class="form-group">
                            <label for="radishRenewPrice" class="col-sm-3 control-label">续费价格:</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" name="radishRenewPrice" id="radishRenewPrice">
                            </div>
                            <span class="col-sm-3">萝卜</span>
                        </div>
                        <div class="form-group">
                            <label for="radishOriginalPrice" class="col-sm-3 control-label">原价:</label>
                            <div class="col-sm-6">
                                <input type="number" class="form-control" placeholder="没有填0" value="0"
                                    name="radishOriginalPrice" id="radishOriginalPrice">
                            </div>
                            <span class="col-sm-3">萝卜</span>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">使用金币购买</label>
                            <div class="col-sm-9">
                                <label class="radio-inline"><input type="radio" name="goldSale" value="0">否</label>
                                <label class="radio-inline"><input type="radio" name="goldSale" value="1" checked>是</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="price" class="col-sm-3 control-label">首次价格:</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" name="price" id="price">
                            </div>
                            <span class="col-sm-3">金币</span>
                        </div>

                        <div class="form-group">
                            <label for="renewPrice" class="col-sm-3 control-label">续费价格:</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" name="renewPrice" id="renewPrice">
                            </div>
                            <span class="col-sm-3">金币</span>
                        </div>
                        <div class="form-group">
                            <label for="originalPrice" class="col-sm-3 control-label">原价:</label>
                            <div class="col-sm-6">
                                <input type="number" class="form-control" placeholder="没有填0" value="0" name="originalPrice"
                                    id="originalPrice">
                            </div>
                            <span class="col-sm-3">金币</span>
                        </div>

                        <div class="form-group">
                            <label for="days" class="col-sm-3 control-label">购买天数:</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="days" id="days">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">是否可售卖</label>
                            <div class="col-sm-9">
                                <label class="radio-inline"><input type="radio" name="isSale" value="1" checked>是</label>
                                <label class="radio-inline"><input type="radio" name="isSale" value="0">否</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="nobleLimit" class="col-sm-3 control-label">限定类型:</label>
                            <div class="col-sm-9">
                                <!--<input type="text" class="form-control" name="limitType" id="limitType">-->
                                <label><input type="checkbox" id="nobleLimit" name="nobleLimit"
                                        value="true">贵族限定&nbsp;&nbsp;</label>
                                <label><input type="checkbox" id="monsterLimit" name="monsterLimit"
                                        value="true">怪兽限定&nbsp;&nbsp;</label>
                                <label><input type="checkbox" id="activityLimit" name="activityLimit"
                                        value="true">活动限定&nbsp;&nbsp;</label>
                                <label><input type="checkbox" id="weekStarLimit" name="weekStarLimit"
                                        value="true">周星榜限定&nbsp;&nbsp;</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="limitDesc" class="col-sm-3 control-label">限定描述:</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="limitDesc" id="limitDesc">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="enable" class="col-sm-3 control-label">头饰状态:</label>
                            <div class="col-sm-9">
                                <!--<input type="text" class="form-control" name="status" id="status">-->
                                <input type="radio" name="enable" id="enable" value="true">上架
                                <input type="radio" name="enable" id="disable" value="false">下架
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="pic" class="col-sm-3 control-label">头饰图片：</label>
                            <div class="col-sm-9">
                                <img src="" id="picImage" style="width:250px;height:90px;" alt="">
                                <input type="file" id="picFile" name="uploadFile">
                                <button class="btn btn-success" type="button"
                                    onclick="uploadfile('picFile','picImage','pic','picInfo')">上传</button>
                                <input type="hidden" id="pic" name="pic" class="form-control" />
                                <span id="picInfo" style="color:red;"></span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="effect" class="col-sm-3 control-label">动效图片：</label>
                            <div class="col-sm-9">
                                <img src="" id="effectImage" style="width:250px;height:90px;" alt="">
                                <input type="file" id="effectFile" name="uploadFile">
                                <button class="btn btn-success" type="button"
                                    onclick="uploadfile('effectFile','effectImage','effect','effectInfo')">上传</button>
                                <input type="hidden" id="effect" name="effect" class="form-control" />
                                <span id="effectInfo" style="color:red;"></span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="seq" class="col-sm-3 control-label">排序:</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="seq" id="seq">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">标签:</label>
                            <div class="col-sm-9">
                                <label class="radio-inline"><input type="radio" name="labelType" id="noOne" value="0"
                                        checked>无</label>
                                <label class="radio-inline"><input type="radio" name="labelType" id="newOne"
                                        value="1">新品</label>
                                <label class="radio-inline"><input type="radio" name="labelType" id="discountOne"
                                        value="2">折扣</label>
                                <label class="radio-inline"><input type="radio" name="labelType" id="limitOne"
                                        value="3">限定</label>
                                <label class="radio-inline"><input type="radio" name="labelType" id="exclusiveOne"
                                        value="4">专属</label>
                            </div>
                        </div>



                        <div class="form-group">
                            <label for="redirectLink" class="col-sm-3 control-label">跳转链接:</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="redirectLink"
                                    placeholder="非必填，若填写，前端展示去看看按钮，反之则无按钮" id="redirectLink">
                            </div>
                        </div>

                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="add">提交</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 为用户赠送头饰弹窗 -->
    <div class="modal fade" id="sendHeadwearModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">x</span></button>
                    <h4 class="modal-title" id="modalSendLabel">赠送头饰</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="headwearSendForm">
                        <input type="hidden" name="sendHeadwearId" id="sendHeadwearId" />
                        <div class="form-group">
                            <label for="sendErbanNo" class="col-sm-3 control-label">平台号</label>
                            <div class="col-sm-9">
                                <textarea name="sendErbanNo" id="sendErbanNo" class="form-control validate[required]"
                                    placeholder="多个平台号使用换行符分隔"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="sendDays" class="col-sm-3 control-label">赠送天数</label>
                            <div class="col-sm-9">
                                <input type="text" name="days" id="sendDays" class="form-control validate[required]" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="sendDesc" class="col-sm-3 control-label">备注</label>
                            <div class="col-sm-9">
                                <input type="text" name="desc" id="sendDesc" class="form-control validate[required]" />
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="send">提交</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import TableHelper from '@/utils/bootstrap-table-helper';
import { getPartitionInfoList } from '@/api/partition/partitionInfo';
import { buildSelectOption } from '@/utils/system-helper';

export default {
    name: "HeadwearAdminView",
    setup() {
        function uploadfile(file, image, path, info) {
            $(this).attr('disabled', "true");
            $.ajaxFileUpload({
                fileElementId: file,    //需要上传的文件域的ID，即<input type="file">的ID。
                url: '/admin/upload/img', //后台方法的路径
                type: 'post',   //当要提交自定义参数时，这个参数要设置成post
                dataType: 'json',   //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
                secureuri: false,   //是否启用安全提交，默认为false。
                async: true,   //是否是异步
                success: function (json) {   //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
                    if (json.path) {
                        $('#' + path).val(json.path);
                        $('#' + image).attr("src", json.path);
                        if (json.path != '') {
                            $("#" + info).html('已上传成功');
                        } else {
                            $("#" + info).html('未上传成功');
                        }
                        console.log(json.path);

                    } else {
                        $("#tipMsg").text(json.msg);
                        $("#tipModal").modal('show');
                    }
                },
                error: function (data, status, e) {  //提交失败自动执行的处理函数。
                    $(this).removeAttr("disabled");
                    console.error(e);
                }
            });
        }
        window.uploadfile = uploadfile;
        return {
            uploadfile
        };
    },
    created() {
        this.$nextTick(function () {
            this.initPartition();
            this.initData();
        });
    },
    methods: {
        initData() {
            $(function () {
                $('#table').bootstrapTable('destroy');

                $('#table').bootstrapTable({
                    columns: [
                        //{field: 'id', title: 'id', align: 'center', width: '5%'},
                        { field: 'tmp', title: 'headwearId', align: 'center', checkbox: true, width: '5%' },
                        { field: 'headwearId', title: '头饰ID', align: 'center', width: '5%' },
                        {field: 'name.zh', title: '头饰名称', align: 'center', width: '5%'},
                        {field: 'name.ar', title: '阿语头饰名称', align: 'center', width: '5%'},
                        {field: 'name.en', title: '英语头饰名称', align: 'center', width: '5%'},
                        {
                            field: 'partitionFlag', 
                            title: '地区', 
                            align: 'center', 
                            width: '10%',
                            formatter: function (val, row, index) {
                                let value = '';
                                if (val & 1 != 0) {
                                    value += '英语区<br>';
                                }
                                if (val & 2 != 0) {
                                    value += '阿拉伯语区<br>';
                                }
                                if (val & 4 != 0) {
                                    value += '华语区<br>';
                                }
                                return value;
                            }
                        },
                        {
                            field: 'goldSale',
                            title: '金币购买',
                            align: 'center',
                            width: '3%',
                            formatter: function (val, row, index) {
                                if (val == true) {
                                    return "是";
                                } else if (val == false) {
                                    return "否";
                                } else {
                                    return "-";
                                }
                            }
                        },
                        {
                            field: 'radishSale',
                            title: '萝卜购买',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                if (val == true) {
                                    return "是";
                                } else if (val == false) {
                                    return "否";
                                } else {
                                    return "-";
                                }
                            }
                        },
                        { field: 'originalPrice', title: '原价（金币）', align: 'center', width: '5%' },
                        { field: 'price', title: '购买价格（金币）', align: 'center', width: '5%' },
                        { field: 'renewPrice', title: '续费价格（金币）', align: 'center', width: '5%' },
                        { field: 'radishOriginalPrice', title: '原价（萝卜）', align: 'center', width: '5%' },
                        { field: 'radishPrice', title: '购买价格（萝卜）', align: 'center', width: '5%' },
                        { field: 'radishRenewPrice', title: '续费价格（萝卜）', align: 'center', width: '5%' },
                        { field: 'days', title: '有效期', align: 'center', width: '5%' },
                        { field: 'seq', title: '排序', align: 'center', width: '5%' },
                        {
                            field: 'labelType',
                            title: '标签',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                if (val == 0) {
                                    return "无";
                                } else if (val == 1) {
                                    return "新品";
                                } else if (val == 2) {
                                    return "折扣";
                                } else if (val == 3) {
                                    return "限定";
                                } else if (val == 4) {
                                    return "专属";
                                } else {
                                    return "未知状态";
                                }
                            }
                        },
                        { field: 'redirectLink', title: '跳转链接', align: 'center', width: '5%' },
                        {
                            field: 'enable',
                            title: '上架状态',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                if (val == true) {
                                    return "上架";
                                } else if (val == false) {
                                    return "下架";
                                } else {
                                    return "未知状态";
                                }
                            }
                        },
                        {
                            field: 'nobleLimit',
                            title: '贵族限定',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                if (val == true) {
                                    return "是";
                                } else if (val == false) {
                                    return "否";
                                } else {
                                    return "未知状态";
                                }
                            }
                        },
                        {
                            field: 'monsterLimit',
                            title: '怪兽限定',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                if (val == true) {
                                    return "是";
                                } else if (val == false) {
                                    return "否";
                                } else {
                                    return "未知状态";
                                }
                            }
                        },
                        {
                            field: 'weekStarLimit',
                            title: '周星榜限定',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                if (val == true) {
                                    return "是";
                                } else if (val == false) {
                                    return "否";
                                } else {
                                    return "未知状态";
                                }
                            }
                        },
                        {
                            field: 'activityLimit',
                            title: '活动限定',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                if (val == true) {
                                    return "是";
                                } else if (val == false) {
                                    return "否";
                                } else {
                                    return "未知状态";
                                }
                            }
                        },
                        { field: 'limitDesc', title: '限定描述', align: 'center', width: '5%' },
                        {
                            field: 'createTime',
                            title: '创建时间',
                            align: 'center',
                            width: '5%',
                            valign: 'middle',
                            formatter: function (val, row, index) {
                                if (val) {
                                    var date = new Date(val);
                                    return date.format("yyyy-MM-dd hh:mm:00");
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'headwearId',
                            title: '操作',
                            align: 'center',
                            width: '5%',
                            formatter: function (val, row, index) {
                                return '<button id="btnEdit" name="btnEdit" class="btn btn-sm btn-success opt-edit" data-id=' + val + '>' +
                                    '<i class="glyphicon glyphicon-edit"></i> 编辑</button>&nbsp;&nbsp;' +
                                    '<button id="btnSend" name="btnSend" class="btn btn-sm btn-success opt-send" data-id=' + val + '>' +
                                    '<i class="glyphicon glyphicon-edit"></i> 赠送</button>&nbsp;&nbsp;';
                            }
                        }
                    ],
                    cache: false,
                    striped: true,
                    showRefresh: false,
                    pageSize: 20,
                    pagination: true,
                    pageList: [20, 50, 100, 200, 300, 500],
                    search: false,
                    sidePagination: "server", //表示服务端请求
                    //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
                    //设置为limit可以获取limit, offset, search, sort, order
                    queryParamsType: "undefined",
                    queryParams: function queryParams(params) {   //设置查询参数
                        var param = {
                            page: params.pageNumber,
                            pageSize: params.pageSize,
                            headwearId: $('#sheadwearId').val(),
                            headwearName: $('#sheadwearName').val(),
                            limitType: $('#slimitType').val(),
                            enable: $('#senable').val(),
                            partitionId: $('#partitionId').val(),
                        };
                        return param;
                    },
                    ajax:function(request){                    //使用ajax请求
                        $.ajax({
                            type:"GET",
                            url:'/admin/headwear/list.action',
                            contentType:'application/json;charset=utf-8',
                            dataType:'json',
                            data: request.data,
                            success:function (res) {
                                console.log(res);
                                request.success({
                                    "rows": res.rows.map(i=>{
                                        i.name = JSON.parse(i.name);
                                        return i;
                                    }),
                                    "total": res.total
                                });
                            },
                            error:function(error){
                                console.log(error);
                            }
                        })
                    },
                    toolbar: '#toolbar',
                    // url: '/admin/headwear/list.action',
                    onLoadSuccess: function () {  //加载成功时执行
                        console.log("load success");
                    },
                    onLoadError: function () {  //加载失败时执行
                        console.log("load fail");
                    }
                });

                // 查询刷新
                $('#btnSearch').on('click', function () {
                    TableHelper.doRefresh('#table');
                });

                // 新建头饰
                $("#btnAdd").click(function () {
                    $("#headwearId").val("");
                    $("#headwearName").val("");
                    $("#headwearArName").val("");
                    $("#headwearEnName").val("");
                    $("#price").val("");
                    $("#renewPrice").val("");
                    $("#originalPrice").val("");
                    $("#days").val("");

                    $("#nobleLimit").prop("checked", false);
                    $("#monsterLimit").prop("checked", false);
                    $("#activityLimit").prop("checked", false);
                    $("#weekStarLimit").prop("checked", false);

                    $("#limitDesc").val("");
                    $("#enable").prop("checked", false);

                    $('#pic').val('');
                    $('#picImage').attr("src", '');
                    $("#picInfo").html('');
                    $("#picFile").val('');

                    $('#effect').val('');
                    $('#effectImage').attr("src", '');
                    $("#effectInfo").html('');
                    $("#effectFile").val('');

                    $("#radishPrice").val("");
                    $("#radishRenewPrice").val("");
                    $("#radishOriginalPrice").val("");

                    $("#seq").val("");
                    $("#redirectLink").val('');
                    $("input:radio[name='goldSale']")[0].checked = true;
                    $("input:radio[name='radishSale']")[0].checked = true;
                    $("input:radio[name='labelType']")[0].checked = true;
                    
                    $('input[name="partitionFlag"]').each(function() {
                        $(this).checked = false;
                    });
                    $("#headwearModal").modal('show');
                });

                $("#add").click(function () {
                    var headwearId = $("#headwearId").val();
                    var headwearName = $("#headwearName").val();
                    var headwearArName = $("#headwearArName").val();
                    var headwearEnName = $("#headwearEnName").val();
                    // 国际化字段
                    var jsonName = {"zh":headwearName,"ar":headwearArName,"en":headwearEnName}
                    const nameValue = JSON.stringify(jsonName);
                    var price = $("#price").val();
                    var renewPrice = $("#renewPrice").val();
                    var days = $("#days").val();

                    var nobleLimit = $("#nobleLimit").is(":checked");
                    var monsterLimit = $("#monsterLimit").is(":checked");
                    var activityLimit = $("#activityLimit").is(":checked");
                    var weekStarLimit = $("#weekStarLimit").is(":checked");

                    var limitDesc = $("#limitDesc").val();
                    var enable = $("#enable").is(":checked");
                    var pic = $("#pic").val();
                    var effect = $("#effect").val();
                    var seq = $("#seq").val();
                    var redirectLink = $("#redirectLink").val();
                    var labelType = $('input[name="labelType"]:checked').val();
                    var originalPrice = $("#originalPrice").val();
                    var isSale = $('input[name="isSale"]:checked').val();
                    var goldSale = $('input[name="goldSale"]:checked').val();
                    var radishSale = $('input[name="radishSale"]:checked').val();
                    var radishOriginalPrice = $("#radishOriginalPrice").val();
                    var radishPrice = $("#radishPrice").val();
                    var radishRenewPrice = $("#radishRenewPrice").val();

                    //分区
                    const partitionFlagArray = $("input:checkbox[name='partitionFlag']:checked").serializeArray();
                    let partitionFlag = 0;
                    for (let i = 0, len = partitionFlagArray.length; i < len; i++) {
                        console.log(partitionFlagArray[i]);
                        partitionFlag |= partitionFlagArray[i].value;
                    }
                    if (goldSale == 0 && radishSale == 0) {
                        $("#tipMsg").text("请选择一种支持的购买方式");
                        $("#tipModal").modal('show');
                        return;
                    }
                    if (goldSale == 1 && (!price || !renewPrice || !originalPrice)) {
                        $("#tipMsg").text("请填写完整金币价格");
                        $("#tipModal").modal('show');
                        return;
                    } else if (radishSale == 1 && (!radishPrice || !radishRenewPrice || !radishOriginalPrice)) {
                        $("#tipMsg").text("请填写完整萝卜价格");
                        $("#tipModal").modal('show');
                        return;
                    }
                    if (!pic) {
                        $("#tipMsg").text("请上传座驾图片");
                        $("#tipModal").modal('show');
                        return;
                    }
                    if (!effect) {
                        $("#tipMsg").text("请上传动画图片");
                        $("#tipModal").modal('show');
                        return;
                    }

                    if (!days || days <= 0) {
                        $("#tipMsg").text("请输入合理的购买天数");
                        $("#tipModal").modal('show');
                        return;
                    }

                    if ($("#addForm").validationEngine('validate')) {
                        $.ajax({
                            type: "post",
                            url: "/admin/headwear/save.action",
                            data: {
                                headwearId: headwearId,
                                name: nameValue,
                                price: price,
                                renewPrice: renewPrice,
                                days: days,
                                nobleLimit: nobleLimit,
                                monsterLimit: monsterLimit,
                                activityLimit: activityLimit,
                                weekStarLimit: weekStarLimit,
                                limitDesc: limitDesc,
                                enable: enable,
                                pic: pic,
                                effect: effect,
                                seq: seq,
                                labelType: labelType,
                                originalPrice: originalPrice,
                                redirectLink: redirectLink,
                                isSale: isSale,
                                goldSale: goldSale,
                                radishSale: radishSale,
                                radishOriginalPrice: radishOriginalPrice,
                                radishPrice: radishPrice,
                                radishRenewPrice: radishRenewPrice,
                                partitionFlag: partitionFlag,
                            },
                            dataType: "json",
                            success: function (json) {
                                if (json.success == 'true') {
                                    $("#tipMsg").text("保存成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                    $("#headwearModal").modal('hide');
                                } else {
                                    $("#tipMsg").text("保存失败." + json.msg);
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                    $("#headwearModal").modal('hide');
                                }
                            }
                        });
                    }
                    
                });

                $("#table").on("click", '.opt-edit', function () {
                    var id = $(this).attr("data-id");
                    $.ajax({
                        type: "get",
                        url: "/admin/headwear/get.action",
                        data: { headwearId: id },
                        dataType: "json",
                        success: function (json) {
                            if (json) {
                                $("#headwearId").val(id);
                                //$("#name").attr('readonly', true);
                                //$("#price").attr('readonly', true);
                                //$("#renewPrice").attr('readonly', true);

                                var jsonName = JSON.parse(json.name);
                                $("#headwearName").val(jsonName.zh);
                                $("#headwearArName").val(jsonName.ar);
                                $("#headwearEnName").val(jsonName.en);
                                $("#price").val(json.price);
                                $("#renewPrice").val(json.renewPrice);
                                $("#limitDesc").val(json.limitDesc);
                                $("#days").val(json.days);
                                $("#seq").val(json.seq);
                                $("#originalPrice").val(json.originalPrice);
                                $("#labelType").val(json.labelType);
                                $("#redirectLink").val(json.redirectLink);
                                $("#radishPrice").val(json.radishPrice);
                                $("#radishRenewPrice").val(json.radishRenewPrice);
                                $("#radishOriginalPrice").val(json.radishOriginalPrice);

                                $("#enable").checked = json.enable;

                                if (json.goldSale == true) {
                                    $("input:radio[name='goldSale']")[1].checked = true;
                                } else {
                                    $("input:radio[name='goldSale']")[0].checked = true;
                                }

                                if (json.radishSale == true) {
                                    $("input:radio[name='radishSale']")[1].checked = true;
                                } else {
                                    $("input:radio[name='radishSale']")[0].checked = true;
                                }

                                // 礼物状态的值跟其他不同, 1为有效 0为无效
                                if (json.enable == true) {
                                    //$("input:radio[name='enable']")[0].checked = true;
                                    $("#enable").prop("checked", true);
                                    $("#disable").prop("checked", false);
                                } else {
                                    //$("input:radio[name='enable']")[1].checked = true;
                                    $("#enable").prop("checked", false);
                                    $("#disable").prop("checked", true);
                                }

                                if (json.isSale == 1) {
                                    $("input:radio[name='isSale']")[0].checked = true;
                                } else {
                                    $("input:radio[name='isSale']")[1].checked = true;
                                }

                                // 贵族限定
                                if (json.nobleLimit == true) {
                                    $("#nobleLimit").prop("checked", true);
                                } else {
                                    $("#nobleLimit").prop("checked", false);
                                }

                                // 怪兽限定
                                if (json.monsterLimit == true) {
                                    $("#monsterLimit").prop("checked", true);
                                } else {
                                    $("#monsterLimit").prop("checked", false);
                                }

                                // 活动限定
                                if (json.activityLimit == true) {
                                    $("#activityLimit").prop("checked", true);
                                } else {
                                    $("#activityLimit").prop("checked", false);
                                }

                                // 周星榜限定
                                if (json.weekStarLimit == true) {
                                    $("#weekStarLimit").prop("checked", true);
                                } else {
                                    $("#weekStarLimit").prop("checked", false);
                                }

                                // 设置礼物图片
                                $('#pic').val(json.pic);
                                $('#picImage').attr("src", json.pic);
                                if (json.pic != '') {
                                    $("#picImgInfo").html('已上传');
                                } else {
                                    $("#picImgInfo").html('未上传');
                                }

                                // 设置动画效果图片
                                $('#effect').val(json.effect);
                                $('#effectImage').attr("src", json.effect);
                                if (json.effect != '') {
                                    $("#effectImgInfo").html('已上传');
                                } else {
                                    $("#effectImgInfo").html('未上传');
                                }

                                var labelType = json.labelType;
                                switch (labelType) {
                                    case 0:
                                        $("#noOne").prop("checked", true);
                                        break;
                                    case 1:
                                        $("#newOne").prop("checked", true);
                                        break;
                                    case 2:
                                        $("#discountOne").prop("checked", true);
                                        break;
                                    case 3:
                                        $("#limitOne").prop("checked", true);
                                        break;
                                    case 4:
                                        $("#exclusiveOne").prop("checked", true);
                                        break
                                }

                                //分区
                                let partitionFlag = json.partitionFlag;
                                if (partitionFlag & 1 != 0) {
                                    $("input:radio[name='partitionFlag']")[0].checked = true;
                                } else {
                                    $("input:radio[name='partitionFlag']")[0].checked = false; 
                                }
                                if (partitionFlag & 2 != 0) {
                                    $("input:radio[name='partitionFlag']")[1].checked = true;
                                } else {
                                    $("input:radio[name='partitionFlag']")[1].checked = false; 
                                }
                                if (partitionFlag & 4 != 0) {
                                    $("input:radio[name='partitionFlag']")[2].checked = true;
                                } else {
                                    $("input:radio[name='partitionFlag']")[2].checked = false; 
                                }

                                // 打开编辑弹窗
                                $("#headwearModal").modal('show');
                            } else {
                                $("#tipMsg").text("获取菜单信息出错");
                                $("#tipModal").modal('show');
                            }
                        }
                    });
                });

                //打开赠送头饰弹窗
                $("#table").on('click', '.opt-send', function () {
                    //清除数据
                    $('#sendHeadwearModal').find('input').val('');
                    $('#sendHeadwearModal').find('textarea').val('');
                    //设置数据
                    var headWearId = $(this).attr("data-id");
                    $('#sendHeadwearId').val(headWearId);
                    $('#sendHeadwearModal').modal('show');
                });

                //赠送头饰
                $("#send").click(function () {
                    debugger;
                    var erbanNo = $('#sendErbanNo').val().trim();//去掉首尾的空格和换行符
                    if (!(erbanNo.length > 0)) {
                        $("#tipMsg").text("请输入平台号");
                        $("#tipModal").modal('show');
                        return;
                    }

                    if (!$('#sendDays').val()) {
                        $("#tipMsg").text("请输入体验天数");
                        $("#tipModal").modal('show');
                        return;
                    }
                    var sendDays = $('#sendDays').val();
                    var sendDesc = $('#sendDesc').val();
                    var headwearId = $('#sendHeadwearId').val();
                    $("#tipMsg").text("赠送中，请稍后...");
                    $("#tipModal").modal('show');
                    if ($("#HeadwearSendForm").validationEngine('validate')) {
                        $.ajax({
                            type: "post",
                            url: "/admin/headwear/send.action",
                            data: {
                                headwearId: headwearId,
                                erbanNo: erbanNo,
                                sendDesc: sendDesc,
                                days: sendDays
                            },
                            dataType: "json",
                            success: function (json) {
                                if (json.code == 100) {
                                    $('#sendHeadwearModal').modal('hide');
                                    $("#tipMsg").text("全部赠送成功");
                                    TableHelper.doRefresh("#table");
                                } else if (json.code == 200) {
                                    $("#tipMsg").text("部分用户赠送失败,赠送失败平台号:" + json.erban + ",赠送失败原因:" + json.message);
                                } else {
                                    $("#tipMsg").text("全部赠送失败,赠送失败原因:" + json.message);
                                }
                            }
                        });
                    }
                });
            });
        },
        initPartition() {
            getPartitionInfoList().then(res => {
                let data = res.data;
                buildSelectOption(
                    "#partitionId",
                    null,
                    data.map((v) => {
                    return {
                        value: v.id,
                        text: v.desc,
                    };
                    })
                );
            });
        },
    },
};
</script>

<style scoped></style>