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

    <div class="filter-container">
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        {{ $t('pages.roomThemeManager.action.add') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.roomThemeManager.loading')"
      fit
      highlight-current-row
    >
      <el-table-column :label="$t('pages.roomThemeManager.table.themeBack')" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 60px; height: 80px"
            :src="scope.row.themeBack"
            :preview-src-list="[scope.row.themeBack]"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.roomThemeManager.table.status')" align="center">
        <template slot-scope="scope">
          <div>
            <span v-if="scope.row.showcase === false">{{ $t('pages.roomThemeManager.status.offShelf') }}</span>
            <span v-if="scope.row.showcase === true">{{ $t('pages.roomThemeManager.status.onShelf') }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="themeMoney" :label="$t('pages.roomThemeManager.table.themeMoney')" align="center" />
      <el-table-column prop="createTime" :label="$t('pages.roomThemeManager.table.createdAt')" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.roomThemeManager.table.actions')" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click.native="handleUpdate(scope.row)">{{ $t('pages.roomThemeManager.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"
    />
    <el-dialog
      :title="textOptTitle"
      :visible.sync="formVisible"
      :modal-append-to-body="true"
      :append-to-body="true"
      :before-close="handleClose"
      width="400px"
    >
      <div v-loading="submitLoading">
        <el-form ref="form" :model="form" :rules="rules" label-width="110px">
          <el-form-item :label="$t('pages.roomThemeManager.form.themeBack')" prop="themeBack">
            <upload-image
              v-model="form.themeBack"
              :file-dir="$application.fileBucket.back"
            />
          </el-form-item>
          <el-form-item :label="$t('pages.roomThemeManager.form.status')" prop="showcase">
            <el-select
              v-model="form.showcase"
              :placeholder="$t('pages.roomThemeManager.placeholder.status')"
              clearable
              style="width:100%;"
              class="filter-item"
            >
              <el-option :label="$t('pages.roomThemeManager.status.onShelf')" :value="true" />
              <el-option :label="$t('pages.roomThemeManager.status.offShelf')" :value="false" />
            </el-select>
          </el-form-item>

          <el-form-item :label="$t('pages.roomThemeManager.form.themeMoney')" prop="themeMoney">
            <el-input v-model.trim="form.themeMoney" type="number" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()">{{ $t('pages.roomThemeManager.action.save') }}</el-button>
            <el-button @click="handleClose()">{{ $t('pages.roomThemeManager.action.cancel') }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { themeTable, updateRoomTheme, deleteRoomTheme, addRoomTheme } from '@/api/room-theme'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20
      },
      formVisible: false,
      dialogType: '',
      // fileList: [],
      form: {
        id: '',
        themeBack: '',
        showcase: '',
        themeMoney: ''
      },
      submitLoading: false,
      listLoading: true
    }
  },
  computed: {
    textOptTitle() {
      return this.dialogType === 'edit'
        ? this.$t('pages.roomThemeManager.dialog.editTitle')
        : this.$t('pages.roomThemeManager.dialog.addTitle')
    },
    rules() {
      return {
        themeBack: [
          { required: true, message: this.$t('pages.roomThemeManager.validation.themeBack'), trigger: 'change' }
        ],
        showcase: [
          { required: true, message: this.$t('pages.roomThemeManager.validation.status'), trigger: 'blur' }
        ],
        themeMoney: [
          { required: true, message: this.$t('pages.roomThemeManager.validation.themeMoney'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.renderData()
  },
  methods: {
    renderData() {
      const that = this
      that.listLoading = true
      themeTable(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        // that.fileList = []
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData()
    },
    clearForm() {
      this.form.id = ''
      this.form.themeBack = ''
      this.form.showcase = ''
      this.form.themeMoney = ''
    },
    submitForm() {
      const that = this
      that.$refs.form.validate(valid => {
        if (valid) {
          that.submitLoading = true
          if (that.form.id) {
            updateRoomTheme(that.form).then(res => {
              that.submitLoading = false
              that.formVisible = false
              that.clearForm()
              that.renderData()
            }).catch(er => {
              that.submitLoading = false
              console.error(er)
              this.$emit('fail')
            })
            return
          }
          addRoomTheme(that.form).then(res => {
            that.submitLoading = false
            that.formVisible = false
            that.clearForm()
            that.renderData()
          }).catch(er => {
            that.submitLoading = false
            console.error(er)
          })
        } else {
          console.error('error submit!!')
          return false
        }
      })
    },
    handlDel(id) {
      this.$confirm(this.$t('pages.roomThemeManager.message.confirmDelete'), this.$t('pages.roomThemeManager.message.tips'), {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        deleteRoomTheme(id).then((res) => {
          this.listLoading = false
          this.$message({
            message: this.$t('pages.roomThemeManager.message.deleteSuccess'),
            type: 'success'
          })
          this.renderData()
        })
      }).catch(() => {

      })
    },
    handleClose() {
      this.formVisible = false
    },
    handleCreate() {
      this.formVisible = true
      this.dialogType = 'add'
      this.clearForm()
      console.log(' this.form', this.form)
    },
    handleUpdate(row) {
      this.dialogType = 'edit'
      this.formVisible = true
      this.clearForm()
      this.form = Object.assign(this.form, row)
    }
  }
}
</script>
