<template>
  <div class="app-container">
    <div ref="sousuH">
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="会议名称" prop="meetingName">
          <el-input v-model="queryParams.meetingName" placeholder="请输入会议名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="会议时间" prop="meetingStartTimes">
          <!-- <el-date-picker clearable v-model="queryParams.meetingStartTime" type="datetime" key="one" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间" style="width: 100%;" /> -->
          <el-date-picker v-model="queryParams.meetingStartTimes" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']" />
        </el-form-item>
        <!-- <el-form-item label="结束时间" prop="meetingEndTime"> -->
        <!-- <el-date-picker clearable v-model="queryParams.meetingEndTime" type="datetime" key="two" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间" style="width: 100%;" /> -->
        <!-- <el-date-picker v-model="queryParams.meetingEndTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']" /> -->
        <!-- </el-form-item> -->
        <!-- <el-form-item label="会议主题" prop="meetingSubject">
              <el-input v-model="queryParams.meetingSubject" placeholder="请输入会议主题" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item> -->
        <el-form-item label="会议类型" prop="meetingType">
          <el-select v-model="queryParams.meetingType" placeholder="请选择会议类型" clearable size="small">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_CONFIG_TYPE)" :key="dict.value"
              :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 操作工具栏 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['backend:meeting:create']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            :loading="exportLoading" v-hasPermi="['backend:meeting:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </div>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" :height="list.length ? tableHeight : 'auto'">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="会议名称" align="center" prop="meetingName" />
      <el-table-column label="开始时间" align="center" prop="meetingStartTime" width="180">
        <template v-slot="scope">
          <span>{{ scope.row.meetingStartTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="meetingEndTime" width="180">
        <template v-slot="scope">
          <span>{{ scope.row.meetingEndTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会议主题" align="center" prop="meetingSubject" />
      <el-table-column label="会议类型" align="center" prop="meetingType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.INFRA_CONFIG_TYPE" :value="scope.row.meetingType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['backend:meeting:update']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['backend:meeting:delete']">删除</el-button> -->
          <el-dropdown @command="(command) => handleCommand(command, scope.$index, scope.row)">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>

            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleDelete" size="mini" type="text" icon="el-icon-delete"
                v-hasPermi="['backend:meeting:delete']">删除</el-dropdown-item>
              <el-dropdown-item command="handleSelectedOrg" size="mini" type="text"
                icon="el-icon-circle-check">已分配单位列表</el-dropdown-item>
              <el-dropdown-item command="handleOrg" size="mini" type="text"
                icon="el-icon-circle-check">分配单位</el-dropdown-item>
              <el-dropdown-item command="handPublish" size="mini" type="text"
                icon="el-icon-circle-check">发布</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-button size="mini" type="text" icon="el-icon-d-arrow-right" @click="lookMes(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="会议名称" prop="meetingName">
          <el-input v-model="form.meetingName" placeholder="请输入会议名称" />
        </el-form-item>
        <el-form-item label="会议编号" prop="meetingCode">
          <el-input v-model="form.meetingCode" placeholder="请输入会议编号" />
        </el-form-item>
        <el-form-item label="会议时间" prop="meetingStartTimes">
          <el-date-picker v-model="form.meetingStartTimes" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']" />
        </el-form-item>
        <!--        <el-form-item label="结束时间" prop="meetingEndTime">-->
        <!--          <el-date-picker clearable v-model="form.meetingEndTime" type="datetime" value-format="timestamp"-->
        <!--            placeholder="选择结束时间" style="width: 100%;" />-->
        <!--        </el-form-item>-->
        <el-form-item label="会议主题" prop="meetingSubject">
          <el-input v-model="form.meetingSubject" placeholder="请输入会议主题" />
        </el-form-item>
        <el-form-item label="会议类型" prop="meetingType">
          <el-select v-model="form.meetingType" placeholder="请选择会议类型" style="width: 100%;">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_CONFIG_TYPE)" :key="dict.value"
              :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status" style="width: 100%;">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)" :key="dict.value"
              :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="单位类别">
              <el-select v-model="form.orgType" multiple placeholder="请选择单位类别">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMPANY_inspectitem_kind)" :key="dict.value"
                  :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item> -->
        <!-- <el-form-item label="单位">
              <el-input clear @focus="handleFocus" @clear="handleClear" placeholder="请选择单位" v-model="orgList" clearable>
                <template slot="append">
                  <i title="选择" @click="handleChoice" style="cursor:pointer;" class="el-icon-plus"></i>
                </template>
              </el-input>
            </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 分配分管单位 -->
    <el-dialog title="分配分管单位" :visible.sync="openOrg" width="85vw" append-to-body v-if="openOrg">
      <choiceSelect ref="choiceSelect" :userId="form.id" :type="'meeting'"></choiceSelect>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOrg">确 定</el-button>
        <el-button @click="cancelOrg">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 已选择分管单位 -->
    <el-dialog title="已选择分管单位" :visible.sync="openSelectedOrg" width="85vw" append-to-body v-if="openSelectedOrg">
      <handleSelectedOrg ref="choiceSelect" :userId="form.id" :type="'meeting'"></handleSelectedOrg>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="openSelectedOrg = false">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 查看会议人员 -->
    <el-dialog title="提示" :visible.sync="meetPeopleModal" width="30%">
      <el-table :data="meetPeople" style="width: 100%">
        <el-table-column prop="ppName" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="ppTel" label="电话号">
        </el-table-column>
        <el-table-column prop="ppTel" label="电话号">
          <template scope="scope">
            <span>{{ dutyComputed ||scope.row.ppDuty }}</span>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="meetPeopleModal = false">取 消</el-button>
        <el-button type="primary" @click="meetPeopleModal = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { createMeeting, updateMeeting, deleteMeeting, getMeeting, getMeetingPage, exportMeetingExcel, getmeetingorg, broadMeeting, setupMeetingOrg, getMeetPeople } from "@/api/backend/meeting"
import choiceSelect from '@/components/choiceSelect';
import handleSelectedOrg from '@/components/handleSelectedOrg';
import { getTableHeight } from "@/utils/ruoyi.js";

export default {
  name: "Meeting",
  components: {
    choiceSelect, handleSelectedOrg
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会议信息列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        meetingName: null,
        // meetingStartTime: [],
        // meetingEndTime: [],
        meetingStartTime: '',
        meetingEndTime: '',
        meetingStartTimes: [],
        meetingSubject: null,
        meetingType: null,
        status: null,
      },
      // 表单参数
      form: {
        meetingStartTimes: [],
        meetingStartTime: '',
        meetingEndTime: '',
        status: 0
      },
      // 表单校验
      rules: {
        meetingName: [{ required: true, message: "会议名称不能为空", trigger: "blur" }],
        meetingStartTimes: [{ required: true, message: "开始时间不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
      // =======单位信息==========================
      // 是否显示弹出层（分管单位）
      openOrg: false,
      // 最终选择的单位信息列表
      alreadySelectedOrgList: [],
      orgList: '',
      tableHeight: "",
      // 已选择单位
      openSelectedOrg: false,
      meetPeople: [],//会议所属人员
      meetPeopleModal: false
    }
  },
  mounted() {
    //挂载window.onresize事件(动态设置table高度)
    window.addEventListener('resize', () => {
      this.$nextTick(() => {
        if (this.$refs['sousuH']) {
          this.tableHeight = getTableHeight(this.$refs['sousuH'].clientHeight + 200)
        }
      })
    })
  },
  created() {
    this.getList()
    this.$nextTick(() => {
      this.tableHeight = getTableHeight(this.$refs['sousuH'].clientHeight + 200)
    })
  },
  methods: {
    // 更多操作
    handleCommand(command, index, row) {
      const id = row.id
      getMeeting(id).then(response => {
        this.form = response.data
      })
      switch (command) {
        case 'handleUpdate':
          this.handleUpdate(row);//修改客户信息
          break;
        case 'handleDelete':
          this.handleDelete(row);//红号变更
          break;
        case 'handleOrg':
          this.handleOrg(row);
          break;
        case 'handleSelectedOrg':
          this.handleSelectedOrg(row);
          break;
        case 'handPublish':
          this.handPublish(row);
          break;
        default:
          break;
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getMeetingPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        meetingName: undefined,
        meetingCode: undefined,
        meetingStartTime: undefined,
        meetingEndTime: undefined,
        meetingSubject: undefined,
        meetingType: undefined,
        status: 0,
        // orgType: undefined,
        orgIds: [],
      }
      this.resetForm("form")
      this.orgList = ''
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.meetingStartTime = this.queryParams.meetingStartTimes[0]
      this.queryParams.meetingEndTime = this.queryParams.meetingStartTimes[1]
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加会议信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id

      getMeeting({ id: id, pageNo: 1, pageSize: 10 }).then(response => {
        this.form = response.data
        const arr = []
        arr.push(this.form.meetingStartTime, this.form.meetingEndTime)
        //this.form.meetingStartTimes=[...arr]
        this.$set(this.form, 'meetingStartTimes', arr)
        console.log(this.form.meetingStartTimes, 'this.form.meetingStartTimes arr arr');
        this.open = true
        this.title = "修改会议信息"
      })
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return
        }
        this.form.meetingStartTime = this.form.meetingStartTimes[0]
        this.form.meetingEndTime = this.form.meetingStartTimes[1]
        // 修改的提交
        if (this.form.id !== undefined) {
          updateMeeting(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createMeeting(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功")
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal
        .confirm('是否确认删除会议名称为"' + row.meetingName + '"的数据项?')
        .then(function () {
          return deleteMeeting(id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      // 执行导出
      this.$modal
        .confirm("是否确认导出?")
        .then(() => {
          this.exportLoading = true
          return exportMeetingExcel(params)
        })
        .then((response) => {
          this.$download.excel(response, "会议信息.xls")
          this.exportLoading = false
        })
        .catch(() => { })
    },
    /**
        * 选单位
        */
    // ========获取已选单位信息=============================================================
    // -========================================================
    // =========表单添加时选单位=================================================
    handleClear() {
      this.form.orgIds = [];
    },
    handleFocus() {
      this.handleChoice();
    },
    handleChoice() {
      // 打开弹窗
      this.openOrg = true;
    },
    // ============操作区修改单位=====================================================
    // 单位信息表单重置
    Orgreset() {
      this.alreadySelectedOrgList = []
    },
    // 打开单位信息v弹窗
    handleOrg(row) {
      this.Orgreset()
      if (row.id) {
        this.form.id = row.id
      }
      // 打开弹窗
      this.openOrg = true;
    },

    // /============单位弹窗功能====================================================
    /** 单位信息确认按钮 */
    submitOrg() {
      this.alreadySelectedOrgList = this.$refs["choiceSelect"].confirmMSg()
      // this.orgType = this.$refs["choiceSelect"].confirmoOrgTypeMSg()
      console.log('this.$refs["choiceSelect"] :>> ', this.alreadySelectedOrgList);
      // 关闭弹窗
      this.openOrg = false;
      this.submitFormOrg(this.$refs["choiceSelect"].confirmMSg(), this.$refs["choiceSelect"].confirmoOrgTypeMSg())
    },
    // =======取消按钮=与=信息保存=========================================================
    // 取消按钮（分管单位）
    // 取消按钮（分管单位）
    cancelOrg() {
      this.openOrg = false;
    },
    // ========用于form表单已存在alreadySelectedOrgList单位信息时保存============================

    submitFormOrg(alreadySelectedOrgList, orgType) {
      var orgIds = []
      alreadySelectedOrgList.map(i => {
        orgIds.push(i.dwid)
      })
      this.form.orgType = orgType
      this.form.orgIds = orgIds

      // this.form.taskId = this.form.id
      console.log('this.form :>> ', this.form);
      // =====================
      setupMeetingOrg(this.form).then(response => {
        // this.$modal.msgSuccess("分配角色成功");
      });
    },
    // ===============已分配单位列表========================
    handleSelectedOrg(row) {
      if (row.id) {
        this.form.id = row.id
      }
      // 打开弹窗
      this.openSelectedOrg = true;
    },

    // -==========发布======================
    handPublish(row) {
      if (row.id) {
        broadMeeting({ mid: row.id }).then(response => {
          // this.$modal.msgSuccess("分配角色成功");

          this.getList()
        });
      }
    },
    lookMes(row) {
      // console.log(row,'5');s
      getMeetPeople({
        meetingId: row.id,
        pageNo: 1,
        pageSize: 10
      }).then(res => {
        console.log(res, 'resddd');
        this.meetPeople = res.data.list
        this.meetPeopleModal = true
      })
    }
  },
  computed: {
    dutyComputed(id) {
      if (id === '1') {
        return '值班人员'
      } else if (id === '2') {
        return '负责人'
      } else {
        return '普通人员'
      }
    }
  }
}
</script>
