<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">发布内容</label>
        <el-input v-model="query.content" clearable placeholder="发布内容" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">状态</label>
        <el-radio v-for="item in dict.information_status" :key="item.id" v-model="query.status" :label="item.value" @keyup.enter.native="crud.toQuery">{{ item.label }}</el-radio>
        <!--<date-range-picker
          v-model="query.releaseTime"
          start-placeholder="releaseTimeStart"
          end-placeholder="releaseTimeStart"
          class="date-item"
        />-->
        <rrOperation :crud="crud" />
        <userTemp @on-change="changeUser" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， 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="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="requestUser.name" label="发起人" />
        <el-table-column prop="approvalUser.name" label="审批人" />
        <el-table-column prop="approvalRemark" 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">
            <udOperation
              :data="scope.row"
              :disabled-edit="scope.row.status != '03' && scope.row.status != '02'"
              :disabled-dle="scope.row.status === '00'"
              :permission="permission"
            />
            <el-button
              v-if="scope.row.status === '00' "
              size="mini"
              type="warning"
              icon="el-icon-refresh-left"
              title="撤回"
              @click="ch(scope.row.id)"
            />
            <el-button
              v-if="!scope.row.isHasApproval"
              size="mini"
              type="warning"
              icon="el-icon-user"
              title="选择审批人"
              @click="crud.doOpenDmUser(scope.row,'approvalUser','选择审批人');changeType('approvalUser')"
            />
          </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 rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import userTemp from '@/components/User/dmUserindex'

// import DateRangePicker from '@/components/DateRangePicker'

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: 'DmInformation',
  components: { pagination, crudOperation, rrOperation, udOperation, userTemp },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['information_status'],
  cruds() {
    return CRUD({ title: '信息', url: 'api/dmInformation', idField: 'id', sort: 'id,desc', tempBtnShow: {
      user: false
    }, selectOne: true, crudMethod: { ...crudDmInformation }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'dmInformation:add'],
        edit: ['admin', 'dmInformation:edit'],
        del: ['admin', 'dmInformation:del']
      },
      tmpType: '', // 弹框所属字段
      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
    },
    changeType(type) {
      // 确定当前弹框控制的值
      this.tmpType = type
    },
    changeUser(user, rowid) {
      var data = {}
      data['id'] = rowid
      data[this.tmpType + 'List'] = user
      if (this.tmpType === 'approvalUser') {
        crudDmInformation.addApprovalUser(data).then(res => {
          this.crud.notify('已添加审批人', CRUD.NOTIFICATION_TYPE.SUCCESS)
          this.crud.refresh()
        }).catch(() => { this.crud.notify('失败', CRUD.NOTIFICATION_TYPE.ERROR) })
      }
    },
    ch(id) {

    }
  }
}
</script>

<style scoped>

</style>
