<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="会议名称" prop="conferenceName">
        <el-input
          v-model="queryParams.conferenceName"
          placeholder="请输入会议名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会议时间" prop="startTime">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="会议时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择会议时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @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="['system:conference:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:conference:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:conference:remove']"
          >删除
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:conference:export']"-->
<!--          >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-table
      stripe
      :cell-style="cellStyle"
      v-loading="loading"
      :data="conferenceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column
        label=""
        align="center"
        v-if="columns[0].visible"
        prop="id"
        width="50px"
      /> -->
      <el-table-column
        label="会议名称"
        align="center"
        v-if="columns[1].visible"
        prop="conferenceName"
      />
      <el-table-column
        label="场地"
        align="center"
        v-if="columns[0].visible"
        prop="areaName"
      />
      <el-table-column
        label="会议开始时间"
        align="center"
        v-if="columns[2].visible"
        prop="startTime"
        width="200px"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="会议结束时间"
        align="center"
        v-if="columns[3].visible"
        prop="endTime"
        width="200px"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template> -->
      </el-table-column>
      <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="会议纪要"
        align="center"
        v-if="columns[4].visible"
        prop="conferenceSummary"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <div v-html="scope.row.conferenceSummary"></div>
        </template>
      </el-table-column>

      <el-table-column
        label="会议资料"
        align="center"
        v-if="columns[5].visible"
        prop="conferenceDataName"
        show-overflow-tooltip
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        v-if="columns[6].visible"
        width="300px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:conference:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:conference:remove']"
            >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="participants(scope.row)"
            >参会人列表
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownLoad(scope.row)"
            >下载资料
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会议信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        style="width: 95%"
        label-width="100px"
      >
        <el-row>
          <el-col :span="12">
            <!-- <el-form-item label="场地" prop="areaId">
              <el-select
                style="width: 100%"
                v-model="form.areaId"
                placeholder="请选择场地"
                @change="changeHandler"
              >
                <el-option
                  v-for="item in options.area"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="场地" prop="areaName">
              <el-input
                v-model="form.areaName"
                style="width: 100%"
                placeholder="请选择场地"
                suffix-icon="el-icon-suitcase"
                @click.native="selectArea"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="会议室" prop="detailId">
              <el-select
                v-model="form.detailId"
                style="width: 100%"
                placeholder="请选择会议室"
              >
                <el-option
                  v-for="item in options.areaDetail"
                  :key="item.id"
                  :label="item.areaNo"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="会议名称" prop="conferenceName">
              <el-input
                v-model="form.conferenceName"
                placeholder="请输入会议名称"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.startTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择开始时间"
                :picker-options="pickerOptionsStart"
                default-time="12:00:00"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- 结束时间 -->
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.endTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择结束时间"
                :picker-options="pickerOptionsEnd"
                default-time="12:00:00"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="会议纪要" prop="conferenceSummary">
          <editor v-model="form.conferenceSummary" :min-height="192" />
        </el-form-item>
        <el-form-item label="附件" prop="conferenceDataUrl">
          <el-upload
            class="upload-demo"
            ref="upload"
            action="#"
            :auto-upload="true"
            :before-upload="beforeUpload"
            :on-change="fileChange"
            :http-request="uploadFile"
            :on-remove="handleRemoveFile"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件
            </el-button>
            <!-- <el-button
              v-show="upload.hasFile"
              style="margin-left: 10px"
              size="small"
              type="success"
              @click="submitFile"
              >上传文件
            </el-button> -->
          </el-upload>
          <el-progress
            v-if="upload.isUploading"
            :percentage="upload.progress"
          ></el-progress>
        </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>
    <!-- 选择场地 -->
    <selectArea ref="selectArea"></selectArea>
  </div>
</template>

<script>
import {
  listConference,
  getConference,
  delConference,
  addConference,
  updateConference,
  downloadFile,
} from "@/api/smartSchool/conferenceManage/conference.js";
import selectArea from "../conferenceInformation/components/selectArea.vue";
import { getAreadetailByAreaId } from "@/api/smartSchool/area/areadetail";
import { listArea } from "@/api/smartSchool/area/area";
import { commonUpload } from "@/api/common/index.js";
import { isString } from "@/utils/validate";
import { CustomCellStyle } from "@/enums/customStyle";

// import { downLoadFile } from "@/utils/utilLibrary/downloadFile";

