<template>
  <el-container>

    <el-main>
      <!-- 查询 -->
      <el-form v-if="isSearchVisible" ref="formData" :model="from" style="width: 100%;" label-width="auto">
        <div class="level-screen-view">
          <el-form-item label="等级名称" prop="levelName">
            <el-input v-model="from.levelName" placeholder="等级名称"></el-input>
          </el-form-item>
          <el-form-item label="隐藏等级" prop="hideLevel">
            <el-select v-model="from.hideLevel">
              <el-option v-for="itemLevel in from.isHideLevelList" :key="itemLevel.type" :label="itemLevel.name"
                         :value="itemLevel.type" style="width: 100%"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="from.remark" placeholder="备注"></el-input>
          </el-form-item>
          <el-form-item label="创建时间" prop="creationTime">
              <CustomChoiceDatePicker @handelChange="handleChoiceTime"></CustomChoiceDatePicker>
          </el-form-item>
        </div>

        <div class="level-screen-view">
          <div></div>
          <div>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </div>
        </div>

      </el-form>

      <!-- 操作按钮 -->
      <div class="level-screen-view" style="margin-top: 16px;">
        <div>
          <el-button type="primary" icon="el-icon-refresh-right" @click="initDataView(true)"></el-button>
          <el-button type="primary" icon="el-icon-plus" style="background-color: #13CE66;border: #13CE66;"
                     @click="openEditorDialog">新建等级
          </el-button>
        </div>
        <div>
          <el-button type="primary" icon="el-icon-search" @click="isSearchVisible = !isSearchVisible"></el-button>
        </div>
      </div>

      <!-- 数据 -->
      <div style="padding-top: 16px;">
        <el-table ref="singleTable" :data="from.tableList" border style="width: 100%;">
          <el-table-column width="99" type="index" label="序号" align="center"></el-table-column>
          <el-table-column prop="id" label="等级ID" width="99" align="center">
          </el-table-column>
          <el-table-column prop="name" label="等级名称" align="center">
          </el-table-column>
          <el-table-column prop="weight" label="等级权重" align="center">
          </el-table-column>
          <el-table-column prop="selfDiscountRate" label="自购折扣" align="center">
          </el-table-column>
          <el-table-column prop="discountCodeRate" label="优惠码折扣" align="center">
          </el-table-column>
          <el-table-column prop="promoCommissionRate" label="推广佣金比例" align="center">
          </el-table-column>
          <el-table-column prop="memo" label="备注" align="center">
          </el-table-column>
          <el-table-column prop="isAgent_Text" label="隐藏等级" align="center">
          </el-table-column>
          <el-table-column label="操作" align="center" min-width="100" fixed="right">
            <template slot-scope="scope">
              <CustomButton type="primary" tip-text="编辑" is-svg icon="edit"
                            @onClick="openEditorDialog(scope.row)"></CustomButton>
              <CustomButton type="danger" tip-text="删除" is-svg icon="delete"
                            @onClick="openDelete(scope.row)"></CustomButton>
            </template>
          </el-table-column>
        </el-table>
      </div>

        <!-- 分页 -->
        <div style="padding-top: 32px; display: flex; justify-content: center">
          <el-pagination
            :current-page="from.currentPage"
            :page-sizes="[5, 10, 15, 20, 30]"
            :page-size="20"
            layout="total, prev, pager, next, sizes, jumper"
            :total="from.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange">
          </el-pagination>
        </div>

    </el-main>

    <!-- 编辑的弹窗 -->
    <EditorDialog
      :show-dialog="isEditorDialogVisible"
      :dialog-title="editorTitle"
      :select-item="editorSelectItem"
      @handle-close="isEditorDialogVisible = false"
      @handle-success="initDataView"></EditorDialog>

  </el-container>
</template>

<script>
import EditorDialog from './components/editor-dialog'
import CustomButton from '@/components/CustomButton/index'
import CustomChoiceDatePicker from '@/components/CustomChoiceDatePicker'
import { masterUserLevelList, delData } from '@/api/master-info-manage/master-level'
export default {
  components: { EditorDialog, CustomButton, CustomChoiceDatePicker },
  data () {
    return {
      // UI控制
      isSearchVisible: false,
      isEditorDialogVisible: false,
      editorTitle: '',
      editorSelectItem: {},
      // 请求数据
      from: {
        // 等级名称
        levelName: '',
        // 隐藏等级
        hideLevel: '',
        // 备注
        remark: '',
        // 创建时间
        creationTime: '',
        // 其它数据
        tableList: [],
        isHideLevelList: [],
        currentPage: 1,
        limit: 20,
        total: 0
      }
    }
  },
  created() {
    // 创建
    this.initDataView()
  },
  mounted() {
  },
  methods: {
    // 查询
    submitForm() {
      this.initDataView()
    },
    // 重置
    resetForm() {
      this.from.creationTime = ''
      this.$refs.formData.resetFields()
    },
    // 添加
    openEditorDialog(row) {
      if (row === undefined) {
        // 选择添加
        this.editorTitle = '新建等级'
        this.editorSelectItem = {}
      } else {
        this.editorTitle = '编辑'
        this.editorSelectItem = row
      }
      this.isEditorDialogVisible = true
    },
    // 删除
    openDelete(row) {
      // 选择删除
      this.$confirm('是否删除等级，删除后不可恢复', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = [row.id]
        this._delData(ids)
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 选中时间
    handleChoiceTime (val) {
      console.log(JSON.stringify(val))
      // 选择右边菜单
      this.from.creationTime = val[0] + ' - ' + val[1]
    },
    // limit 改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.from.limit = val
      this.initDataView()
    },
    // 页面改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.from.currentPage = val
      this.initDataView()
    },
    // 获取数据
    initDataView(needToast = false) {
      this.isEditorDialogVisible = false
      const params = {
        name: this.from.levelName,
        memo: this.from.remark,
        isHideLevel: this.from.hideLevel,
        createTime: this.from.creationTime,
        p: this.from.currentPage,
        limit: this.from.limit
      }
      masterUserLevelList(params).then(resp => {
        if (parseInt(resp.code) === 0) {
          if (needToast) this.$message.success('刷新成功~')
          this.from.tableList = resp.data.levelList
          this.from.isHideLevelList = resp.data.isHideLevel
          this.from.total = resp.data.total
        } else {
          this.$message.error(resp.msg)
        }
      })
    },
    // 删除数据
    _delData(ids) {
      const params = {
        ids: ids
      }
      delData(params).then(resp => {
        if (resp.code === 0) {
          this.$message.success('删除成功～')
          this.initDataView()
        } else {
          this.$message.error(resp.msg)
        }
      })
    }
  }
}
</script>

<style scoped>
.level-screen-view{
  display: flex;
  justify-content: space-between;
}
.level-screen-itemView{
  display: flex;
  justify-content: flex-end;
  width: 25%;
}
.level-screen-itemView-title{
  width: 30%;
  text-align: right;
  margin-right: 20px;
}
.level-screen-itemView-rightInput{
  width: 60%;
  margin-right: 15px;
  /*margin-left: 20px;*/
}
.level-screen-itemView-rightList{
  width: 60%;
  margin-right: 15px;
  /*margin-left: 20px;*/
}
.level-screen-itemView-finder{
  width: 60%;
  margin-right: 15px;
  /*margin-left: 20px;*/
}
</style>
