<?php
/*
 * @author: CloudZA(云之安) <2922619853@qq.com>
 * @hitokoto: 一场秋雨一场凉，秋心酌满泪为霜。
 * Copyright (c) 2022 by CloudZA, All Rights Reserved.
 */

include_once 'common.php';
if ( !isset($islogin)) header("Location: /");//非法访问
$title = '套餐设置';
include_once 'partials/head.php';
//$res = Db::table('web_notice')->find();
$res = [];
?>
<div id="pjax-container">
<!--导航-->
<div class="bg-body-light">
  <div class="content content-full">
    <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2">
      <div class="flex-grow-1">
        <h1 class="h3 fw-bold mb-2">
          <?php echo $title ?>
        </h1>
      </div>
      <nav class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3" aria-label="breadcrumb">
        <ol class="breadcrumb breadcrumb-alt">
          <li class="breadcrumb-item">
            <a class="link-fx" href="javascript:void(0)">网站设置</a>
          </li>
          <li class="breadcrumb-item" aria-current="page">
            <?php echo $title ?>
          </li>
        </ol>
      </nav>
    </div>
  </div>
</div>
<!--导航结束-->
<!--内容-->
<div class="content">
    <link rel="stylesheet" id="css-main" href="<?php echo __CSS__ ?>bootstrap-table.css">
    <div class="block block-rounded">
        <div class="block-header block-header-default">
            <h3 class="block-title"> <?php echo $title ?> </h3>
        </div>
        <div class="block-content">
            <form method="" class="form-inline" id="toolbar">
            <div class="dropdown d-inline-block">
                <button type="button" class="btn btn-light btn-alt-secondary btn-xs" style="padding: 0.4375rem 0.925rem" onclick="viewAddPlan(true)">
                    <span class="d-sm-inline-block fw-semibold">添加套餐</span>
                </button>
            </div>
                <a class="btn btn-light btn-alt-secondary btn-xs" style="padding: 0.4375rem 0.925rem" href="javascript:delChecks()">
                    <span class="d-sm-inline-block fw-semibold">删除选中</span>
                </a>

        </form>
            <table class="text-center fs-sm mb-2 ths" id="table"></table>
        </div>
    </div>

    <div class="block block-rounded block-content p-2" id="addPlans" style="display: none;margin-bottom: 0!important;">
        <input type="hidden" id="uid" name="uid">
        <div class="text-center fw-semibold fs-sm" id="plan_title">添加套餐</div>
        <div class="form-group mb-2">
            <label class="form-label fs-sm text-muted" for="plan_name">套餐名称</label>
            <input type="text" name="plan_name" id="plan_name" value="" class="form-control" placeholder="比如：月付" required />
        </div>
        <div class="form-group mb-2">
            <label class="form-label fs-sm text-muted" for="plan_period">有效期(天)</label>
            <input type="text" name="plan_period" id="plan_period" value="" class="form-control" placeholder="比如：30" required />
        </div>
        <div class="form-group mb-2">
            <label class="form-label fs-sm text-muted" for="plan_price">套餐价格</label>
            <input type="text" name="plan_price" id="plan_price" value="" class="form-control" placeholder="比如：15" required />
        </div>
        <div class="form-group mb-2">
            <label class="form-label fs-sm text-muted" for="plan_chat_limit">对话配额</label>
            <input type="text" name="plan_chat_limit" id="plan_chat_limit" value="" class="form-control" placeholder="比如：200" required />
        </div>

        <button class="btn btn-sm btn-alt-primary me-1 m-1" id="submitPlans">
            <i class="fa fa-bolt opacity-50 me-1"></i> 保存
        </button>
        <button class="btn btn-sm btn-alt-primary me-1 m-1" id="editPlans" onclick="editPlans(false)" style="display:none;">
            <i class="fa fa-bolt opacity-50 me-1"></i> 保存
        </button>
    </div>
