@using MineTec.ProManager.Admin.ViewResources
@using MineTec.ProManager.Authorization
@using MineTec.ProManager.TaskCenter.DailyTasks
@{
    ViewBag.CurrentPageName = PageNames.DailyTask;
}

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">日常任务</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dailyTask-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="dailyTask-table" lay-filter="dailyTask-table"></table>
    </div>
</div>

<script type="text/html" id="dailyTask-toolbar">
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_TaskCenter_DailyTasks_Create))
    {
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>新增
        </button>
    }
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_TaskCenter_DailyTasks_Delete))
    {
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    }
</script>

<script type="text/html" id="dailyTask-bar">
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_TaskCenter_DailyTasks_Update))
    {
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
    }
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_TaskCenter_DailyTasks_Delete))
    {
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    }
    <button class="pear-btn pear-btn-primary pear-btn-sm" id="more{{d.id}}">
        <i class="layui-icon layui-icon-triangle-d"></i>
    </button>
</script>

<!--时间段-->
<script type="text/html" id="dailyTask-dateRange">
    {{#
        var dateTimeConvertString = function(dateTime){
            return layui.util.toDateString(dateTime,'yyyy-MM-dd');
        };
    }}
    <div>
        {{dateTimeConvertString(d.startTime)}} - {{dateTimeConvertString(d.endTime)}}
    </div>
</script>

