<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-custom__evaluateoption}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <!-- <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item> -->
        <el-form-item label="评价选项" prop="optionContent">
          <el-input v-model="dataForm.optionContent" placeholder="评价选项" clearable></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="roleCode">
          <el-select v-model="dataForm.roleCode" placeholder="类别" clearable=true>
            <el-option v-for="item in roleFlagArr" :key="item.dictValue" :label="item.dictName" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('custom:evaluateoption:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
        <el-table-column label="序号" header-align="center" align="center" width="50px">
          <template slot-scope="scope">
            {{scope.$index+1}}
          </template>
        </el-table-column>
        <el-table-column prop="optionContent" label="评价选项" header-align="center" align="center"></el-table-column>
        <el-table-column prop="roleCode" label="类别" header-align="center" align="center" :formatter="showFlagFormat"></el-table-column>
        <el-table-column prop="sort" label="排序" header-align="center" align="center"></el-table-column>
        <el-table-column prop="available" label="操作" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.available" active-color="#13ce66" inactive-color="#ff4949" active-value="1" inactive-value="0" @change="availableHandle(scope.row.id,scope.row.available)"></el-switch>
            &nbsp;
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
          </template>
        </el-table-column>
        <el-table-column v-if=false :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('custom:evaluateoption:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('custom:evaluateoption:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './evaluateoption-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/custom/evaluateoption/page',
        getDataListIsPage: true,
        deleteURL: '/custom/evaluateoption',
        deleteIsBatch: true
      },
      dataForm: {
        id: ''
      },
      roleFlagArr: []
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    this.getRoleListInfo()
  },
  methods: {
    // 获取评价类别下拉信息
    getRoleListInfo () {
      this.$http.get(`/custom/evaluateoption/listSimple`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.roleFlagArr = res.data
      }).catch(() => {})
    },
    showFlagFormat (row, column) {
      if (row.roleCode === 1) {
        return '点赞'
      } else {
        return '吐槽'
      }
    },
    availableHandle (id, availableValue) {
      let postData = {
        id: id,
        available: availableValue
      }
      this.$http['post'](
        '/custom/evaluateoption/updateOptionInfo',
        postData
      )
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>
