<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle" />
      <!--审批组件-->
      <el-dialog
        title="审批"
        :visible.sync="approvalVisible"
        width="40%"
      >
        <el-form>
          <el-form-item label="审批意见:">
            <el-input v-model="form.approvalRemark" placeholder="请输入审批意见" :rows="2" type="textarea" style="width: 70%;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" @click="approval('02')">驳回</el-button>
          <el-button type="primary" @click="approval('01')">通过</el-button>
        </div>
      </el-dialog>

      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， 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" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="发布内容">
            <el-input v-model="form.content" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <!--<el-form-item label="发布时间">
            <el-date-picker v-model="form.releaseTime" type="datetime" style="width: 370px;" />
          </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="id" label="主键" />
        <el-table-column prop="content" label="发布内容" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.information_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="approvalUser.name" label="审批人" />
        <el-table-column prop="requestUser.name" label="发起人" />
        <el-table-column prop="createTime" label="发布时间" />
        <el-table-column prop="approvalTime" label="审批时间" />
        <el-table-column v-if="checkPer(['admin','dmInformation:edit','dmInformation:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == '00'"
              size="mini"
              type="warning"
              icon="el-icon-user"
              @click="openSpproval(scope.row.id)"
            >审批</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmInformation from '@/api/dm/information/dmInformation'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, content: null, status: null, approvalUser: null, taskId: null, procInstId: null, requestUser: null, createBy: null, updateBy: null, createTime: null, updateTime: null, releaseTime: null, approvalTime: null }
export default {
  name: 'DmInformationApprovalOne',
  components: { pagination, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['information_status'],
  cruds() {
    return CRUD({ title: '信息', url: 'api/dmInformation/approvalList', sort: 'id,desc', crudMethod: { ...crudDmInformation }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'dmInformation:add'],
        edit: ['admin', 'dmInformation:edit'],
        del: ['admin', 'dmInformation:del']
      },
      tmpType: '', // 弹框所属字段
      approvalId: '', // 所选审批主键
      approvalVisible: false, // 审批框状态
      rules: {
        id: [
          { required: true, message: '主键不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'content', display_name: '发布内容(需发布的信息内容)' },
        { key: 'status', display_name: '(00-待审核  01-审批通过(已发布） 02-审核未通过)' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    openSpproval(rowId) {
      this.approvalId = rowId
      this.approvalVisible = true
    },
    approval(value) {
      crudDmInformation.approval({ 'id': this.approvalId, 'status': value, 'approvalRemark': this.form.approvalRemark }).then(res => {
        this.$message({
          message: '审批成功',
          type: 'success'
        })
        this.approvalId = ''
        this.approvalVisible = false
        this.crud.refresh()
      }).catch((res) => {
      })
    }
  }
}
</script>

<style scoped>

</style>
