<template>
  <div class="report-template">
    <el-drawer class="custom-drawer-form" title="报告模板管理" size="1000px" :visible.sync="drawer" direction="rtl" :before-close="close" destroy-on-close>
      <el-container>
        <el-header>
          <el-form :inline="true" class="margin-left_20">
            <el-form-item label="模板名称">
              <el-input v-model="queryParams.name" size="small" placeholder="请输入模板名称"></el-input>
            </el-form-item>
            <el-form-item label="专题库">
              <el-input v-model="queryParams.projectName" size="small" placeholder="请输入专题库名称"></el-input>
            </el-form-item>
            <el-form-item label="使用权限">
              <el-select v-model="queryParams.permissions" size="small" placeholder="请选择使用权限" clearable @clear="queryParams.permissions = null">
                <el-option v-for="item in permissions" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="" size="small" @click="getList" :disabled="!$permission('/workspace/folder/analyticSystem/templateManager/check')">查询</el-button>
              <el-button type="primary" size="small" @click="handleAdd()" :disabled="!$permission('/workspace/folder/analyticSystem/templateManager/add')">新增</el-button>
            </el-form-item>
          </el-form>
        </el-header>
        <el-main class="container-common-main">
          <el-table v-loading="loading" :data="tableData" border header-row-class-name="custom-table-header">
            <el-table-column type="index" label="序号" width="55" align="center"></el-table-column>
            <el-table-column prop="name" label="模板名称" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="使用权限" align="center">
              <template slot-scope="scope">
                <table-tag :dict-data="permissions" :value="scope.row.permissions" effect="dark" />
              </template>
            </el-table-column>
            <el-table-column label="所属专题库" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <span>{{ scope.row.projectName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <span>{{ scope.row.createName }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" align="center" width="120">
              <template slot-scope="scope">
                <el-dropdown split-button type="primary" size="small" >
                  
                  <p @click="handleEdit(scope.row)" v-if="$permission('/workspace/folder/analyticSystem/templateManager/modify')">编辑</p>
                  <p v-else :disabled="true">编辑</p>
                  <el-dropdown-menu slot="dropdown" style="text-align: center">
                    <el-dropdown-item style="color: red" @click.native="handleDelete(scope.row)" :disabled="!$permission('/workspace/folder/analyticSystem/templateManager/delete')">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </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>
        </el-main>
        <el-footer class="footer-common">
          <el-button @click="close">关 闭</el-button>
        </el-footer>
      </el-container>
    </el-drawer>

    <report-template-form @close="getList" ref="reportTemplateForm" />
  </div>
</template>

<script>
import TableTag from "@/components/TableTag";
import ReportTemplateForm from "../dialog/ReportTemplateForm";
export default {
  components: {
    TableTag,
    ReportTemplateForm
  },
  data() {
    return {
      drawer: false,
      btnLoading: false,
      projectId: 0,
      queryParams: {
        name: '',
        createBy: '',
        projectName: '',
        permissions: null,
        current: 1,
        size: 10,
      },
      title: '',
      permissions: this.$constants.permissions,
      dialogVisible: false,
      loading: true,
      total: 0,
      tableData: [],
    }
  },
  mounted() {

  },
  methods: {
    open(projectId) {
      this.projectId = projectId
      this.drawer = true
      this.getList()
    },
    close() {
      this.drawer = false
      this.$emit('close')
    },
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    getList() {
      this.loading = true
      this.$api.getNewTemplateList(this.queryParams).then(response => {
        this.tableData = response.data.records
        this.total = response.data.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    cancel() {
      this.getList()
      this.dialogVisible = false
    },
    handleAdd() {
      this.$refs.reportTemplateForm.open({
        remark: '',
        name: '',
        permissions: 2,
        file: null,
        projectId: null
      }, '新增报告模板')
    },
    handleEdit(row) {
      this.$refs.reportTemplateForm.open(row, '编辑报告模板')
    },
    handleDelete(row) {
      this.$confirm('确认删除本条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$api.deleteNewTemplate({id: row.id}).then(response => {
          this.$message.success('删除成功')
          this.loading = false
          this.getList()
        }).catch(error => {
          this.loading = false
        })
      })
    }
  }
}
</script>

<style lang="less">

</style>