<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="createNew">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">新建会议预订</span>
        </el-button>
        <el-button
          type="danger"
          size="medium"
          :disabled="multipleSelection.length == 0"
          @click="delData"
          >删除</el-button
        >
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="state_value">
          <el-select
            class="mr10"
            clearable
            v-model="state_value"
            placeholder="全部"
            @change="getListData"
          >
            <el-option
              v-for="item in state_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="keyword"
            clearable
            @clear="getListData"
          >
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
          >搜索
        </el-button>
      </el-form>
    </div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="我发起的" name="first">
        <el-table
          v-loading="loading"
          :data="applyTable"
          @selection-change="handleSelectionChange"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column type="selection" align="center" width="55">
          </el-table-column>
          <el-table-column label="序号" align="center" type="index" width="50">
          </el-table-column>
          <el-table-column
            label="会议名称"
            show-overflow-tooltip
            prop="hysyd_hymc"
          >
          </el-table-column>
          <el-table-column
            label="会议室"
            show-overflow-tooltip
            width="160"
            prop="hysyd_hys_mc"
          ></el-table-column>
          <el-table-column
            label="会议时间"
            show-overflow-tooltip
            width="auto"
            prop="hysyd_time"
          ></el-table-column>
          <el-table-column label="参会人数" prop="hysyd_chrs"></el-table-column>
          <el-table-column label="会议状态" prop="scope">
            <template slot-scope="scope">
              <span v-if="scope.row.hysyd_hyzt_option_k == 0">未开始</span>
              <span v-if="scope.row.hysyd_hyzt_option_k == 1">进行中</span>
              <span v-if="scope.row.hysyd_hyzt_option_k == 2">已结束</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180" align="right">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.hysyd_hyzt_option_k == 0"
                type="text"
                @click="editData(scope.row)"
                >编辑
              </el-button>
              <el-button
                type="text"
                v-if="
                  scope.row.hysyd_hyzt_option_k == 1 &&
                  scope.row.hysyd_ewm_option_k == 1
                "
                @click="sign(scope.row)"
                >签到二维码
              </el-button>
              <el-button type="text" @click="detailData(scope.row)"
                >详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="我参与的" name="second">
        <el-table
          v-loading="loading"
          :data="applyTable"
          @selection-change="handleSelectionChange"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column type="selection" align="center" width="55">
          </el-table-column>
          <el-table-column label="序号" align="center" type="index" width="50">
          </el-table-column>
          <el-table-column
            label="会议名称"
            show-overflow-tooltip
            prop="hysyd_hymc"
          >
          </el-table-column>
          <el-table-column
            label="会议室"
            show-overflow-tooltip
            prop="hysyd_hys_mc"
          ></el-table-column>
          <el-table-column
            label="会议时间"
            show-overflow-tooltip
            prop="hysyd_time"
          ></el-table-column>
          <el-table-column label="参会人数" prop="hysyd_chrs"></el-table-column>
          <el-table-column label="会议状态">
            <template slot-scope="scope">
              <span v-if="scope.row.hysyd_hyzt_option_k == 0">未开始</span>
              <span v-if="scope.row.hysyd_hyzt_option_k == 1"
                >进行中</span
              >
              <span v-if="scope.row.hysyd_hyzt_option_k == 2">已结束</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" align="right">
            <template slot-scope="scope">
              <el-button type="text" @click="detailData(scope.row)"
                >详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="581px">
      <el-form :model="dialogForm" :ref="dialogForm" :rules="rules" label-width="100px">
        <div class="mb20 text-center">
          <el-radio-group v-model="dialogForm.way">
            <el-radio label="按日期时间预定" />
            <el-radio label="按会议室预定" />
          </el-radio-group>
        </div>

        <el-form-item label="会议名称" prop="hysyd_hymc">
          <el-input v-model="dialogForm.hysyd_hymc" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="参会人员" prop="hysyd_chry_mc">
          <el-select
            multiple
            filterable
            @change="chcrChange"
            style="width: 100%"
            v-model="dialogForm.hysyd_chry_mc"
            placeholder=""
          >
            <el-option
              v-for="item in userList"
              :key="item.user_id"
              :label="item.user_realname"
              :value="{ value: item.user_id, label: item.user_realname }"
            >
            </el-option>
          </el-select>
          <div>
            已选{{
              dialogForm.hysyd_chry_mc && dialogForm.hysyd_chry_mc.length
            }}人
            <el-checkbox v-model="send" style="margin-left: 19px"
              >参会成员发送会议通知</el-checkbox
            >
          </div>
        </el-form-item>
        <div v-if="dialogForm.way === '按日期时间预定'">
          <el-form-item label="会议日期" prop="hysyd_hysj_time">
            <el-date-picker
              style="width: 100%"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dialogForm.hysyd_hysj_time"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @change="timeChange"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="可选会议室" prop="hysyd_hys">
            <el-select
              style="width: 100%"
              v-model="dialogForm.hysyd_hys"
              placeholder="下拉可选择本时间段所有空闲的会议室"
            >
              <el-option
                v-for="item in hysList"
                :key="item.hys_id"
                :label="item.hys_mc"
                :value="{ value: item.hys_id, label: item.hys_mc }"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="可选会议室" prop="hysyd_hys">
            <el-select
              clearable
              style="width: 100%"
              @change="hysChoose"
              v-model="dialogForm.hysyd_hys"
              placeholder="下拉选择会议室"
            >
              <el-option
                v-for="item in hysList"
                :key="item.hys_id"
                :label="item.hys_mc"
                :value="{ value: item.hys_id, label: item.hys_mc }"
              >
              </el-option>
            </el-select>
            <div class="d-flex text-blue">
              <div>已预订时间:&emsp;</div>
              <ul>
                <li v-for="o in orderedList" :key="o.hysyd_id">
                  <div v-if="o.length != 0">
                    {{ o.hysyd_hys_mc }}-{{ o.hysyd_hysj_ks }}-{{
                      o.hysyd_hysj_js.split(" ")[1]
                    }}
                  </div>
                  <div v-else>暂无预定信息</div>
                </li>
              </ul>
            </div>
          </el-form-item>

          <el-form-item label="会议时间" prop="hysyd_hysj_time">
            <el-date-picker
              style="width: 100%"
              v-model="dialogForm.hysyd_hysj_time"
              type="datetimerange"
              range-separator="-"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="只显示此会议室可预定时间"
              start-placeholder=""
              end-placeholder=""
            >
            </el-date-picker>
          </el-form-item>
        </div>
        <el-form-item label="会议资料" prop="hysyd_hyzl">
          <FileUpload
            :limit="5"
            :full.sync="dialogForm.hysyd_hyzl"
            :key="timestamp"
            v-model="dialogForm.hysyd_hyzl"
            multiple
          >
          </FileUpload>
          <el-checkbox v-model="code">生成签到二维码</el-checkbox>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit(dialogForm)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      class="sign"
      :visible.sync="dialogSign"
      title="签到二维码"
      width="581px"
    >
      <div class="left">
        <div class="ewm">
          <canvas ref="canvas"></canvas>
        </div>
        <div class="des">请扫描二维码进行签到</div>
      </div>
      <div class="right">
        <el-table :data="sign_ry" style="width: 100%">
          <el-table-column
            label="序号"
            align="center"
            type="index"
            width="50"
          ></el-table-column>
          <el-table-column prop="realname" label="参会成员" width="100" />
          <el-table-column label="签到状态" width="180">
            <template slot-scope="scope">
              <span v-if="scope.row.is_sign == 0">未签到</span>
              <span class="signed" v-if="scope.row.is_sign == 1">已签到</span>
            </template>
          </el-table-column>
          <el-table-column label="签到时间">
            <template slot-scope="scope">
              <span class="signed" v-if="scope.row.dateline">{{
                scope.row.dateline
              }}</span>
              <span v-else>--</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { useUserStore } from "@/stores/user";
