<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 hysList"
              :key="item.hysyd_id"
              :label="item.hysyd_hymc"
              :value="item.hysyd_id"
            >
            </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-table
      v-loading="loading"
      :data="applyTable"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <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="jy_mc">
      </el-table-column>
      <el-table-column
        label="所属会议"
        show-overflow-tooltip
        prop="jy_hysyd_mc"
      ></el-table-column>
      <el-table-column label="创建人" prop="jy_cjr"></el-table-column>
      <el-table-column
        label="创建时间"
        show-overflow-tooltip
        prop="jy_cjsj"
      ></el-table-column>
      <el-table-column label="类型" width="150" prop="jy_lx"> </el-table-column>
      <el-table-column label="操作" width="180" align="right">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="detailData(scope.row)"
            >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <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="110px"
      >
        <el-form-item label="会议纪要名称" prop="jy_mc">
          <el-input v-model="dialogForm.jy_mc" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="所属会议" prop="jy_hysyd_mc">
          <el-select
            style="width: 100%"
            v-model="dialogForm.jy_hysyd_mc"
            placeholder=""
          >
            <el-option
              v-for="item in hysList"
              :key="item.hysyd_id"
              :label="item.hysyd_hymc"
              :value="{ value: item.hysyd_id, label: item.hysyd_hymc }"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会议内容" prop="jy_hynr">
          <el-input v-model="dialogForm.jy_hynr" type="textarea" />
        </el-form-item>
        <el-form-item label="会议照片" prop="jy_hytp">
          <FileUpload
            :limit="5"
            :full.sync="dialogForm.jy_hytp"
            :key="timestamp"
            v-model="dialogForm.jy_hytp"
            multiple
          >
          </FileUpload>
        </el-form-item>
        <el-form-item label="类型" prop="jy_lx">
          <el-radio-group v-model="dialogForm.jy_lx">
            <el-radio label="公开" />
            <el-radio label="涉密" />
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button :disabled="button" type="primary" @click="formSubmit(dialogForm)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { useUserStore } from "@/stores/user";

const { real_name, user_id, dept_ids } = useUserStore();
import { hyjy_list, hyjy_add, hysyd_list, hyjy_edit, hyjy_del } from "./api";
import FileUpload from "@/components/FileUpload/index.vue";
export default {
  name: "index",
  components: {
    FileUpload,
  },
  data() {
    return {
      state_value: "",
      loading: false,
      keyword: "",
      title: "",
      button:false,
      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,
        },
      ],
      roomList: [
        {
          name: "一号会议室",
          room_id: 1,
        },
        {
          name: "二号会议室",
          room_id: 2,
        },
      ],
      send: true,
      code: false,
      dialogForm: {
        jy_id: "",
        jy_mc: "",
        jy_lx: "",
        jy_hysyd_mc: {},
        time: [],
        jy_hytp: "",
        jy_hynr: "",
      },
      hysList: [],
      isEdit: false,
      timestamp: Date.now(),
      userList: [],
      rules: {
        jy_mc: [
          { required: true, message: "请填写纪要名称", trigger: "submit" },
        ],
        jy_hysyd_mc: [
          {
            required: true,
            message: "请填写所属会议名称",
            trigger: "submit",
          },
        ],
        jy_hynr: [
          { required: true, message: "请填写会议内容", trigger: "submit" },
        ],
        jy_hytp: [
          { required: true, message: "请上传会议图片", trigger: "submit" },
        ],
        jy_lx: [
          { required: true, message: "请选择会议类型", trigger: "submit" },
        ],
      },
      multipleSelection: [],
    };
  },
  methods: {
    getUserList() {
      let data = {
        dept_id: dept_ids[0],
        page: 1,
        limit: 9999,
      };
      //   org_users(data).then(res => {
      //     if (res.code == 1) {
      //       this.userList = res.data.page_data;
      //     }
      //   })
    },
    gethyjyList() {
      this.loading = true;
      let data = {
        site: "jnmf",
        page: 1,
        limit: 999,
        create_user_id: user_id,
      };
      hyjy_list(data).then((res) => {
        if (res.code == "1") {
          this.applyTable = res.data.page_data;
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
          this.loading = false;
        }
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        site: "jnmf",
        page: this.cur_page,
        limit: this.limit,
        jy_mc: this.keyword,
        jy_hysyd_id: this.state_value,
        create_user_id: user_id,
      };
      hyjy_list(data).then((res) => {
        this.applyTable = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
        this.loading = false;
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].jy_id);
      }
    },
    delData() {
      this.$confirm("确认删除这些会议纪要?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            site: "jnmf",
            jy_id: this.multipleSelection,
          };
          hyjy_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "会议纪要删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    getHy() {
      let data = {
        site: "jnmf",
      };
      hysyd_list(data).then((res) => {
        if (res.code == "1") {
          this.hysList = res.data.page_data;
        }
      });
    },
    createNew() {
      this.getHy();
      this.isEdit = false;
      this.button = false;
      this.title = "新建会议纪要";
      this.dialogForm = {
        jy_id: "",
        jy_mc: "",
        jy_lx: "",
        jy_hysyd_mc: {},
        time: [],
        jy_hytp: "",
        jy_hynr: "",
      };
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.getHy();
      this.isEdit = true;
      this.button = false;
      this.title = "编辑会议纪要";
      this.dialogForm = {
        jy_id: item.jy_id,
        jy_mc: item.jy_mc,
        jy_hynr: item.jy_hynr,
        jy_hytp: item.jy_hytp,
        jy_lx: item.jy_lx,
        jy_hysyd_mc: { value: item.jy_hysyd_id, label: item.jy_hysyd_mc },
      };
      this.timestamp = Date.now();
      this.dialogFormVisible = true;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.button = true;
          let data = {
            site: "jnmf",
            issubmit: "1",
            jdbg_hy_jy: {
              jy_mc: this.dialogForm.jy_mc,
              jy_hysyd_mc: this.dialogForm.jy_hysyd_mc.label,
              jy_hysyd_id: this.dialogForm.jy_hysyd_mc.value,
              jy_lx: this.dialogForm.jy_lx == "公开" ? "1" : "0",
              jy_hytp: this.dialogForm.jy_hytp,
              jy_hynr: this.dialogForm.jy_hynr,
            },
          };
          if (!this.isEdit) {      
            hyjy_add(data).then((res) => {
              if (res.code == 1) {
                this.dialogFormVisible = false;
                this.$message({
                  message: "会议纪要新建成功！",
                  type: "success",
                });
                this.getListData();
              }
            });
          } else {
            Object.assign(data, { jy_id: this.dialogForm.jy_id });
            hyjy_edit(data).then((res) => {
              if (res.code == 1) {
                this.dialogFormVisible = false;
                this.$message({
                  message: "会议纪要编辑成功！",
                  type: "success",
                });
                this.getListData();
              }
            });
          }
        }
      });
    },
    detailData(row) {
      this.$router.push({
        name: "hyjy_detail",
        params: {
          jy_id: row.jy_id,
        },
      });
    },
  },
  mounted() {
    this.getUserList();
    this.getHy();
    this.gethyjyList();
    // this.getListData();
  },
};
</script>