<template>
  <div class="plantform-feedback-type-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('反馈类型编号')"
        prop="feedback_type_id"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('类型名称')"
        prop="feedback_type_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('是否启用')"
        prop="feedback_type_enable"
        width="100"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.feedback_type_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>

      <!--      <el-table-column-->
      <!--        align="center"-->
      <!--        show-overflow-tooltip-->
      <!--        prop="feedback_type_genus"-->
      <!--        :label="__('所属身份')"-->
      <!--        :formatter="formatGenus"-->
      <!--      />-->
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList, doDelete, doEdit } from '@/api/plantform/feedback/type'
  import Edit from './components/PlantformFeedbackTypeEdit'
  import VabQueryFormRightPanel from '@/vab/components/VabQueryForm/components/VabQueryFormRightPanel'

  export default {
    name: 'PlantformFeedbackType',
    components: { VabQueryFormRightPanel, Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatGenus(row) {
        return row.payment_channel_wechat === 1
          ? this.__('会员所属')
          : this.__('经销商所属')
      },

      handleEdit(row) {
        if (row.feedback_type_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            feedback_type_id: row.feedback_type_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async sChange(row) {
        const param = {
          feedback_type_id: row.feedback_type_id,
          feedback_type_enable: row.feedback_type_enable,
        }
        const { msg, status } = await doEdit(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