export default {
  name: "Conference",
  // dicts: ["area_name", "Detail"],
  components: {
    selectArea,
  },
  data() {
    return {
      fullscreen: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会议详情信息表格数据
      conferenceList: [],
      fileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //下拉框选项
      // options: {
      //   area: [],
      //   areaDetail: [],
      // },
      // 上传
      upload: {
        isUploading: false,
        progress: 0,
        hasFile: false,
      },
      // 日期范围
      dateRange: [],
      limitDate: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 1 - 24 * 60 * 60 * 1000;
        },
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: null,
        areaId: null,
        areaNo: null,
        detailId: null,
        areaDetils: null,
        conferenceName: null,
        time: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {
        id: null,
        areaId: null,
        detailId: null,
        areaDetils: null,
      },
      //表单数据
      formData: {
        id: null,
        areaId: null,
        detailId: null,
      },
      //参会人员信息
      participantsList: [],
      //列
      columns: [
        // { key: 0, label: `会议室id`, visible: true },
        { key: 0, label: `会议室房间号`, visible: true },
        { key: 1, label: `会议名称`, visible: true },
        { key: 2, label: `会议开始时间`, visible: true },
        { key: 3, label: `会议结束时间`, visible: true },
        { key: 4, label: `会议纪要`, visible: true },
        { key: 5, label: `会议资料`, visible: true },
        { key: 6, label: `操作`, visible: true },
      ],
      // 表单校验
      rules: {
        // areaId: [{ required: true, message: "场地不能为空", trigger: "blur" }],
        // detailId: [
        //   { required: true, message: "场地详细不能为空", trigger: "blur" },
        // ],
        areaName: [
          { required: true, message: "场地不能为空", trigger: "change" },
        ],
        conferenceName: [
          { required: true, message: "会议名称不能为空", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
      },
      pickerOptionsStart: {
        disabledDate: (time) => {
          if (this.form.endTime) {
            return time.getTime() > this.form.endTime;
          }
        },
      },

      cellStyle: CustomCellStyle,
    };
  },

  created() {
    this.getList();
    // this.getAreaList();
  },
  methods: {
    // 选择场地
    selectArea() {
      this.$refs.selectArea.open();
    },

    // 确认选择的场地
    // confirmArea(areaName, areaId) {
    //   this.form.areaName = areaName;
    //   this.form.areaId = areaId;
    //   this.$forceUpdate(); //强制刷新
    // },
    confirmArea(areaName, areaId) {
      // this.form.areaName = areaName;
      this.$set(this.form, "areaName", areaName);
      this.form.areaId = areaId;
      // this.$forceUpdate(); //强制刷新
    },
    //查询所有场地信息
    // getAreaList() {
    //   listArea(this.queryParams).then((response) => {
    //     response.rows.forEach((item) => {
    //       this.options.area.push({ label: item.areaName, value: item.id });
    //     });
    //   });
    // },
    // changeHandler(id) {
    //   // 根据id查询场地详细
    //   getAreadetailByAreaId(id).then((response) => {
    //     // console.log(response.data);
    //     this.options.areaDetail = response.data;
    //   });
    // },

    /** 上传文件前的钩子 */
    beforeUpload() {
      this.upload.isUploading = false;
      this.upload.progress = 0;
    },

    /** 添加的文件时触发该钩子 */
    fileChange(file, fileList) {
      fileList.length > 0 && (this.upload.hasFile = true);
      this.$refs.upload.uploadFiles = [file];
    },
    /** 上传文件到服务器 */
    submitFile() {
      this.$refs.upload.submit();
    },
    /** 删除文件 */
    handleRemoveFile(file, fileList) {
      fileList.length === 0 && (this.upload.hasFile = false);
    },
    /** 文件上传 */
    uploadFile(file, fileList) {
      console.log(file);
      const fileData = file.file;
      const fileName = fileData && fileData.name;
      const formData = new FormData();
      formData.append("file", fileData);
      this.upload.isUploading = true;
      commonUpload(formData, (progress) => {
        const { loaded, total } = progress;
        this.upload.progress = Math.round((loaded / total) * 100) | 0;
      }).then((response) => {
        this.$message.success("上传成功");
        this.form.conferenceDataUrl = response.url;
        this.form.conferenceDataName = fileName;
        this.upload.isUploading = false;
      });
    },

    /** 下载 */
    handleDownLoad({ conferenceDataUrl, name }) {
      if (
        isString(conferenceDataUrl) &&
        conferenceDataUrl.lastIndexOf("/") > -1
      ) {
        this.$download.resource(conferenceDataUrl);
      } else {
        return "";
      }
    },

    /** 查询会议信息列表 */
    getList() {
      this.loading = true;
      listConference(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.conferenceList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // /** 更多下拉菜单 */
    // handleCommand(command, row) {
    //   this[command].apply(undefined, [row]);
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.$refs.upload.clearFiles();
      this.upload.hasFile = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaName: null,
        areaNo: null,
        detailId: null,
        startTime: null,
        endTime: null,
        areaId: null,
        areaDetils: null,
        conferenceName: null,
        time: null,
        conferenceSummary: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        conferenceDataUrl: "",
        conferenceDataName: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会议信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getConference(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会议信息";
      });
    },

    // // 选择参会人
    // selectparticipants(row) {
    //   this.$router.push({
    //     path: "/conferenceManage/selectparticipants",
    //     query: {
    //       id: row.id,
    //     },
    //   });
    // },
    // 参会人列表
    participants(row) {
      this.$router.push({
        path: "/conferenceManage/participants",
        query: {
          id: row.id,
        },
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateConference(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.cancel();
            });
          } else {
            addConference(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.cancel();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          `是否确认删除选中的${
            Object.hasOwnProperty.call(ids, "length") ? ids.length : 1
          }条数据？`
        )
        .then(function () {
          return delConference(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm(
        `是否确认导出${
          !!this.ids.length ? this.ids.length + "条" : "所有"
        }数据？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          if (this.ids.length) {
            this.download(
              "schoolConference/export/" + this.ids,
              {
                ...this.queryParams,
              },
              `会议信息${Date.now()}.xlsx`
            );
          } else {
            this.download(
              "schoolConference/export/",
              {
                ...this.queryParams,
              },
              `会议信息${Date.now()}.xlsx`
            );
          }
          this.$message({
            type: "success",
            message: "导出成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
  },
};
</script>

