<template>
  <div>
    <div class="con-title">
      <div>办事大厅</div>
    </div>
    <Tap :tapParam="tapParam" @tapParam="tapIndexs"></Tap>
    <div class="con-box">
      <el-button type="primary" v-if="tapIndex == 0" @click="golearningAdd('/app/affairs-hall/learning-add/-1')"
        >申请新的活动</el-button
      >
      <el-button type="primary" v-if="tapIndex == 1" @click="golearningAdd('/app/affairs-hall/share-add/-1')"
        >申请新乐分享</el-button
      >
      <el-button type="primary" v-if="tapIndex == 2" @click="golearningAdd('/app/affairs-hall/again-add/-1')"
        >申请重修</el-button
      >
      <div style="width: 100%; height: 0.2rem"></div>
      <table-list
        :key="affairId"
        v-bind="tableOption"
        v-if="affairId && resultName !== '成绩单'"
        ref="tableList"
      ></table-list>
      <hall-form
        v-if="resultName === '成绩单'"
        :formData="formData"
        @submit="submit"
        @submitType="obtainReport"
      ></hall-form>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确定删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDeletion">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Tap from '../../components/comTable/tap.vue'
import mPage from '../../components/comTable/mPage.vue'
import TableList from '../../components/comTable/tableList.vue'
import HallForm from './components/hall-form.vue'
import cAction from '@action'
const status = {
  '-1': '待审核',
  0: '未通过',
  1: '通过',
  2: '通过',
  3: '未通过',
  4: '通过'
}
export default {
  components: {
    Tap,
    HallForm,
    mPage,
    TableList
  },
  data() {
    const remote = {
      request: cAction.Affairs.getAffairsData,
      params: { offset: 1 },
      beforeRequest: this.tableListbeforeRequest
    }
    return {
      dialogVisible: false,
      loading: false,
      tapIndex: this.$route.query.index || 0,
      // tap切换
      tapParam: [
        {
          name: '分类',
          isShow: false,
          selectIndex: this.$route.query.index || 0,
          key: 'cc',
          arrItem: []
        }
      ],
      // table 配置
      tableOptions: {
        // 学术活动
        symposium: {
          remote,
          columns: [
            { prop: 'form.symposium_name', label: '活动名称' },
            { prop: 'form.symposium_time', label: '活动时间' },
            { prop: 'form.symposium_lecturer', label: '主讲人' },
            {
              prop: 'status',
              label: '审核状态',
              computed(value) {
                return status[value]
              }
            },
            { prop: 'approve_time1', label: '审核时间' },
            { prop: 'remark1', label: '备注' },
            {
              label: '操作',
              attrs: { width: '200' },
              buttons: [
                {
                  label: '查看报告',
                  isShow(row) {
                    return row.form.reportscount !== '0'
                  },
                  onClick: row => {
                    this.$router.push({
                      path: `/app/affairs-hall/view-report/${row.form.id}`
                    })
                  }
                },
                {
                  label: '上传报告',
                  isShow(row) {
                    const isPass = [1, 2, 4].includes(row.status)
                    return isPass && row.form.reportscount === '0'
                  },
                  onClick: row => {
                    this.$router.push({
                      path: `/app/affairs-hall/upload-report/${row.form.id}/-1`
                    })
                  }
                },
                {
                  label: '查看',
                  onClick: row => {
                    this.$router.push({
                      path: `/app/affairs-hall/learning-add/${row.id}`,
                      query: { id: row.affair_id, is_view: true }
                    })
                  }
                },
                {
                  label: '编辑',
                  isShow(row) {
                    return [-1, 0].includes(row.status)
                  },
                  onClick: row => {
                    this.$router.push({
                      path: `/app/affairs-hall/learning-add/${row.id}`,
                      query: { id: row.affair_id }
                    })
                  }
                },
                {
                  label: '删除',
                  isShow(row) {
                    return row.status === -1
                  },
                  onClick: row => {
                    this.confirmDeletion(row)
                  }
                }
              ]
            }
          ]
        },
        // 乐分享
        sharing: {
          remote,
          columns: [
            { prop: 'form.sharing_theme', label: '分享主题' },
            { prop: 'form.class_name', label: '班级' },
            { prop: 'form.personal_name', label: '申请人' },
            { prop: 'form.telephone', label: '电话' },
            {
              prop: 'status',
              label: '审核状态',
              computed(value) {
                return status[value]
              }
            },
            { prop: 'approve_time1', label: '审核时间' },
            { prop: 'remark1', label: '备注' },
            {
              label: '操作',
              attrs: { width: '100' },
              buttons: [
                {
                  label: '查看',
                  onClick: row => {
                    this.$router.push({
                      path: `/app/affairs-hall/share-add/${row.id}`,
                      query: { id: row.affair_id, is_view: true }
                    })
                  }
                }
              ]
            }
          ]
        },
        // 重修
        retake: {
          remote,
          columns: [
            { prop: 'form.semester_name', label: '学期' },
            { prop: 'form.class_name', label: '班级' },
            { prop: 'form.course_name', label: '课程' },
            {
              prop: 'status',
              label: '审核状态',
              computed(value) {
                return status[value]
              }
            },
            {
              prop: 'approve_time1',
              label: '审核时间'
            },
            { prop: 'remark1', label: '备注' },
            {
              label: '操作',
              attrs: { width: '100' },
              buttons: [
                {
                  label: '查看',
                  onClick: row => {
                    this.$router.push({
                      path: `/app/affairs-hall/again-add/${row.id}`,
                      query: { id: row.affair_id, is_view: true }
                    })
                  }
                }
              ]
            }
          ]
        }
      },
      affairList: [],
      formData: {},
      resultName: '',
      affairId: null
    }
  },
  computed: {
    tableOption() {
      const found = this.affairList.find(item => item.id === this.affairId)
      if (found) {
        return this.tableOptions[found.form_name]
      } else {
        return []
      }
    }
  },
  methods: {
    submit(row, type) {
      const params = Object.assign(
        {
          type: type,
          affair_id: this.affairList.find(k => k.form_name === type).id
        },
        row
      )
      cAction.Affairs.submitLearning(params)
        .then(() => {
          this.$message({ type: 'success', message: '已提交成功！' })
          this.obtainReport(type)
        })
        .catch(e => {
          this.$message.error(e.message)
        })
    },
    obtainReport(type = 'elreport') {
      cAction.Affairs.obtainReport(type)
        .then(data => {
          this.formData = data
        })
        .catch(e => {
          this.$message.error(e.message)
        })
    },
    confirmDeletion(row) {
      /* 删除 */
      const loading = this.$loading({
        lock: true,
        text: '',
        spinner: '',
        background: 'rgba(255, 255, 255, 0.9)'
      })
      cAction.Affairs.deleteAffairs(row.id)
        .then(data => {
          if (data.success) {
            this.$message({ type: 'success', message: '删除成功' })
            // 刷新列表，强刷新，返回第一页
            this.$refs.tableList && this.$refs.tableList.refersh(true)
          }
        })
        .catch(e => {
          this.$message.error(e.message)
        })
        .finally(() => {
          loading.close()
        })
      this.dialogVisible = false
    },
    tapIndexs(data) {
      this.resultName = data.name
      this.tapIndex = data.index
      this.affairId = data.id
    },
    // 获取事务类型
    getTapData() {
      cAction.Affairs.getAffairsType()
        .then(data => {
          this.tapParam[0].arrItem = data
            .map(item => {
              if (item.form_name === 'elreport') {
                item.affair_name = '成绩单'
              }
              return { val: item.id, name: item.affair_name, form_name: item.form_name }
            })
            .filter(k => k.form_name !== 'pareport')
          const [first] = data
          const datas = data[this.$route.query.index] || first
          this.affairId = datas.id
          this.affairList = data
        })
        .catch(e => {
          this.$message.error(e.message)
        })
    },
    // 新增
    golearningAdd(url) {
      if (this.affairId) {
        this.$router.push({ path: url, query: { id: this.affairId } })
      }
    },
    // 列表接口请求之前
    tableListbeforeRequest(params) {
      params.affair_id = this.affairId
      return params
    }
  },
  created() {
    this.getTapData()
  }
}
</script>

<style lang="scss" scoped>
.con-title {
  display: flex;
  justify-content: space-between;
}
</style>
