<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input
        v-model.trim="listQuery.factoryCode"
        :placeholder="$t('pages.payFactory.placeholder.factoryCode')"
        style="width: 200px;"
        class="filter-item"
      />
      <el-input
        v-model.trim="listQuery.factoryName"
        :placeholder="$t('pages.payFactory.placeholder.factoryName')"
        style="width: 200px;"
        class="filter-item"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t('pages.payFactory.action.search') }}
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="createChannel"
      >
        {{ $t('pages.payFactory.action.add') }}
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.payFactory.word.loading')"
      fit
      highlight-current-row
    >
      <el-table-column label="Code" prop="factoryCode" align="center" />
      <el-table-column :label="$t('pages.payFactory.table.name')" prop="factoryName" align="center" />
      <el-table-column label="Icon" prop="factoryIcon" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 45px; height: 45px"
            :src="scope.row.factoryIcon"
            :preview-src-list="[scope.row.factoryIcon]"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.payFactory.table.createTime')" prop="createTime" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.payFactory.table.actions')" align="center" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="clickAssociatedChannels(scope.row)">{{ $t('pages.payFactory.action.associateChannels') }}</el-button>
          <el-button type="text" @click="clickEdit(scope.row)">{{ $t('pages.payFactory.action.edit') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />

    <edit-form
      v-if="formEditVisible"
      :row="selectedRow"
      @close="formEditVisible=false;selectedRow={}"
      @success="formEditSuccess"
    />

    <associated-channel
      v-if="associatedChannelVisible"
      :factory-code="selectedRow.factoryCode"
      @close="associatedChannelVisible=false;selectedRow={}"
    />
  </div>
</template>
<script>
import { pagePayFactory } from '@/api/sys-pay'
import { payChannelGroups } from '@/constant/type'
import EditForm from './edit-form'
import AssociatedChannel from './associated-channel'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination, EditForm, AssociatedChannel },
  data() {
    return {
      associatedChannelVisible: false,
      payChannelGroups,
      formEditVisible: false,
      selectedRow: {},
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 30,
        factoryCode: '',
        factoryName: ''
      },
      listLoading: true,
      uploadLoading: false
    }
  },
  created() {
    this.renderData()
  },
  methods: {
    renderData(isClean) {
      const that = this
      that.listLoading = true
      if (isClean === true) {
        that.list = []
        that.listQuery.cursor = 1
      }
      pagePayFactory(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData(true)
    },
    createChannel() {
      const that = this
      that.selectedRow = {}
      that.formEditVisible = true
    },
    clickEdit(row) {
      const that = this
      that.selectedRow = row
      that.formEditVisible = true
    },
    formEditSuccess(res) {
      this.selectedRow = {}
      this.formEditVisible = false
      this.renderData(res.event === 'ADD')
    },
    clickAssociatedChannels(row) {
      this.selectedRow = row
      this.associatedChannelVisible = true
    }
  }
}
</script>
<style scoped lang="scss">
  .bottom {
    color: #666;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 25px;
    .line {
      display: flex;
      >div {
        width: 100%;
      }
    }
  }

</style>
