<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="公告标题">
            <el-input v-model="form.announceTitle" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="公告内容" prop="abstract">
            <WangEditor v-model="form.announceContent" />
          </el-form-item>
          <el-form-item label="发布方式">
            <el-radio-group v-model="form.sendType" @change="sendTypeChange">
              <el-radio :label="0">立刻发布</el-radio>
              <el-radio :label="1">定时发布</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-show="sendTypeShow" label="定时时间">
            <el-date-picker v-model="form.sendTime" type="datetime" placeholder="选择日期时间" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="announceTitle" label="公告标题" />
        <el-table-column prop="createBy" label="发布人员" />
        <el-table-column prop="sendTime" label="发布公告时间" />
        <el-table-column prop="sendState" label="发布状态">
          <template slot-scope="scope">
            {{ dict.label.send_state[scope.row.sendState] }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','dmAnnounce:edit','dmAnnounce:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.isCancel">已取消</div>
            <udOperation
              v-if="!scope.row.isCancel"
              :data="scope.row"
              :permission="permission"
            />
            <el-button v-if="!scope.row.isCancel" size="mini" @click="toCancel(scope.row.id)">取消</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmAnnounce from '@/api/dm/announce/dmAnnounce'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

import WangEditor from '@/components/WangEditor/WangEditor'
import { cancel } from '../../../api/dm/announce/dmAnnounce'

const defaultForm = { id: null, announceTitle: null, announceContent: null, sendType: 0, sendTime: null, isCancel: null, sendState: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'DmAnnounce',
  components: { pagination, crudOperation, rrOperation, udOperation, WangEditor },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['send_state'],
  cruds() {
    return CRUD({ title: '系统公告', url: 'api/dmAnnounce', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDmAnnounce }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'dmAnnounce:add'],
        edit: ['admin', 'dmAnnounce:edit'],
        del: ['admin', 'dmAnnounce:del']
      },
      rules: {
      },
      sendTypeShow: false
    }
  },
  watch: {
    'form.sendType'(val) {
      if (val === 0) {
        this.sendTypeShow = false
      } else if (val === 1) {
        this.sendTypeShow = true
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    sendTypeChange(data) {
      if (data === 0) {
        this.sendTypeShow = false
      } else if (data === 1) {
        this.sendTypeShow = true
      }
    },
    toCancel(id) {
      this.$confirm('确认取消当前申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudDmAnnounce.cancel(id).then(() => {
          this.crud.notify('取消成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
          this.crud.refresh()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
