<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <label class="el-form-item-label">会议议题</label>
        <el-input v-model.trim="query.blurry" clearable size="small" placeholder="会议议题" style="width: 250px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">会议室</label>
        <el-input v-model.trim="query.roomName" clearable placeholder="会议室" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />






        <label class="el-form-item-label">参与部门</label>
        <div style="width: 185px; display: inline-block; vertical-align: middle; margin: 0 3px 10px 0;">
          <treeselect
            v-model="query.deptId"
            :options="depts"
            :load-options="loadDepts"
            style="width: 178px"
            placeholder="选择部门"
            clearable
          />
        </div>

        <label class="el-form-item-label">发起人</label>
        <el-input v-model.trim="query.initUserName" clearable placeholder="发起人" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery"  />
        <label class="el-form-item-label">审批状态</label>
        <el-select v-model="query.approveStatus" filterable placeholder="请选择">
          <el-option
            v-for="item in dict.dm_approve_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">审批时间</label>
        <date-range-picker
          v-model="query.startDate"
          start-placeholder="开始"
          end-placeholder="结束"
          class="date-item"
          style="width: 285px;"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--表单组件-->
          <el-dialog :visible.sync="dialogVisible" title="审批预约申请" width="500px"  :close-on-click-modal="false">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="审批结果" prop="approveStatus">
            <el-input v-model="form.approveStatus" style="display: none;" />
            <el-input :value="form.approveStatus === 1 ? '通过' : '驳回'" readonly="" style="width: 370px;" />
            <!-- <el-select v-model="form.approveStatus" filterable placeholder="请选择" disabled="">
              <el-option
                v-for="item in dict.dm_approve_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select> -->
          </el-form-item>
          <el-form-item label="审批意见" prop="approveSuggest">
            <el-input v-model.trim="form.approveSuggest" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="closeDialog()">取消</el-button>
          <el-button :loading="dialogLoading" type="primary" @click="submitApprove()">确认</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="meetingName" label="会议议题" />
        <el-table-column prop="meetingDescribe" label="会议介绍" />
        <el-table-column prop="meetingType" label="会议类型">
          <template slot-scope="scope">
            {{ dict.label.dm_meeting_type[scope.row.meetingType] }}
          </template>
        </el-table-column>
        <el-table-column prop="roomName" label="会议室" />
        <el-table-column prop="deptName" label="部门" />
        <el-table-column prop="initiatorUserName" label="发起人" />
        <el-table-column prop="approveStatus" label="审批状态">
          <template slot-scope="scope">
            {{ dict.label.dm_approve_status[scope.row.approveStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="approveSuggest" label="审批意见" />
        <el-table-column prop="approveDate" label="审批时间" />
        <el-table-column v-if="checkPer(['admin','dmMeeting:edit','dmMeeting:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.approveStatus === 0 && scope.row.isCancel !== 1" id="approve_success" size="mini" type="primary" @click="toApprove(scope.row,1)"> 通过 </el-button>
            <el-button v-if="scope.row.approveStatus === 0 && scope.row.isCancel !== 1" id="approve_reject" size="mini" type="primary" @click="toApprove(scope.row,2)"> 驳回 </el-button>
            <div v-if="scope.row.approveStatus === 0 && scope.row.isCancel === 1">已取消</div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmMeeting from '@/api/dm/meeting/dmMeeting'
// import crudDmMeeting from '@/api/dm/meeting/dmMeeting'
//获取当前账号绑定的DM用户信息
import { getCurrUser } from '@/api/dm/user/dmUser'
//
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 DateRangePicker from '@/components/DateRangePicker'
import { getDepts, getDeptSuperior } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'

const defaultForm = { meetingId: null, meetingName: null, meetingDescribe: null, meetingType: null, approveStatus: null, approveSuggest: null, taskId: null }

export default {
  name: 'DmMeeting',
  components: { pagination, /* crudOperation, */ rrOperation/* ,  udOperation */, DateRangePicker, Treeselect },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['dm_meeting_type', 'dm_approve_status', 'dm_meeting_status'],
  cruds() {
    return CRUD({ title: '会议', url: 'api/dmMeeting/approveList', idField: 'meetingId', sort: 'create_time,desc', crudMethod: { ...crudDmMeeting }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'dmMeeting:add'],
        edit: ['admin', 'dmMeeting:edit'],
        del: ['admin', 'dmMeeting:del']
      },
      currDmUser: {},
      deptId: null,
      depts: [],
      dialogVisible: false,
      dialogLoading: false,
      rules: {}/* ,
      queryTypeOptions: [
        { key: 'blurry', display_name: '模糊查询' },
        { key: 'room.roomId', display_name: '会议室' },
        { key: 'meetingDescribe', display_name: '会议介绍' },
        { key: 'meetingType', display_name: '会议类型' },
        { key: 'dept.id', display_name: '参与部门' },
        { key: 'initiatorUser.id', display_name: '发起人' },
        { key: 'approveStatus', display_name: '审批状态' },
        { key: 'approveSuggest', display_name: '审批者意见' },
        { key: 'actualCount', display_name: '实际签到人数' },
        { key: 'planCount', display_name: '应到人数/计划人数' },
        { key: 'remark', display_name: '备注' },
        { key: 'meetingStatus', display_name: '会议状态(未开始/进行中/已结束)' }
      ] */
    }
  },
  created() {
    this.getDepts()
    console.log(this.getDepts)
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
      console.log("aaaa")
    },
    toApprove(row, result) {
      // console.log('审批 meetingId ==>> ' + row.meetingId)
      // console.log('审批 taskId ==>> ' + row.taskId)
      // console.log('审批 result ==>> ' + result)
      this.form.meetingId = row.meetingId
      this.form.taskId = row.taskId
      this.form.approveStatus = result
      this.dialogVisible = true
      this.form.approveSuggest = null
      // console.log('表单 form ==>> ' + JSON.stringify(this.form))
    },
    closeDialog() {
      this.dialogVisible = false
      this.crud.refresh()

    },
    submitApprove() {
      // console.log('表单 form ==>> ' + JSON.stringify(this.form))
      var self_ = this
      // if (self_.form.approveSuggest) {
      //   self_.form.approveSuggest = self_.form.approveSuggest.replace(/\s+/g, '')
      // }
      if (!self_.form.approveStatus) {
        this.$message({
          message: '审批结果不能为空',
          type: 'warning'
        })
        return false
      } else if (self_.form.approveStatus === 2 && !self_.form.approveSuggest) {
        this.$message({
          message: '驳回时,请填写审批意见',
          type: 'warning'
        })
        return false
      }


      self_.dialogLoading = true
      crudDmMeeting.approve({
        meetingId: self_.form.meetingId,
        taskId: self_.form.taskId,
        approveStatus: self_.form.approveStatus,
        approveSuggest: self_.form.approveSuggest
      }).then(() => {
        self_.dialogLoading = false
        self_.dialogVisible = false
        this.crud.refresh()
      }).catch(() => {
        self_.dialogLoading = false
        this.crud.refresh()
      })
    },
    findCurrDmUser() {
      getCurrUser().then(res => {
        // console.log('当前账号关联用户信息 ==>> ' + JSON.stringify(res))
        this.currDmUser = res
      })
    },




    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    getSupDepts(deptId) {
      getDeptSuperior(deptId).then(res => {
        const date = res.content
        this.buildDepts(date)
        this.depts = date
      })
    },
    buildDepts(depts) {
      depts.forEach(data => {
        if (data.children) {
          this.buildDepts(data.children)
        }
        if (data.hasChildren && !data.children) {
          data.children = null
        }
      })
    },
    // 获取弹窗内部门数据
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        getDepts({ enabled: true, pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 200)
        })
      }
    },
    // 切换部门
    handleNodeClick(data) {
      if (data.pid === 0) {
        this.query.deptId = null
      } else {
        this.query.deptId = data.id
      }
      this.crud.toQuery()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
  .el-form-item .el-form-item__label{
    width: 90px !important;
  }
  .head-container .date-item {
      width: 270px !important;
  }
</style>