<!--更多操作-->
<script id="moreTool" type="text/plain">
    [
    {{#  layui.each(d.triggers, function(index, item){ }}
    {{# if(item.id == @TaskOperateTrigger.Progress.Id){ }}
    [{txt: "@TaskOperateTrigger.Progress.Name", event: "@TaskOperateTrigger.Progress.Name.ToLower()"}]
    {{#  }  }}
    {{# if(item.id == @TaskOperateTrigger.Resolve.Id){ }}
    [{txt: "@TaskOperateTrigger.Resolve.Name", event: "@TaskOperateTrigger.Resolve.Name.ToLower()"}]
    {{#  }  }}
    {{# if(item.id == @TaskOperateTrigger.Reopen.Id){ }}
    [{txt: "@TaskOperateTrigger.Reopen.Name", event: "@TaskOperateTrigger.Reopen.Name.ToLower()"}]
    {{#  }  }}
    {{# if(item.id == @TaskOperateTrigger.Qualify.Id){ }}
    [{txt: "@TaskOperateTrigger.Qualify.Name", event: "@TaskOperateTrigger.Qualify.Name.ToLower()"}]
    {{#  }  }}
    {{# if(item.id == @TaskOperateTrigger.Pend.Id){ }}
    [{txt: "@TaskOperateTrigger.Pend.Name", event: "@TaskOperateTrigger.Pend.Name.ToLower()"}]
    {{#  }  }}
    {{# if(item.id == @TaskOperateTrigger.Close.Id){ }}
    [{txt: "@TaskOperateTrigger.Close.Name", event: "@TaskOperateTrigger.Close.Name.ToLower()"}]
    {{#  }  }}
    {{#  });  }}
    ]
</script>

@section scripts{
    <script type="text/javascript">
        layui.use(['abp', 'form', 'table', 'dropdown'], function () {
            var $ = layui.$;
            var abp = layui.abp;
            var form = layui.form;
            var table = layui.table;
            var dropdown = layui.dropdown;

		    table.render({
		        elem: '#dailyTask-table',
                url: '@Url.Action("GetDailyTaskList", "DailyTask", new { area= "TaskCenter" })',
                height: 'full-150',
                page: true,
                parseData: function (res) {
                    return {
                        "code": res.result.code, //解析接口状态
                        "msg": res.result.msg, //解析提示文本
                        "count": res.result.count, //解析数据长度
                        "data": res.result.data //解析数据列表
                    };
                },
                response: {
                    statusCode: 200 //规定成功的状态码，默认：0
                },
		        cols: [
		            [
                        { type: 'checkbox' },
                        { title: '任务名', field: 'name', align: 'left', width: 270 },
                        { title: '备注', field: 'remark', align: 'left', width: 260 },
                        { title: '时间段', field: 'dateRange', align: 'left',  templet: '#dailyTask-dateRange' },
                        { title: '状态', field: 'taskStateTypeName', align: 'center', width: 100 },
                        { title: '操作', toolbar: '#dailyTask-bar', align: 'left' }
		            ]
		        ],
		        skin: 'line',
		        toolbar: '#dailyTask-toolbar',
		        defaultToolbar: [{
		            layEvent: 'refresh',
		            icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports'],
                done: function (res) {
                    for (var i = 0; i < res.data.length; i++) {
                        if (res.data[i].triggers.length == 0) {
                            $("#more" + res.data[i].id).hide();
                            continue;
                        }

                        dropdown.suite("#more" + res.data[i].id, {
                            align:'left',
                            data: res.data[i],
                            templateMenu: "moreTool",
                        });
                    }
                }
		    });

            table.on('tool(dailyTask-table)', function (obj) {
                var data = obj.data;
                if (obj.event === 'remove') {
                    window.remove([{ "id": data.id }]);
		        } else if(obj.event === 'edit'){
		            window.edit(data);
		        } else if (obj.event === "@TaskOperateTrigger.Progress.Name.ToLower()") {
                    window.trigger({
                        "id": data.id,
                        "url": "@Url.Action("ProgressDailyTask", "DailyTask", new { area= "TaskCenter" })"
                    });
                } else if (obj.event === "@TaskOperateTrigger.Resolve.Name.ToLower()") {
                    window.trigger({
                        "id": data.id,
                        "url": "@Url.Action("ResolveDailyTask", "DailyTask", new { area= "TaskCenter" })"
                    });
                } else if (obj.event === "@TaskOperateTrigger.Reopen.Name.ToLower()") {
                    window.trigger({
                        "id": data.id,
                        "url": "@Url.Action("ReopenDailyTask", "DailyTask", new { area= "TaskCenter" })"
                    });
                } else if (obj.event === "@TaskOperateTrigger.Qualify.Name.ToLower()") {
                    window.trigger({
                        "id": data.id,
                        "url": "@Url.Action("QualifyDailyTask", "DailyTask", new { area= "TaskCenter" })"
                    });
                } else if (obj.event === "@TaskOperateTrigger.Pend.Name.ToLower()") {
                    window.trigger({
                        "id": data.id,
                        "url": "@Url.Action("PendDailyTask", "DailyTask", new { area= "TaskCenter" })"
                    });
                } else if (obj.event === "@TaskOperateTrigger.Close.Name.ToLower()") {
                    window.trigger({
                        "id": data.id,
                        "url": "@Url.Action("CloseDailyTask", "DailyTask", new { area= "TaskCenter" })"
                    });
                }
		    });

		    table.on('toolbar(dailyTask-table)', function(obj){
		        if(obj.event === 'add'){
		            window.add();
		        } else if(obj.event === 'refresh'){
		            window.refresh();
                } else if (obj.event === 'batchRemove') {
                    let checkData = table.checkStatus(obj.config.id).data;
				    if (checkData.length === 0) {
					    parent.layer.msg("未选中数据", {
						    icon: 3,
						    time: 1000
					    });
					    return false;
                    }
                    var ids = checkData.map(function (d) { return { "id": d.id }; });
                    window.remove(ids);
		        }
		    });

		    form.on('submit(dailyTask-query)', function(data){
                table.reload('dailyTask-table', {
                    where: data.field,
					page: {
                        curr: 1
                    }
                })
		        return false;
            });

			window.saveCallback = function (data) {
                parent.layer.close(data.index);
                abp.notify.success(data.msg);
                table.reload("dailyTask-table");
            }

			window.add = function () {
				parent.layer.open({
                    type: 2,
                    title: "添加日常任务",
                    content: "@Url.Action("CreateOrUpdateDailyTask", "DailyTask", new { area= "TaskCenter" })",
                    area: ["710px","606px"],
                    shade: 0.1,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe'+ index].submitForm();
                    }
                });
            }

		    window.edit = function(data){
		        parent.layer.open({
                    type: 2,
                    title: "编辑日常任务",
                    content: "@Url.Action("CreateOrUpdateDailyTask", "DailyTask", new { area= "TaskCenter" })" + abp.utils.formatString("?id={0}", data.id),
                    area: ["710px","606px"],
                    shade: 0.1,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe'+ index].submitForm();
                    }
                });
		    }

            window.trigger = function (data) {
                let waitIndex = parent.layer.load(2);
                abp.ajax({
                    url: data.url,
                    data: JSON.stringify({ "id": data.id }),
					abpHandleError: false
                }).done(function (data) {
                    if (data.code == 200) {
                        abp.notify.success(data.msg);
                        table.reload('dailyTask-table');
                    }
                }).fail(function (jqXHR) {
                    parent.layer.msg(jqXHR.message, { icon: 5 });
                }).always(function () {
                    parent.layer.close(waitIndex);
                });
            }

            window.remove = function (data) {
                parent.layer.confirm('确定删除吗?', {
					icon: 3,
					title: '提示'
                }, function (index) {
                    parent.layer.close(index);
                    let waitIndex = parent.layer.load(2);
                    abp.ajax({
                        url: "@Url.Action("DeleteDailyTask", "DailyTask", new { area= "TaskCenter" })",
                        data: JSON.stringify(data),
						abpHandleError: false
                    }).done(function (data) {
                        if (data.code == 200) {
                            abp.notify.success(data.msg);
                            table.reload('dailyTask-table');
                        }
                    }).fail(function (jqXHR) {
                        parent.layer.msg(jqXHR.message, { icon: 5 });
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
		    }

		    window.refresh = function(param){
		        table.reload('dailyTask-table');
		    }
        });
    </script>
}