<template>
  <div class="L-public-main" id="L-size-main">
    <div class="L-main">
      <header id="L-header">
        <el-form :inline="true" :model="formInline" size="small" class="demo-form-inline" ref="queryFrom">
          <el-col :span="22">
            <el-form-item label="政策文件：" prop="name">
              <el-input v-model.trim="formInline.name" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label="发文单位：" prop="enterUnit">
              <el-input v-model.trim="formInline.enterUnit" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label="政策等级：" prop="policyLevel">
              <el-select class="ignoreElement" v-model="formInline.policyLevel" placeholder="请选择">
                <!-- <el-option
                  v-for="dict in dict.type.jjh_project_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option> -->
                <el-option
                  label="省级"
                  value="0">
                </el-option>
                <el-option
                  label="市级"
                  value="1">
                </el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="发文时间：" prop="projectYear">
              <el-date-picker
                class="ignoreElement"
                v-model="formInline.projectYear"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                type="daterange"
                placeholder="选择发文时间">
              </el-date-picker>
            </el-form-item> -->
            <el-form-item>
              <el-button size="mini" @click="resetQuery('queryFrom')">重置</el-button>
              <el-button size="mini" type="primary" @click="handleQuery('queryFrom')">查询</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <!-- <el-button type="primary" class="import-btn" icon="icon iconfont icon-jc-shangchuan" size="mini" @click="importBtn">新增发文</el-button> -->
            <el-button type="primary" class="import-btn" icon="el-icon-plus" size="mini" @click="importBtn">新增发文</el-button>
          </el-col>
        </el-form>
      </header>
      <section>
        <el-table v-loading="loading" :data="tableData" :row-class-name="tableRowClassName" :height="tabHeader" :max-height="tabHeader">
          <el-table-column label="政策文件名称" width="500px" prop="name" />
          <el-table-column label="关联项目" width="450px" prop="joinProject">
            <template slot-scope="scope">
              <div class="project-name" @click="goToProject(scope.row.joinProject)">
                <dict-tag :options="dict.type.jjh_project_type" :value="scope.row.joinProject"/>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="发文单位" prop="enterUnit" />
          <el-table-column label="政策等级" prop="policyLevel" />
          <el-table-column label="发文时间" prop="enterTime" />
          <el-table-column label="上传用户" prop="createBy" />
          <el-table-column label="上传时间" prop="createTime" width="180px" />
          <el-table-column label="操作" prop="userId" class-name="table-operation" width="180px">
            <template slot-scope="scope">
              <span class="look-info" @click="goInfo(scope.row)">查看详情</span>
              <span class="edit-info" @click="editInfo(scope.row)">编辑</span>
              <span class="del-info" @click="delInfo(scope.row)">删除</span>
            </template>
          </el-table-column>
        </el-table>
        <my-pagination 
          id="L-pagination"
          :total="total"
          :page="pagination.current"
          :limit="pagination.size"
          @pagination="getPagination"
          :current-page.sync="pagination.current" 
        ></my-pagination>
      </section>
    </div>
    <my-dialog :title="title" @close="importClose" @confirm="confirm" ref="importProject" closeText="关闭" :myclass="true">
      <el-form :model="addFrom" size="small" ref="importFrom" label-width="120px" :rules="rules" class="newArticle">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="政策文件名称：" class="importFormItem" prop="name">
              <el-input v-model.trim="addFrom.name" placeholder="请输入内容"></el-input>
              <!-- <el-select class="ignoreElement" v-model="addFrom.projectClassify" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.jjh_project_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="政策等级：" class="importFormItem" prop="policyLevel">
              <el-input v-model.trim="addFrom.policyLevel" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="发文单位：" class="importFormItem" prop="enterUnit">
              <el-input v-model.trim="addFrom.enterUnit" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发文时间：" class="importFormItem" prop="enterTime">
              <el-date-picker
                v-model="addFrom.enterTime"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择发文时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="关联项目：" class="importFormItem" prop="joinProject">
              <el-select v-model="addFrom.joinProject" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.jjh_project_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="发文内容：" class="importFormItem" prop="enterContent">
              <Editor :height="200" v-model="addFrom.enterContent"></Editor>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </my-dialog>
    <el-dialog
      title="查看详情" 
      :visible.sync="infoVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="infoVisible = false"
      custom-class="info-dialog"
      append-to-body
    >
      <div class="info-content">
        <div class="info-title">{{ addFrom.name }}</div>
        <div class="info-content-top">
          <div>发布人：{{ addFrom.createBy }}</div>
          <div>发布时间：{{ addFrom.enterTime }}</div>
        </div>
        <div v-html="addFrom.enterContent" class="info-content-content"></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-form size="small">
          <el-form-item class="dialog-from-item">
            <el-button size="mini" @click="infoVisible = false">关闭</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import myPagination from "@/views/components/Pagination/index.vue"