</div>
<script>
    function viewAddPlan (type = true, id = '')
    {
        if (type) {
            app.getval('#plan_title', '添加套餐')
            app.getval('#plan_name', '')
            app.getval('#plan_period', '')
            app.getval('#plan_price', '')
            app.getval('#plan_chat_limit', '')
        }
        else {
            app.getval('#uid', id)
        }
        layer.open({
            type   : 1,
            shade  : false, // 遮障可点击，透明度为0.5
            title  : false,
            content: $('#addPlans'),
            cancel : () => {

            },
        })
    }

    $('#submitPlans').click(() => {
        app.ajax('ajax_table.php?act=addPlans', {
            plan_name      : app.getval('#plan_name'),
            plan_period    : app.getval('#plan_period'),
            plan_price     : app.getval('#plan_price'),
            plan_chat_limit: app.getval('#plan_chat_limit')
        }, res => {
            if (res.code === 200) {
                app.btn(res.msg, '我知道了', null, () => {
                    app.pjax('plans.php')
                })
            }
            else {
                app.btn(res.msg, '我知道了')
            }
        })
    })

    function delChecks (id = '')
    {
        let selected = [];
        if (id === '') {
            // 获取选中的行
            selected = $('#table').bootstrapTable('getSelections');
            if (selected.length === 0) {
                app.notify('未选择记录', 'warning');
                return;
            }
        }
        else {
            // 添加指定的id
            selected.push({id: id});
        }

        const checkbox = [];
        $.each(selected, function (key, item) {
            checkbox.push(item.id);
        });

        console.log(checkbox);
        app.btn('确定要删除选中的内容吗？<br/>删除套餐同时也会删除套餐绑定的卡密(不影响已使用的)<br/>您确定要删除吗？', '确定', '取消', () => {
            app.ajax('ajax_table.php?act=delPlans', {
                id: checkbox
            }, res => {
                if (res.code === 200) {
                    app.notify(res.msg, 'success')
                    setTimeout(() => {
                        app.pjax('plans.php');
                    }, 1000)
                }
                else {
                    app.notify(res.msg, 'warning')
                }
            })
        }, null)
    }

    function editPlans (type = false, id = '', name = '', period = '', price = '', chat_limit = '')
    {
        if (type) {
            $('#submitPlans').hide();
            $('#editPlans').show();
            app.getval('#plan_title', '编辑套餐')
            app.getval('#plan_name', name)
            app.getval('#plan_period', period)
            app.getval('#plan_price', price)
            app.getval('#plan_chat_limit', chat_limit)
            viewAddPlan(false, id)
        }
        else {
            app.ajax('ajax_table.php?act=editPlans', {
                id             : app.getval('#uid'),
                plan_name      : app.getval('#plan_name'),
                plan_period    : app.getval('#plan_period'),
                plan_price     : app.getval('#plan_price'),
                plan_chat_limit: app.getval('#plan_chat_limit')
            }, res => {
                if (res.code === 200) {
                    app.btn(res.msg, '我知道了', null, () => {
                        app.pjax('plans.php')
                    })
                }
                else {
                    app.btn(res.msg, '我知道了')
                }
            })
        }
    }

    app.ajax('ajax_table.php?act=plan_lists', null, res => {
        if (res.code === 200) {
            let columns = [
                {
                    field   : 'A',
                    checkbox: true,
                },
                {
                    field: 'name',
                    title: '套餐名称'
                }, {
                    field: 'period',
                    title: '有效期(天)'
                },
                {
                    field: 'price',
                    title: '价格'
                },
                {
                    field: 'chat_limit',
                    title: '对话额度'
                },
                {
                    field: 'id',
                    title: '操作',
                    formatter (value, row, index)
                    {
                        let html = '<button type="button" class="btn btn-sm btn-alt-primary dropdown-toggle" id="dropdown-default-alt-secondary" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">设置</button><div class="dropdown-menu fs-sm" aria-labelledby="dropdown-default-alt-secondary">';
                        html += '<a class="dropdown-item d-flex align-items-center justify-content-between space-x-1" href="javascript:editPlans(true,\'' + row.id + '\',\'' + row.name + '\',\'' + row.period + '\',\'' + row.price + '\',\'' + row.chat_limit + '\')"><span>修改套餐</span> </a><div class="dropdown-divider"></div><a class="dropdown-item d-flex align-items-center justify-content-between space-x-1" href="javascript:delChecks(\'' + row.id + '\')"><span>删除</span></a>';
                        html += '</div>';
                        return html;
                    }
                }
            ];
            table.initTable('table', columns, res.data.custom, null, res.data.page);
        }
    })
</script>
</div>
<?php include "partials/foot.php"; ?>
