define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {
    var Controller = {
        index: function () {
            require(['vue', 'ELEMENT'], function (Vue, ELEMENT) {
                Vue.use(ELEMENT)
                $('#app').show()
                var app = new Vue({
                    el: '#app',
                    data: function () {
                        this.scoreItemsLen = 0
                        return {
                            tableData: [],
                            scores: {}
                        }
                    },
                    computed: {
                        totalScore: function () {                            
                            if ('{}' === JSON.stringify(this.scores)) {
                                return 0
                            }
                            return Object.values(this.scores).reduce((a,b) => {
                                return formatterNum(a) + formatterNum(b)
                            })
                        }
                    },
                    created: function () {
                        var _me = this
                        $.ajax({
                            url: '/school/project_common/score/index?review_score_id='+Config.review_score_id,
                            method: 'POST',
                            success: function (res) {
                                if (1 === res.code) {
                                    res.data.score_items.forEach(function (v, i) {
                                        _me.scoreItemsLen += v.item_details.length
                                        _me.tableData.push(v)
                                    })
                                    _me.scores = res.data.score_detail || {}
                                } else {
                                    Toastr.error(res.msg)
                                }
                            }
                        })
                    },
                    methods: {
                        onSave: function() {
                            var scoreCount = 0,
                                scoresData = this.scores
                            for (var p in scoresData) {
                                if (('string' === typeof scoresData[p]) || ('number' === typeof scoresData[p])) {
                                    scoreCount++
                                }
                            }
                            if (scoreCount !== this.scoreItemsLen) {
                                Toastr.warning('部分评分项未评分,请先评分')
                                return
                            }
                            $.ajax({
                                url: '/school/project_common/score/mark',
                                method: 'POST',
                                data: {
                                    review_score_id: Config.review_score_id,
                                    total_score: this.totalScore,
                                    scores: JSON.stringify(this.scores)
                                },
                                success: function (res) {
                                    if (1 === res.code) {
                                        parent.Toastr.success('打分成功!')
                                        Fast.api.close()
                                    } else {
                                        Toastr.error(res.msg)
                                    }
                                }
                            })
                        }
                    }
                })

                /**
                 * 格式化数字
                 * @param {String|Number} n 数字或数字字符串
                 * @return {Number}
                 */
                function formatterNum(n) {
                    return Number(n) || 0
                }
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    }

    return Controller
})