<template>
  <div v-loading="isLoading" class="app-container">
    <!-- 搜索区域 -->
    <div class="filter-container">
      <div class="filter-control tip">配置订制化服务功能<span>（以下为设置会员特定的订制服务功能）</span></div>
      <div class="filter-control" />
    </div>
    <!-- 表格区域 -->
    <el-table :data="serviceList" border fit height="600" header-row-class-name="tableHeader">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="订制需求" prop="title" align="center" />
      <el-table-column label="是否配置" align="center" width="120">
        <template slot-scope="{ row }">
          <el-checkbox v-model="row.checkFlag" :true-label="1" :false-label="0" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 确认 取消 -->
    <div class="control-box">
      <el-button type="warning" @click="navigateBack">返回</el-button>
      <el-button type="primary" @click="updateUserMenusConfigure">保存</el-button>
    </div>
  </div>
</template>

<script>
import { updateUserMenusConfigure, fetchUserMenusConfigure } from '@/api/member'
export default {
  data() {
    return {
      isLoading: false,
      authUserId: '',
      serviceList: [],
      roleIds: []
    }
  },
  created() {
    this.authUserId = parseInt(this.$route.query.id)
    this.fetchUserMenusConfigure()
  },
  methods: {
    // 修改列表
    updateUserMenusConfigure() {
      this.roleIds = this.serviceList.filter(item => item.checkFlag === 1).map(item => item.roleId)
      this.isLoading = true
      updateUserMenusConfigure({ roleIds: this.roleIds.join(','), authUserId: this.authUserId }).then(res => {
        this.$message.success('保存成功')
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.back()
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 获取列表
    fetchUserMenusConfigure() {
      this.isLoading = true
      fetchUserMenusConfigure({
        authUserId: this.authUserId
      }).then(res => {
        this.serviceList = res.data
      }).finally(() => {
        this.isLoading = false
      })
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  width: 800px;
  margin: 0 auto;
  .filter-container {
    display: flex;
    justify-content: space-between;
    align-items: center;

    margin-bottom: 16px;

    .filter-control {
      margin: 0;
    }
  }
  .tip {
    font-size: 14px;
    span {
      color: #f56c6c;
    }
  }
}
</style>
