<template>
  <div class="create-report">
    <el-container v-if="showPage" v-loading="loading">
      <el-aside width="300px">
        <el-container>
          <el-main class="nopadding">
            <el-header style="padding: 0 20px !important;">
              <el-button type="primary" size="small" style="width: 100%" @click="handleChangeTemplate">选择报告模板</el-button>
            </el-header>
            <div v-if="showItem">
              <el-menu :default-active="activeItem" style="height: 100%;width: 100%;" @select="handleSelect">
                <el-menu-item v-for="item in template.label" :index="String(item.id)">
                  <span slot="title">{{ item.name }}</span>
                </el-menu-item>
              </el-menu>
            </div>
          </el-main>
        </el-container>
      </el-aside>
      <el-main style="background: #fff;padding: 0">
        <patent-list ref="patentList" v-if="showList" @submit="handleConfirm2" />
      </el-main>
    </el-container>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="900px" :before-close="handleClose" :append-to-body="true"  :close-on-click-modal="false">
      <el-form inline>
        <el-form-item label="模板名称">
          <el-input v-model="queryParams.name" placeholder="请输入模板名称" size="small"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="" size="small" @click="getTemplateList">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="multipleTable" border :data="templateList" header-row-class-name="custom-table-header" v-loading="loading2">
        <el-table-column prop="" label="选择" width="55" align="center">
          <template slot-scope="scope">
            <el-radio v-model="templateId" :label="scope.row.id"></el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="模板名称" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column label="使用权限" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            <table-tag :dict-data="$constants.permissions" :value="scope.row.permissions" size="small" effect="dark" />
          </template>
        </el-table-column>
        <el-table-column label="所属专利数据库" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.projectName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="" label="创建人" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.createName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建日期" show-overflow-tooltip align="center"></el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination :current-page.sync="queryParams.current" :page-size="queryParams.size" :total="total" @current-change="handleCurrentChange" layout="total, prev, pager, next, jumper" background></el-pagination>
      </div>
      <div style="text-align: right">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="dialogVisible2" width="450px" :before-close="handleClose2" :append-to-body="true"  :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
        <el-form-item label="报告名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入报告名称"></el-input>
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button @click="handleClose2">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="loadingBtn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import TableTag from "@/components/TableTag";
import PatentList from "./components/PatentList";

export default {
  components: {
    TableTag,
    PatentList,
  },
  data() {
    return {
      dialogVisible: false,
      dialogVisible2: false,
      showItem: false,
      showList: false,
      loading: false,
      loading2: false,
      loading3: false,
      showPage: true,
      activeItem: '',
      title: '',
      template: {},
      templateId: 0,
      rules: {
        name: [{ required: true, message: '请输入报告名称', trigger: 'blur' },],
      },
      queryParams: {
        name: '',
        current: 1,
        size: 10,
        projectId: 0,
        permissions: 1
      },
      colorList: [
        '#409eff',
        '#67c23a',
        '#909399',
        '#f56c6c',
        '#e6a23c',
        '#e63c91',
        '#e6553c',
        '#3ce6cc',
        '#8e3ce6',
        '#e63c3c'
      ],
      total: 0,
      patentList: [],
      templateList: [],
      loadingBtn: false,
    }
  },
  computed: {
    ...mapGetters(['projectId', 'webSocket', 'userinfo']),
    form() {
      return this.$store.state.report.form
    },
    patentKey() {
      return this.$store.state.chart.patentKey
    }
  },
  async mounted() {
    this.resetForm()
    this.loading = true
    this.queryParams.projectId = this.projectId
    await this.getTemplateList()
    await this.getPatentList()
    this.loading = false
    this.handleChangeTemplate()
    this.initWebSocket()
  },
  methods: {
    initWebSocket() {
      this.webSocket.onmessage = (e) => {
        const data = JSON.parse(e.data)
        if (data.data.success) {
          this.$message.success('任务完成')
          this.$refs.patentList.getReportList()
        }
      }
    },
    close() {

    },
    resetForm() {
      this.$store.commit('SET_FORM')
      this.form.projectId = this.projectId
      this.form.patentKey = this.patentKey
    },
    handleConfirm() {
      if (!this.templateId) {
        this.$message.error('请选择模板')
        return false
      }
      this.template = this.templateList.find(item => item.id === this.templateId)
      this.activeItem = String(this.template.label[0].id)
      this.$store.commit('SET_TEMPLATE', this.template)
      this.$store.commit('SET_ACTIVE_ITEM', this.activeItem)
      this.form.templateId = this.template.id
      this.showItem = false
      this.showList = false
      this.$nextTick(() => {
        this.showItem = true
        this.showList = true
      })
      this.dialogVisible = false
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loadingBtn = true
          if (this.form.id) {
            this.$api.editNewReport(this.form).then(response => {
              this.$message.success('更新成功')
              this.dialogVisible2 = false
              this.loadingBtn = false
            }).catch(error => {
              this.loadingBtn = false
            })
          } else {
            this.$api.addNewReport(this.form).then(response => {
              this.$message.success('保存成功')
              this.dialogVisible2 = false
              this.loadingBtn = false
            }).catch(error => {
              this.loadingBtn = false
            })
          }
        } else {
          this.$message.error('请完善表单内容')
          return false;
        }
      });
    },
    handleConfirm2() {
      this.title = '一键生成报告'
      this.dialogVisible2 = true
    },
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getTemplateList();
    },
    handleClose() {
      this.dialogVisible = false
    },
    handleClose2() {
      this.dialogVisible2 = false
    },
    handleSelect(index, indexPath) {
      this.showList = false
      this.activeItem = index
      this.$store.commit('SET_ACTIVE_ITEM', this.activeItem)
      this.$nextTick(() => {
        this.showList = true
      })
    },
    handleChangeTemplate() {
      this.title = '选择报告模板'
      this.dialogVisible = true
    },
    handleCheck(type) {
      this.$refs.patentList.handleCheck(type)
      this.showList = false
      this.$nextTick(() => {
        this.showList = true
      })
    },
    async getTemplateList() {
      try {
        const response = await this.$api.getNewTemplateList(this.queryParams)
        this.templateList = response.data.records
        this.total = response.data.total
      } catch (e) {
        this.loading = false
      }
    },
    async getPatentList() {
      try {
        const { data } = await this.$api.getReportPatentList({ patentKey: this.patentKey, projectId: this.projectId })
        this.patentList = data
        this.$store.commit('SET_PATENT_LIST', this.patentList)
      } catch (e) {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="less">
.create-report {
  .el-radio__label {
    display: none !important;
  }
  .el-menu-item.is-active {
    outline: 0;
    background-color: #ecf5ff !important;
  }
}
</style>