import QRCode from "qrcode";
const { real_name, dept_ids, user_id, user_org } = useUserStore();
import Tinymce from "@/components/TinymceVue/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import {
  hysyd_list,
  hys_list,
  org_user,
  hysyd_add,
  hysyd_edit,
  hysyd_del,
} from "./api";

export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      loading: false,
      state_value: "",
      state_list: [
        {
          label: "未开始",
          value: "0",
        },
        {
          label: "进行中",
          value: "1",
        },
        {
          label: "已结束",
          value: "2",
        },
      ],
      keyword: "",
      title: "",
      activeName: "first",
      chcy: [],
      applyTable: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      dialogSign: false,
      lbList: [
        {
          name: "测试1",
          id: 0,
        },
        {
          name: "测试2",
          id: 1,
        },
        {
          name: "测试3",
          id: 2,
        },
      ],
      hysList: [],
      send: true,
      code: false,
      dialogForm: {
        hysyd_hymc: "",
        hysyd_hysj_time: [],
        hysyd_chry_mc: [],
        hysyd_hyzl: "",
        way: "按日期时间预定",
        hysyd_hys: {
          label: "",
          value: "",
        },
      },
      rules: {
        hysyd_hymc: [
          { required: true, message: "请填写会议名称", trigger: "submit" },
        ],
        hysyd_hysj_time: [
          { required: true, message: "请选择会议时间", trigger: "submit" },
        ],
        hysyd_chry_mc: [
          { required: true, message: "请选择参会成员", trigger: "submit" },
        ],
        hysyd_hyzl: [
          { required: true, message: "请上传会议资料", trigger: "submit" },
        ],
        hysyd_hys: [
          { required: true, message: "请选择会议室", trigger: "submit" },
        ],
      },
      hysyd_id: "",
      sign_ry: [],
      hysyd_chry_id: "",
      create_user_id: user_id,
      isEdit: false,
      timestamp: Date.now(),
      userList: [],
      qrcode: "",
      chcrNameList: [],
      chcrIdList: [],
      orderedList: [],
      multipleSelection: [],
    };
  },
  methods: {
    handleClick(tab, event) {
      if (event.target.id == "tab-second") {
        this.hysyd_chry_id = user_id;
        this.create_user_id = "";
        this.getListData();
      } else {
        this.hysyd_chry_id = "";
        this.create_user_id = user_id;
        this.getListData();
      }
    },
    getOrgUser() {
      let data = {};
      org_user(data).then((res) => {
        if (res.code == "1") {
          this.userList = res.data.page_data;
        }
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        site: "jnmf",
        page: this.cur_page,
        limit: this.limit,
        hysyd_hymc: this.keyword,
        hysyd_chry_id: this.hysyd_chry_id,
        create_user_id: this.create_user_id,
        hysyd_hyzt: this.state_value,
      };
      hysyd_list(data).then((res) => {
        if (res.code == "1") {
          this.applyTable = res.data.page_data;
          res.data.page_data.forEach((item) => {
            item.hysyd_time = item.hysyd_hysj_ks + "-" + item.hysyd_hysj_js;
          });
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
          this.loading = false;
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].hysyd_id);
      }
    },
    delData() {
      this.$confirm("确认删除这些会议室预定?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            site: "jnmf",
            hysyd_id: this.multipleSelection,
          };
          hysyd_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "会议室预定删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getHys() {
      let data = {
        site: "jnmf",
      };
      hys_list(data).then((res) => {
        if (res.code == "1") {
          this.hysList = res.data.page_data;
        }
      });
    },
    createNew() {
      this.getHys();
      this.getOrgUser();
      this.isEdit = false;
      this.title = "新建会议预定";
      this.dialogForm = {
        hysyd_hymc: "",
        hysyd_hysj_ks: [],
        hysyd_chry_mc: [],
        hysyd_hysj_time:'',
        hysyd_hyzl: "",
        way: "按日期时间预定",
        hysyd_hys: {
          label: "",
          value: "",
        },
      };
      this.timestamp = Date.now();
      this.dialogFormVisible = true;
    },
    editData(item) {
      console.log(item.hysyd_chry_mc);
      this.getOrgUser();
      let chcrNameList = item.hysyd_chry_mc.split(","); //名字集合
      let chcrIdList = item.hysyd_chry_id.split(","); //id集合
      this.getHys();
      this.hysChoose(item.hysyd_hys_id);
      this.hysyd_id = item.hysyd_id;
      this.isEdit = true;
      this.title = "编辑会议预定";
      this.dialogForm = {
        hysyd_hymc: item.hysyd_hymc,
        hysyd_hysj_ks: item.kylx_sqrq,
        hysyd_hyzl: item.hysyd_hyzl,
        hysyd_hysj_time: [item.hysyd_hysj_ks, item.hysyd_hysj_js],
        way: "按日期时间预定",
        hysyd_hys: {
          label: item.hysyd_hys_mc,
          value: item.hysyd_hys_id,
        },
        hysyd_chry_mc: [],
      };
      this.code = item.hysyd_ewm_option_k == 0 ? false : true;
      this.chcrNameList = chcrNameList;
      this.chcrIdList = chcrIdList;
      chcrNameList.forEach((i, index) => {
        this.dialogForm.hysyd_chry_mc.push({
          label: i,
          value: chcrIdList[index],
        });
      });
      this.timestamp = Date.now();
      this.dialogFormVisible = true;
    },
    chcrChange(e) {
      this.chcrNameList = [];
      this.chcrIdList = [];
      e.forEach((item) => {
        this.chcrNameList.push(item.label);
        this.chcrIdList.push(item.value);
      });
    },
    hysChoose(e) {
      let data = {
        site: "jnmf",
        hysyd_hys_id: e.value,
      };
      hysyd_list(data).then((res) => {
        if (res.code == "1") {
          this.orderedList = res.data.page_data;
        }
      });
    },
    timeChange(e) {
      let data = {
        site: "jnmf",
        hysyd_hysj_ks: e[0],
        hysyd_hysj_js: e[1],
      };
      hysyd_list(data).then((res) => {
        res.data.page_data.forEach((item) => {
          this.hysList.forEach((i) => {
            if (i.hys_id === item.hysyd_hys_id) {
              let index = this.hysList.indexOf(i.hys_id);
              this.hysList.splice(index, 1);
            }
          });
        });
        console.log(this.hysList);
      });
    },
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(valid);
          let data = {
            site: "jnmf",
            issubmit: "1",
            send_msg: this.send === true ? "1" : "",
            jdbg_hy_hysyd: {
              hysyd_hymc: this.dialogForm.hysyd_hymc,
              hysyd_chry_mc: this.chcrNameList,
              hysyd_chry_id: this.chcrIdList,
              hysyd_chrs: this.chcrNameList.length,
              hysyd_hyzl: this.dialogForm.hysyd_hyzl,
              hysyd_hys_mc: this.dialogForm.hysyd_hys.label,
              hysyd_hys_id: this.dialogForm.hysyd_hys.value,
              hysyd_hysj_ks: this.dialogForm.hysyd_hysj_time[0],
              hysyd_hysj_js: this.dialogForm.hysyd_hysj_time[1],
              hysyd_ewm: this.code === true ? "1" : "0",
              hysyd_ssks: user_org[user_org.length - 1].uo_name,
            },
          };
          if (!this.isEdit) {
            hysyd_add(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "会议室预定新建成功！",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.getListData();
              }
            });
          } else {
            Object.assign(data, { hysyd_id: this.hysyd_id });
            hysyd_edit(data).then((res) => {
              if (res.code == "1") {
                this.$message({
                  message: "会议室预定编辑成功！",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.getListData();
              }
            });
          }
        }
      });
    },
    detailData(row) {
      this.$router.push({
        name: "hysyd_detail",
        params: {
          yd_id: row.hysyd_id,
        },
      });
    },
    makeQrcode(e) {
      let opts = {
        errorCorrectionLevel: "H",
        type: "image.png",
        width: 300,
        height: 300,
        text: e,
        color: {
          dark: "#333333",
        },
      };
      console.log(e);
      QRCode.toCanvas(this.$refs.canvas, e, opts);
    },
    sign(item) {
      this.dialogSign = true;
      this.qrcode = item.qrcode;
      this.$nextTick(() => {
        this.makeQrcode(item.qrcode);
      });
      // this.$forceUpdate();
      this.sign_ry = item.sign;
    },
  },
  mounted() {
    this.getListData();
  },
};
</script>

<style lang="scss" scoped>
.left {
  width: 100%;
  display: flex;
  position: relative;
  &::after {
    position: absolute;
    content: "";
    width: 100%;
    height: 1px;
    background-color: #ccc;
    opacity: 0.5;
    left: 0;
    top: -30px;
  }
  // .ewm {
  //   width: 310px;
  //   height: 310px;
  //   padding: 10px;
  //   box-sizing: border-box;
  // }
  .des {
    height: 300px;
    line-height: 300px;
    font-size: 14px;
    font-family: Segoe UI, Segoe UI-Regular;
    font-weight: 400;
    color: #265cd4;
  }
}
.right {
  width: 100%;
  .signed {
    color: #265cd4;
  }
}
</style>
