<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 -->
                <section class="content">
                    <div id="table"></div>
                    <div id="toolbar">
                        <div class="col-sm-12">
                            <label for="query_carGoodsName" class="col-sm-1 control-label">名称:</label>
                            <div class="col-sm-3"><input type="text" class="form-control" name="query_carGoodsName"
                                    id="query_carGoodsName" /></div>

                            <label for="query_carGoodsType" class="col-sm-1 control-label">类型:</label>
                            <div class="col-sm-3">
                                <select name="query_carGoodsType" id="query_carGoodsType" class="form-control">
                                    <option value="" selected="selected">全部</option>
                                    <option value="1">普通座驾</option>
                                    <option value="2">贵族座驾</option>
                                </select>
                            </div>

                            <label for="condition_type" class="col-sm-1 control-label">状态:</label>
                            <div class="col-sm-3">
                                <select name="condition_type" id="condition_type" class="form-control">
                                    <option value="-1" selected="selected">全部</option>
                                    <option value="2">启用</option>
                                    <option value="1">未启用</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-12">
                            <label for="partitionId" class="col-sm-1 control-label">地区:</label>
                            <div class="col-sm-3">
                                <select name="partitionId" id="partitionId" class="form-control"></select>
                            </div>
                        </div>
                        <div class="col-sm-12">
                            <button id="car-goods-refresh" class="btn btn-default">
                                <i class="glyphicon glyphicon-search"></i>查询
                            </button>
                            <button id="car-goods-add" class="btn btn-default">
                                <i class="glyphicon glyphicon-plus"></i>增加
                            </button>
                        </div>

                    </div>
                </section><!-- .content -->
            </div>
        </div>
    </section>
    <div class="modal fade" id="cardGoodsModal" 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="carGoodsForm">
                        <input type="hidden" name="id" id="id" />
                        <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="carGoodsType" class="col-sm-3 control-label">座驾类型:</label>
                            <div class="col-sm-9">
                                <select name="carGoodsType" id="carGoodsType" data-btn-class="btn-warning"
                                    class="form-control">
                                    <option value="">请选择</option>
                                    <option value="1">普通座驾</option>
                                    <option value="2">贵族座驾</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="name" class="col-sm-3 control-label">座驾名称</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control validate[required]" name="name" id="name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="ar_name" class="col-sm-3 control-label">阿语座驾名称</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control validate[required]" name="name" id="ar_name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="en_name" class="col-sm-3 control-label">英语座驾名称</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control validate[required]" name="name" id="en_name">
                            </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 validate[required]" 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="enable" value="2" checked>启用</label>
                                <label class="radio-inline"><input type="radio" name="enable" value="1">未启用</label>
                            </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 class="col-sm-3 control-label">贵族限定</label>
                            <div class="col-sm-3">
                                <label class="radio-inline"><input type="radio" name="isNobleLimit" value="0"
                                        checked>否</label>
                                <label class="radio-inline"><input type="radio" name="isNobleLimit" value="1">是</label>
                            </div>

                            <label class="col-sm-3 control-label">怪兽限定</label>
                            <div class="col-sm-3">
                                <label class="radio-inline"><input type="radio" name="isMonsterLimit" value="0"
                                        checked>否</label>
                                <label class="radio-inline"><input type="radio" name="isMonsterLimit" value="1">是</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">周星榜限定</label>
                            <div class="col-sm-3">
                                <label class="radio-inline"><input type="radio" name="isWeekStarLimit" value="0"
                                        checked>否</label>
                                <label class="radio-inline"><input type="radio" name="isWeekStarLimit" value="1">是</label>
                            </div>

                            <label class="col-sm-3 control-label">活动限定</label>
                            <div class="col-sm-3">
                                <label class="radio-inline"><input type="radio" name="isActivityLimit" value="0"
                                        checked>否</label>
                                <label class="radio-inline"><input type="radio" name="isActivityLimit" value="1">是</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="limitTipMsg" class="col-sm-3 control-label">限定提示信息</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="limitTipMsg" id="limitTipMsg">
                            </div>
                        </div>
                        <div class="form-group">
                            <label 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="picUploadFile" name="file">
                                <button class="btn btn-success" type="button" id="picUploadBtn">上传</button>
                                <input type="hidden" id="pic" name="pic" class="form-control validate[required]" />
                                <span id="picImgInfo" style="color:red;"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label 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="vggUploadFile" name="file">
                                <button class="btn btn-success" type="button" id="vggUploadBtn">上传</button>
                                <input type="hidden" id="effect" name="effect" class="form-control validate[required]" />
                                <span id="effectImgInfo" style="color:red;"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="otherViewType" class="col-sm-3 control-label">其他视图文件类型:</label>
                            <div class="col-sm-9">
                                <select name="otherViewType" id="otherViewType" data-btn-class="btn-warning">
                                    <option value="0">全部</option>
                                    <option value="1">MP4</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">其他视图文件:</label>
                            <div class="col-sm-9">
                                <img src="" id="viewFile" style="width:250px;height:90px;" alt="">
                                <input type="file" id="viewUploadFile" name="file">
                                <button class="btn btn-success" type="button" id="viewFileUploadBtn">上传</button>
                                <input type="hidden" id="viewUrl" name="viewUrl" class="form-control validate[required]" />
                                <span id="viewFileInfo" style="color:red;"></span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="nobleId" class="col-sm-3 control-label">贵族等级</label>
                            <div class="col-sm-9">
                                <select name="nobleId" id="nobleId" data-btn-class="btn-warning">
                                    <option value="0">&nbsp;&nbsp;</option>
                                    <option value="1">男爵</option>
                                    <option value="2">子爵</option>
                                    <option value="3">伯爵</option>
                                    <option value="4">侯爵</option>
                                    <option value="5">公爵</option>
                                    <option value="6">国王</option>
                                    <option value="7">皇帝</option>
                                </select>
                            </div>
                        </div>
                        <input type="hidden" id="nobleName" name="nobleName">

                        <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 validate[required]" 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="limitDesc" class="col-sm-3 control-label">限定描述:</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="limitDesc" placeholder="限定和专属需要填写描述，限制30字"
                                    id="limitDesc">
                            </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="save">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 为用户赠送座驾弹窗 -->
    <div class="modal fade" id="sendCarModal" 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="carSendForm">
                        <input type="hidden" name="sendCarId" id="sendCarId" />
                        <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">
                                <select name="days" id="sendDays">
                                    <option value="">-- 请选择 --</option>
                                    <option value="1">1天</option>
                                    <option value="3">3天</option>
                                    <option value="5">5天</option>
                                    <option value="7">7天</option>
                                    <option value="15">15天</option>
                                    <option value="30">30天</option>
                                    <option value="999">自定义</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group otherZDY" style="display: none">
                            <label for="otherVal" class="col-sm-3 control-label">自定义</label>
                            <div class="col-sm-9">
                                <input type="text" id="otherVal" class="" oninput="value=value.replace(/[^\d]/g,'')" />
                            </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>

    <!--<div class="modal fade" id="tipModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">提示信息</h4>
            </div>
            <div class="modal-body" id="tipMsg"></div>
        </div>
    </div>
