<template>
  <div class="evaluation-container">
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane label="评价数据" name="1">
        <el-table stripe ref="table" :height="tableHeight" v-loading="tableLoading"
                  :data="evaluationList"
                  border row-key="id">
          <el-table-column show-overflow-tooltip prop="id" label="序号" align="center" width="50"/>
          <el-table-column min-width="200" show-overflow-tooltip prop="content" label="评价数据" align="center"/>
          <el-table-column show-overflow-tooltip prop="note" label="备注" align="center"/>
          <el-table-column width="80" show-overflow-tooltip prop="format_create_user" label="添加人" align="center"/>
          <el-table-column width="160" show-overflow-tooltip prop="format_create_time" label="添加时间" align="center"/>
          <el-table-column width="80" show-overflow-tooltip prop="sort" label="显示顺序" align="center"/>
          <el-table-column width="100" show-overflow-tooltip prop="endPoint" label="状态" align="center">
            <template slot-scope="{row}">
              <span v-show="row.status ==1" style="color: #666">显示</span>
              <span v-show="row.status ==2" style="color: #666">隐藏</span>
              <el-switch v-if="isAdmin||btnRule.some(btn=>btn=='status')" v-model="row.status" :active-value="1"
                         :inactive-value="2" size="small"
                         @change="changeStatus(row)"/>
            </template>
          </el-table-column>
          <el-table-column v-if="isAdmin||btnRule.some(btn=>btn=='edit')" width="60" label="操作">
            <template slot-scope="scoped">
              <el-tooltip v-if="isAdmin||btnRule.some(btn=>btn=='edit')" class="item" effect="dark" content="编辑"
                          placement="bottom">
                <el-button @click="handleEdit(scoped.row)" type="primary" size="mini" icon="el-icon-edit"/>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="page-current">
          <el-pagination
            background
            :current-page="currentPage"
            :page-size="pageSize"
            prev-text="上一页"
            next-text="下一页"
            layout="total, prev, pager, next, jumper"
            style="display: inline-block;"
            @current-change="handleCurrentChange"
            :total="count"/>
        </div>
      </el-tab-pane>
      <el-tab-pane v-if="isAdmin||btnRule.some(btn=>btn=='add')" label="添加评价" name="2">
        <evaluationForm :ruleForm="ruleForm"
                    style="width: 40%" v-if="activeName==2" @submit="submit"></evaluationForm>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" center :visible.sync="addDialogVisible" title="编辑评价">
      <evaluationForm ref="dialogForm" :ruleForm="ruleForm"
                  style="width: 100%" @submit="submit" @onBack="onBack"></evaluationForm>
    </el-dialog>
  </div>
</template>

<script>
  import evaluationApi from '@/api/basicData/evaluation'
  import evaluationForm from './evaluationForm'

  export default {
    name: "evaluation",
    data() {
      return {
        activeName: '1',
        btnRule: [],
        isAdmin: false,
        addDialogVisible: false,
        ruleForm: {
          content:'',
          note:'',
          sort:'1'
        },
        currentPage: 1,
        pageSize: 10,
        count: 0,
        evaluationList: [],
        tableLoading: false,
        tableHeight: document.body.clientHeight - 245,
      }
    },
    components: {evaluationForm},
    created() {
      const {btnRule, isAdmin} = this.$store.state.user
      this.btnRule = btnRule
      this.isAdmin = isAdmin
      this.pageSize = parseInt((document.body.clientHeight - 284) / 50)
    },
    mounted() {
      this.tableLoading = true
      this.getAllList()
    },
    watch: {
      activeName() {
        this.initRuleForm()
      }
    },
    methods: {
      changeStatus(data) {
        var content = ''
        if (data.status === 1) {
          content = '确定要启用吗？'
        } else {
          content = '确定要禁用吗？'
        }
        this.$confirm(content, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            status: data.status == 1 ? '1' : '2',
            id: data.id
          }
          this.tableLoading = true
          evaluationApi.setStatus(params).then(res => {
            this.$message({
              type: 'success',
              message: '操作成功！'
            })
            this.getAllList()
          }).catch(e => {
            let evaluationList = JSON.parse(JSON.stringify(this.evaluationList))
            for (let i = 0; i < evaluationList.length; i++) {
              if (evaluationList[i].id == data.id) {
                evaluationList[i].status = data.status == 1 ? 2 : 1
              }
            }
            this.evaluationList = Object.assign([], evaluationList)
            this.tableLoading = false
          })
        }).catch(() => {
          let evaluationList = JSON.parse(JSON.stringify(this.evaluationList))
          for (let i = 0; i < evaluationList.length; i++) {
            if (evaluationList[i].id == data.id) {
              evaluationList[i].status = data.status == 1 ? 2 : 1
            }
          }
          this.evaluationList = Object.assign([], evaluationList)
          this.$message({
            type: 'info',
            message: '已取消该操作！'
          })
        })
      },
      handleEdit(data, type) {
        this.initRuleForm()
        this.addDialogVisible = true
        this.$nextTick(function () {
          try {
            let child = JSON.parse(JSON.stringify(data))
            child.baseScore=child.base_score
            child.increaseNum=child.increase_num
            this.ruleForm = Object.assign({}, child)
            this.$refs.dialogForm.$refs['ruleForm'].resetFields()
          } catch (e) {
          }
        })
      },
      initRuleForm() {
        this.ruleForm = {
          content:'',
          note:'',
          sort:'1'
        }
      },
      getAllList() {
        let searchParam={
          page : this.currentPage,
          pageSize : this.pageSize
        }
        evaluationApi.getIndex(searchParam).then(res => {
          this.evaluationList = res.data.records
          this.count = res.data.total
          this.tableLoading = false
        }).catch(err => {
          this.evaluationList = []
          this.tableLoading = false
        })
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      submit(type) {
        if (type == 1) {
          this.activeName='1'
          this.currentPage = 1
          this.tableLoading = true
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.getAllList()
        } else {
          this.tableLoading = true
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.getAllList()
          this.addDialogVisible = false
        }
      },
      onBack() {
        this.initRuleForm()
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang="less">
  .evaluation-container {
    width: 100%;

    .el-input.is-disabled .el-input__inner {
      background-color: #fff !important;
      color: #333 !important;
    }
  }
</style>
