<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="addData">
          <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>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="state_value">
          <el-select class="mr10" v-model="school_value" placeholder="学校名称">
            <el-option
              v-for="item in school_list"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="time">
          <el-date-picker
            style="width: 100%"
            v-model="time"
            type="date"
            placeholder="调研时间"
          >
          </el-date-picker>
        </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
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      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="jjjg_title"
        width="auto"
      ></el-table-column>
      <el-table-column
        label="登记人"
        width="150"
        prop="jjjg_djr"
      ></el-table-column>
      <el-table-column
        label="日期"
        width="150"
        prop="jjjg_djrq"
      ></el-table-column>
      <el-table-column label="调研学校" prop="jjjg_dyxx" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.jjjg_dyxx == 0">全部</span>
          <span v-if="scope.row.jjjg_dyxx == 1">元和小学</span>
          <span v-if="scope.row.jjjg_dyxx == 2">相城中学</span>
          <span v-if="scope.row.jjjg_dyxx == 3">御窑小学</span>
        </template>
      </el-table-column>
      <el-table-column label="调研结果" prop="jjjg_content" width="200">
        <template slot-scope="scope">
          <div v-html="scope.row.jjjg_content"></div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="delData(scope.row.jjjg_id)"
            >删除
          </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="750px"
      destory-on-close
    >
      <el-form :model="dialogForm" label-width="80px">
        <el-form-item label="标题" prop="bt">
          <el-input v-model="dialogForm.bt" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="日期" prop="djrq">
          <el-date-picker
            v-model="dialogForm.djrq"
            placeholder="选择日期"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="发送学校" prop="dyxx">
          <el-select
            v-model="dialogForm.dyxx"
            style="width: 100%"
            placeholder="请选择学校名称"
          >
            <el-option
              v-for="item in school_list"
              v-if="item.id != ''"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="调研结果" prop="content">
          <Tinymce
            v-if="dialogFormVisible"
            v-model="dialogForm.content"
            :height="300"
            :key="timestamp"
          ></Tinymce>
        </el-form-item>
        <el-form-item label="其他附件" prop="qtfj">
          <FileUpload
            :limit="5"
            :full.sync="dialogForm.qtfj"
            :key="timestamp"
            multiple
          >
            <div slot="tip" class="el-upload__tip">
              支持文件类型:doc(.docx)、.ppt(.pptx)、.xls(.xlsx)、.pps、.wps、.pdf、.txt文件格式。
            </div>
          </FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit">发 布</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Tinymce from "@/components/TinymceVue/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import { dyjg_list, dyjg_add, dyjg_edit, dyjg_del,org_list_tree } from "./api";
import { useUserStore } from "@/stores/user";
const { real_name } = useUserStore();
export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      school_value: "",
      school_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "元和小学",
          value: "1",
        },
        {
          label: "相城中学",
          value: "2",
        },
        {
          label: "御窑小学",
          value: "3",
        },
      ],
      time: "",
      keyword: "",
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        bt: "",
        djr: "",
        djrq: "",
        dyxx: "",
        content: "",
        qtfj: "",
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
      timestamp: Date.now(),
    };
  },
  methods: {
    getOrgTree() {
      let data = {}
      org_list_tree(data).then(res => {
        if (res.code == 1) {
          this.school_list = res.data.one_info;
          let obj = {
            id:'',
            label:'全部'
          }
          this.school_list.unshift(obj);
        }
      })
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      dyjg_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },

    addData() {
      this.title = "新建结果";
      this.dialogForm = {
        bt: "",
        djr: "",
        djrq: "",
        dyxx: "",
        content: "",
        qtfj: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑结果";
      this.isEdit = true;
      this.id = item.jjjg_id;
      this.dialogForm.bt = item.jjjg_title;
      this.dialogForm.djr = item.jjjg_djr;
      this.dialogForm.djrq = item.jjjg_djrq;
      this.dialogForm.dyxx = item.jjjg_dyxx;
      this.timestamp = Date.now();
      this.dialogForm.content = item.jjjg_content;
      this.dialogForm.qtfj = item.jjjg_qtfj;

      this.dialogFormVisible = true;
    },
    formSubmit() {
      let data = {
        jjjg_title: this.dialogForm.bt,
        jjjg_djr: real_name,
        jjjg_djrq: this.dialogForm.djrq,
        jjjg_dyxx: this.dialogForm.dyxx,
        jjjg_content: this.dialogForm.content,
        jjjg_qtfj: this.dialogForm.qtfj,
      };
      if (!this.isEdit) {
        dyjg_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "添加成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      } else {
        data.jjjg_id = this.id;
        dyjg_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "编辑成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    delData(id) {
      let data = {
        jjjg_id: id,
      };
      this.$confirm("确认删除该结果?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          dyjg_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "结果删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
  },
  mounted() {
    this.getOrgTree();
    this.getListData();
  },
};
</script>

<style scoped>
</style>