import myDialog from "@/views/components/myDialog/index.vue"
import { getJPolicyFile, jPolicyFileNew, jPolicyFileChange, jPolicyFileId, jPolicyFileDel } from "@/api/jin_ji_hu/policyDocument"
export default {
  components:{myPagination, myDialog},
  dicts: ['jjh_project_type'],
  data() {
    return {
      total:0,
      pagination: {
        current:1,
        size:10,
      },
      loading:false,
      formInline: {
        name: '',
        enterUnit:"",
        policyLevel:"",
      },
      tableData:[
        {
          projectName:"苏财建[2021]101号 关于下达2021年江苏省级现代服务业发展 专项资金（现代服务业项目）的通知",
          project:"全方位生物大分子药定制研发生产（CDMO）服务平台",
          unit:"苏财政",
          grade:"市级",
          issueTime:"2023-03-19",
          user:"张珊",
          uploadingTime:"2023-05-11 14:00",
        }
      ],
      tabHeader: undefined,
      addFrom: {
        name:"",
        joinProject:"",
        enterUnit:"",
        policyLevel:"",
        enterTime:"",
      },
      rules:{
        name:[
          { required: true, message: '请填写政策文件名称', trigger: 'blur' }
        ],
        joinProject:[
          { required: true, message: '请填写政策等级', trigger: 'blur' }
        ],
        enterUnit:[
          { required: true, message: '请填写发文单位', trigger: 'blur' }
        ],
        policyLevel:[
          { required: true, message: '请选择发文时间', trigger: 'change' }
        ],
        enterTime:[
          { required: true, message: '请选择关联项目', trigger: 'change' }
        ],
      },
      title:"新增发文",
      infoVisible:false,
    }
  },
  mounted() {
    this.cancalDebounce();
    window.addEventListener('resize', this.cancalDebounce);
    
    this.getList();
  },
  destroyed() {
    window.removeEventListener('resize', this.cancalDebounce);
  },
  methods:{
    // 列表获取
    getList(){
      this.loading = true;
      getJPolicyFile(this.pagination).then(res=>{
        this.loading = false;
        this.total = res.data.total;
        this.tableData = res.data.records;
      })
    },
    // 获取页码
    getPagination(pages) {
      this.pagination.current = pages.page;
      this.pagination.size = pages.limit;
      this.getList();
    },
    // 查询
    handleQuery() {
      this.pagination = {
        current: 1,
        size: 10
      }
      this.pagination = { ...this.pagination,...this.formInline };
      // console.log(this.pagination);
      // console.log(this.formInline);
      this.getList();
    },
    // 重置
    resetQuery(formName){
      this.$refs[formName].resetFields();
      this.pagination = {
        current: 1,
        size: 10
      }
      this.getList();
    },
    // 修改table背景色
    tableRowClassName({row, rowIndex}){
      if (rowIndex % 2 !== 0) {
        return 'evenNumber-row';
      }
      return '';
    },
    // 导入按钮
    importBtn(){
      this.title = "新增发文";
      this.$refs.importProject.open();
    },
    // 关闭事件触发
    importClose(){
      this.$nextTick(()=>{
        this.$refs.importFrom.resetFields();
        this.$refs.importProject.close();
        this.addFrom = {
          name:"",
          joinProject:"",
          enterUnit:"",
          policyLevel:"",
          enterTime:"",
        }
      })
    },
    // 查看详情
    goInfo(row){
      jPolicyFileId(row.id).then((res=>{
        if(res.code == 200) {
          this.addFrom = res.data;
          this.infoVisible = true;
        }
      }))
    },
    // 编辑
    editInfo(row){
      jPolicyFileId(row.id).then((res)=>{
        if(res.code == 200) {
          this.addFrom = res.data;
          this.addFrom.joinProject = this.addFrom.joinProject + ''
          this.$refs.importProject.open();
        }
      })
    },
    // 新增发文
    confirm(){
      this.$refs.importFrom.validate((valid) => {
        if (valid) {
          // console.log(this.addFrom);
          let id = this.addFrom.id ? this.addFrom.id : null;
          if(id) {
            jPolicyFileChange(this.addFrom).then((res)=>{
              if(res.code == 200) {
                this.getList();
                this.importClose();
                this.$message({
                  type: "success",
                  message: "修改成功!",
                });
              }
            })
          } else {
            jPolicyFileNew(this.addFrom).then((res)=>{
              if(res.code == 200) {
                this.getList();
                this.importClose();
                this.$message({
                  type: "success",
                  message: "新增成功!",
                });
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    // 删除
    delInfo(row){
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          jProjectDel({ id: row.id }).then((res) => {
            this.getList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 去往项目库搜索
    goToProject(id){
      // this.$router.push({
      //   path: '/project/index',
      //   params: { projectId: id }
      // })
      this.$router.push({
        name: 'ProjectList',
        params: { projectId: id }
      })
    },
    // 屏幕尺寸变化
    cancalDebounce(){
      const element = document.getElementById('L-size-main'); // 通过元素的 ID 获取元素
      const header = document.getElementById('L-header'); // 通过元素的 ID 获取元素
      const pagination = document.getElementById('L-pagination'); // 通过元素的 ID 获取元素
      const elementHeight = element.offsetHeight;
      const headerHeight = header.offsetHeight;
      const paginationtHeight = pagination.offsetHeight;
      this.tabHeader = elementHeight - headerHeight - paginationtHeight - 140;
    }
  },
}
</script>
<style lang="scss" scoped>
::v-deep .info-dialog {
  margin-top: 15vh !important;
}
</style>