</div>-->
</template>

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

export default {
    name: "CarGoodsAdminView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initPartition();
            this.initData();
        });
    },
    methods: {
        initData() {
            ComboboxHelper.build(null, '#enable');
            //ComboboxHelper.build(null,'#condition_type');
            ComboboxHelper.build(null, '#nobleId');
            $(function () {
                //选择select时触发
                $("#sendDays").change(function () {
                    var val = $(this).val();
                    if (val == "999") {
                        $('.otherZDY').show();
                    } else {
                        $('.otherZDY').hide();
                        $('.otherZDY input').val('');
                    }
                    console.log(val);
                });
                $('#table').bootstrapTable('destroy');
                $('#table').bootstrapTable({
                    columns: [
                        { field: 'tmp', title: 'id', align: 'center', checkbox: true, width: '5%' },
                        { field: 'id', title: '座驾ID', align: 'center', width: '5%' },
                        {field: 'name.zh', title: '座驾名称', align: 'center', width: '20%'},
                        {field: 'name.ar', title: '阿语座驾名称', align: 'center', width: '20%'},
                        {field: 'name.en', title: '英语座驾名称', align: 'center', width: '20%'},
                        {
                            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: 'carGoodsType', title: '座驾类型', align: 'center', width: '20%',
                            formatter: function (val) {
                                if (val == 1) {
                                    return '普通座驾';
                                } else if (val == 2) {
                                    return '贵族座驾';
                                }
                            }
                        },
                        {
                            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: 'enable', title: '启用状态', align: 'center', width: '15%', formatter: function (val, row, index) {
                                if (val == 1) {
                                    return '未启用';
                                } else if (val == 2) {
                                    return '启用';
                                } else {
                                    return '无效';
                                }
                            }
                        },
                        { field: 'days', title: '购买天数', align: 'center', width: '10%' },
                        {
                            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: 'limitDesc', title: '限定描述', align: 'center', width: '5%' },
                        { field: 'redirectLink', title: '跳转链接', align: 'center', width: '5%' },
                        { field: 'seq', title: '排序', align: 'center', width: '10%' },
                        {
                            field: 'id', title: '操作', align: 'center', width: '30%', formatter: function (val, row, index) {
                                let isSendDisabled = false;
                                if (row.carGoodsType === 2) {
                                    isSendDisabled = true;
                                }
                                return '<button class="btn btn-sm btn-success opt-edit" data-id=' + val + '>' +
                                    '<i class="glyphicon glyphicon-edit"></i>编辑</button>' +
                                    '<button class="btn btn-sm btn-success opt-send" data-id=' + val + "" + `${isSendDisabled ? ' disabled ' : ''}` + '>' +
                                    '<i class="glyphicon glyphicon-send"></i>赠送</button>';/* +
                            '&nbsp;&nbsp;<button class="btn btn-sm btn-danger opt-remove" data-id=' + val +
                            '><i class="glyphicon glyphicon-remove"></i>删除</button>';*/
                            }
                        }
                    ],
                    cache: false,
                    striped: true,
                    showRefresh: false,
                    pageSize: 20,
                    pagination: true,
                    pageList: [1, 20, 50, 100, 200],
                    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,
                            searchText: params.searchText,
                            carGoodsName: $("#query_carGoodsName").val(),
                            carGoodsType: $("#query_carGoodsType").val(),
                            enable: $("condition_type").val(),
                            partitionId: $('#partitionId').val(),
                        };
                        return param;
                    },
                    ajax:function(request){                    //使用ajax请求
                        $.ajax({
                            type:"GET",
                            url:'/admin/car/goods?enable=' + ComboboxHelper.getSelected("#condition_type"),
                            contentType:'application/json;charset=utf-8',
                            dataType:'json',
                            data: request.data,
                            success:function (res) {
                                console.log(res);
                                request.success({
                                    "rows": res.rows.map(i=>{
                                        let value = i.name;
                                        if (value.startsWith('{') && value.endsWith('}')) {
                                            i.name = JSON.parse(value);
                                        } else {
                                            i.name = {
                                                zh: value,
                                                ar: value,
                                                en: value,
                                            };
                                        }
                                        return i;
                                    }),
                                    "total": res.total
                                });
                            },
                            error:function(error){
                                console.log(error);
                            }
                        })
                    },
                    toolbar: '#toolbar',
                    onLoadSuccess: function () {  //加载成功时执行
                        console.log("load success");
                    },
                    onLoadError: function () {  //加载失败时执行
                        console.log("load fail");
                    }
                });

                /*$("#table").on("click", '.opt-remove', function () {
                    var id = $(this).attr("data-id");
                    if (id == 'undefined') {
                        $("#tipMsg").text("id参数有误");
                        $("#tipModal").modal('show');
                        return;
                    }
                    if (confirm("你确认删除该记录吗? \r\n 删除后再也不能找回，请谨慎操作！")) {
                        $.ajax({
                            type: 'post',
                            url: "/admin/car/goods/deletion",
                            data: {'ids': JSON.stringify([id])},
                            dataType: "json",
                            success: function (json) {
                                if (json.success == 'true') {
                                    $("#tipMsg").text("删除成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("删除失败");
                                    $("#tipModal").modal('show');
                                }
                            }
                        });
                    }
                });*/

                $("#car-goods-add").click(function () {
                    // 打开编辑弹窗
                    $("#cardGoodsModal").modal('show');
                    $("#carGoodsForm")[0].reset();

                    $('#pic').val('');
                    $('#picImage').attr("src", '');
                    $("#picImgInfo").html('');
                    $('#effect').val('');
                    $('#effectImage').attr("src", '');
                    $("#effectImgInfo").html('');

                    $('#viewUrl').attr('');
                    $('#viewFile').attr("src", '');
                    $("#viewFileInfo").html('');

                    $("#id").val('');
                    $('#name').val('');
                    $('#ar_name').val('');
                    $('#en_name').val('');
                    $("#name").attr('readonly', false);
                    $("#ar_name").attr('readonly', false);
                    $("#en_name").attr('readonly', false);
                    // $("#price").attr('readonly', false);
                    // $("#renewPrice").attr('readonly', false);
                    $("#seq").val('');
                    $("#limitTipMsg").val('');

                    $("#price").val("");
                    $("#renewPrice").val("");
                    $("#originalPrice").val("");
                    $("#radishPrice").val("");
                    $("#radishRenewPrice").val("");
                    $("#radishOriginalPrice").val("");

                    $("input:radio[name='goldSale']")[0].checked = true;
                    $("input:radio[name='radishSale']")[0].checked = true;
                    $("input:radio[name='enable']")[0].checked = true;
                    $("input:radio[name='isSale']")[0].checked = true;  
                    $("input:radio[name='isNobleLimit']")[0].checked = true;
                    $("input:radio[name='isMonsterLimit']")[0].checked = true;
                    $("input:radio[name='isWeekStarLimit']")[0].checked = true;
                    $("input:radio[name='isActivityLimit']")[0].checked = true;

                    $('input[name="partitionFlag"]').each(function() {
                        $(this).checked = false;
                    });
                    $("#nobleId").btComboBox('disable');
                    ComboboxHelper.setDef("#nobleId", '0');

                    ComboboxHelper.setDef("#enable", '2');
                });

                $("#nobleId").on('change', function () {
                    if (ComboboxHelper.getSelected("#nobleId") != 0) {
                        var nobleName = $("#nobleId").btComboBox('selectedOption')[1];
                        $("#nobleName").val(nobleName);
                    } else {
                        $("#nobleName").val('');
                    }

                })

                $("#car-goods-refresh").click(function () {
                    $("#table").bootstrapTable('refresh', { url: '/admin/car/goods' });
                })

                /*$("#car-goods-multiDel").click(function () {
                    var rows = $("#table").bootstrapTable("getSelections");
                    if (rows.length == 0) {
                        alert("请先选择要删除的记录");
                        return;
                    }
                    console.log("rows: " + rows.length);
                    var idArr = [];
                    for (var i = 0; i < rows.length; i++) {
                        idArr.push(rows[i]['id']);
                    }
                    console.log(idArr);
        
                    if (confirm("你确认批量删除该记录吗?" + "\r\n删除后再也不能找回，请谨慎操作！")) {
                        $.ajax({
                            type: 'post',
                            url: "/admin/car/goods/deletion",
                            data: {'ids': JSON.stringify(idArr)},
                            dataType: "json",
                            success: function (json) {
                                if (json.success == 'true') {
                                    $("#tipMsg").text("删除成功");
                                    $("#tipModal").modal('show');
                                    TableHelper.doRefresh("#table");
                                } else {
                                    $("#tipMsg").text("删除失败");
                                    $("#tipModal").modal('show');
                                }
                            }
                        });
                    }
                });*/

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

                            } else {
                                $("#tipMsg").text(json.msg);
                                $("#tipModal").modal('show');
                            }
                        },
                        error: function (data, status, e) {  //提交失败自动执行的处理函数。
                            console.error(e);
                        }
                    });
                })

                $('#vggUploadBtn').on('click', function () {
                    $.ajaxFileUpload({
                        url: '/admin/car/goods/upload',//后台请求地址
                        type: 'post',//请求方式  当要提交自定义参数时，这个参数要设置成post
                        secureuri: false,//是否启用安全提交，默认为false。
                        fileElementId: 'vggUploadFile',// 需要上传的文件域的ID，即<input type="file">的ID。
                        dataType: 'json',//服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。如果json返回的带pre,这里修改为json即可解决。
                        success: function (json, status) {//提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
                            console.log(json);
                            if (json.path) {
                                $('#effect').val(json.path);
                                $('#effectImage').attr("src", json.path);
                                if (json.path != '') {
                                    $("#effectImgInfo").html('已上传成功');
                                } else {
                                    $("#effectImgInfo").html('未上传成功');
                                }
                                console.log(json.path);
                            } else {
                                $("#tipMsg").text(json.msg);
                                $("#tipModal").modal('show');
                            }
                        }
                    });
                })

                $('#viewFileUploadBtn').on('click', function () {
                    $.ajaxFileUpload({
                        url: '/admin/car/goods/upload',//后台请求地址
                        type: 'post',//请求方式  当要提交自定义参数时，这个参数要设置成post
                        secureuri: false,//是否启用安全提交，默认为false。
                        fileElementId: 'viewUploadFile',// 需要上传的文件域的ID，即<input type="file">的ID。
                        dataType: 'json',//服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。如果json返回的带pre,这里修改为json即可解决。
                        success: function (json, status) {//提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
                            console.log(json);
                            if (json.path) {
                                $('#viewUrl').val(json.path);
                                $('#viewFile').attr("src", json.path);
                                if (json.path != '') {
                                    $("#viewFileInfo").html('已上传成功');
                                } else {
                                    $("#viewFileInfo").html('未上传成功');
                                }
                                console.log(json.path);
                            } else {
                                $("#tipMsg").text(json.msg);
                                $("#tipModal").modal('show');
                            }
                        }
                    });
                })

                $("#cancel").click(function () {
                    TableHelper.unCheckAll("#table");
                });

                function parseNum(val) {
                    if (val == undefined || val == 'false' || val == 0) {
                        return 0;
                    }
                    else {
                        return 1;
                    }
                }

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

                                //$("#name").val(json.entity.name);
                                // $("#price").val(json.entity.price);
                                // $("#renewPrice").val(json.entity.renewPrice);
                                $("#days").val(json.entity.days);
                                console.log(json.entity.name)
                                let name = json.entity.name;
                                if (name.startsWith('{') && name.endsWith('}')) {
                                    var jsonName = JSON.parse(json.entity.name);
                                    $("#name").val(jsonName.zh);
                                    $("#ar_name").val(jsonName.ar);
                                    $("#en_name").val(jsonName.en);
                                } else {
                                    $("#name").val(name);
                                    $("#ar_name").val(name);
                                    $("#en_name").val(name);
                                }
                                $("#price").val(json.entity.price);
                                $("#renewPrice").val(json.entity.renewPrice);
                                $("#seq").val(json.entity.seq);
                                $("#limitTipMsg").val(json.entity.limitTipMsg);

                                $("#originalPrice").val(json.entity.originalPrice);
                                $("#redirectLink").val(json.entity.redirectLink);
                                $("#limitDesc").val(json.entity.limitDesc);
                                $("#radishPrice").val(json.entity.radishPrice);
                                $("#radishRenewPrice").val(json.entity.radishRenewPrice);
                                $("#radishOriginalPrice").val(json.entity.radishOriginalPrice);
                                $("#carGoodsType").val(json.entity.carGoodsType);


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

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

                                //设置标签
                                var labelType = json.entity.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
                                }
                                ComboboxHelper.setDef("#enable", json.entity.enable);


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


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

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

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

                                // 设置其他视图文件效果图片
                                $('#otherViewType').val(json.entity.otherViewType);
                                $('#viewUrl').val(json.entity.viewUrl);
                                $('#viewFile').attr("src", json.entity.viewUrl);
                                if (json.entity.viewUrl != null && json.entity.viewUrl != 'undefined' && json.entity.viewUrl != '') {
                                    $("#viewFileInfo").html('已上传');
                                } else {
                                    $("#viewFileInfo").html('未上传');
                                }

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

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

                $("#save").click(function () {
                    if ($("#carGoodsForm").validationEngine('validate')) {
                        var goldSale = $('input[name="goldSale"]:checked').val();
                        var radishSale = $('input[name="radishSale"]:checked').val();
                        var price = $("#price").val();
                        var renewPrice = $("#renewPrice").val();
                        var originalPrice = $("#originalPrice").val();
                        var radishOriginalPrice = $("#radishOriginalPrice").val();
                        var radishPrice = $("#radishPrice").val();
                        var radishRenewPrice = $("#radishRenewPrice").val();
                        var pic = $('#pic').val();
                        var effect = $('#effect').val();
                        var otherViewType = $('#otherViewType').val();
                        var viewUrl = $('#viewUrl').val();
                        // console.log("$('#viewUrl').val() : "+ $('#viewUrl').val())
                        /*if (otherViewType !== 0){
                            viewUrl =$('#viewUrl').val();
                        }*/
                        var days = $('#days').val();
                        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 (otherViewType < 0) {
                            $("#tipMsg").text("请选择正确的文件类型");
                            $("#tipModal").modal('show');
                            return;
                        }
                        if (otherViewType > 0 && !viewUrl) {
                            $("#tipMsg").text("请上传对应类型的视图文件");
                            $("#tipModal").modal('show');
                            return;
                        }
                        // 获取序列化字符串并转换为对象
                        let formData = $('#carGoodsForm').serializeArray().reduce(function(obj, item) {
                            obj[item.name] = item.value;
                            return obj;
                        }, {});

                        var name = $("#name").val();
                        // 阿语名称
                        var ar_name = $("#ar_name").val();
                        // 英语名称
                        var en_name = $("#en_name").val();
                        // 国际化字段
                        var jsonName = {"zh":name,"ar":ar_name,"en":en_name}
                        const nameValue = JSON.stringify(jsonName);
                        // 修改属性值
                        formData.name = nameValue; // 将giftName修改为"newGiftName"
                        //分区
                        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;
                        }
                        formData.partitionFlag = partitionFlag;
                        // 将修改后的对象转换回序列化字符串
                        let newSerializeStr = $.param(formData);
                        $.ajax({
                            type: "post",
                            url: "/admin/car/goods",
                            data: newSerializeStr,
                            dataType: "json",
                            success: function (json) {
                                if (json.code == 200) {
                                    $("#carGoodsModal").modal('hide');
                                    $("#tipMsg").text("保存成功");
                                    $("#tipModal").modal('show');
                                    setTimeout(function() {
                                        TableHelper.doRefresh("#table");
                                        $("#cardGoodsModal").modal('hide');
                                    }, 1000); 
                                } else {
                                    $("#tipMsg").text("保存失败." + json.msg);
                                    $("#tipModal").modal('show');
                                    $("#cardGoodsModal").modal('hide');

                                }
                            }
                        });
                    }
                });
            });

            //打开赠送座驾弹窗
            $("#table").on("click", '.opt-send', function () {
                var id = $(this).attr("data-id");
                //清理数据
                $('#sendCarModal').find('input').val('');
                $('#sendCarModal').find('select').val('');
                $('#sendCarModal').find('textarea').val('');
                //保存数据
                $('#sendCarId').val(id);
                // 隐藏并清空自定义天数
                $('.otherZDY').hide();
                $('.otherZDY input').val('');
                //显示弹窗
                $('#sendCarModal').modal('show');
            });

            //赠送座驾
            $('#send').on('click', function () {
                var otherZDYVal = $('.otherZDY input').val();
                if (!($('#sendCarId').val() && $('#sendDays').val() && $('#sendDesc').val())) {
                    $("#tipMsg").text("请输入体验天数和备注");
                    $("#tipModal").modal('show');
                    return;
                }
                var erbanNo = $('#sendErbanNo').val().trim();//去掉首尾的空格和换行符
                if (!(erbanNo.length > 0)) {
                    $("#tipMsg").text("请输入平台号");
                    $("#tipModal").modal('show');
                    return;
                }
                var carId = $("#sendCarId").val();
                var sendDays = $("#sendDays").val();
                var sendDesc = $("#sendDesc").val();
                $("#tipMsg").text("赠送中，请稍后...");
                $("#tipModal").modal('show');
                $.ajax({
                    type: 'post',
                    url: "/admin/car/experByOfficial",
                    data: {
                        erbanNo: erbanNo,
                        carId: carId,
                        days: sendDays,
                        desc: sendDesc,
                        otherDay: otherZDYVal
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 100) {
                            $('#sendCarModal').modal('hide');
                            $("#tipMsg").text("全部赠送成功");
                            TableHelper.doRefresh("#table");
                        } else if (res.code == 200) {
                            $("#tipMsg").text("部分赠送成功，赠送失败平台号：" + res.erban + "，赠送失败原因：" + res.message);
                        } else {
                            $("#tipMsg").text("全部赠送失败，赠送失败原因：" + res.message);
                        }
                    }
                })

            });
        },
        initPartition() {
            getPartitionInfoList().then(res => {
                let data = res.data;
                buildSelectOption(
                    "#partitionId",
                    null,
                    [{
                        value: '',
                        text: '全部',
                    }].concat(data.map((v) => {
                    return {
                        value: v.id,
                        text: v.desc,
                    };
                    }))
                );
            });
        },
    },
};
</script>

<style scoped></style>