<template>
  <div class="app-container">
    <el-divider content-position="left">会员套餐配置</el-divider>
    <div class="list">
      <div v-for="(item, index) in packageList" :key="index" class="section">
        <div class="control">
          <span>套餐{{ index + 1 }}</span>
        </div>
        <div class="control">
          <span>期限</span>
          <el-input v-model="item.duration" size="mini" style="width: 80px; margin-right: 16px" />
          <el-select v-model="item.unit" placeholder="请选择" size="mini">
            <el-option label="月" :value="1" />
            <el-option label="年" :value="2" />
          </el-select>
        </div>
        <div class="control">
          <span>原价</span>
          <el-input v-model="item.originalPrice" size="mini" />
          <span>元</span>
        </div>
        <div class="control">
          <span>现价</span>
          <el-input v-model="item.price" size="mini" />
          <span>元</span>
        </div>
        <div class="control">
          <el-button
            v-if="index === packageList.length - 1"
            type="primary"
            size="mini"
            @click="insertOnePackage"
          >添加</el-button>
          <el-button
            v-if="canRemovePackage"
            type="danger"
            size="mini"
            @click="removeOnePackage(item, index)"
          >删除</el-button>
        </div>
      </div>
    </div>
    <el-divider content-position="left">会员订制服务配置</el-divider>
    <div class="list">
      <div v-for="(item, index) in serviceList" :key="index" class="section">
        <div class="control">
          <span>订制服务{{ index + 1 }}</span>
        </div>
        <div class="control">
          <span>订制服务名称</span>
          <el-input v-model="item.name" size="mini" />
        </div>
        <div class="control">
          <el-button
            v-if="index === serviceList.length - 1"
            type="primary"
            size="mini"
            @click="insertOneSerivice"
          >添加</el-button>
          <el-button
            v-if="canRemoveService"
            type="danger"
            size="mini"
            @click="removeOneService(item, index)"
          >删除</el-button>
        </div>
      </div>
    </div>
    <el-divider />
    <!-- 确认 取消 -->
    <div class="control-box">
      <el-button type="warning" @click="navigateBack">返回</el-button>
      <el-button type="primary" @click="updateConfigure">保存</el-button>
    </div>
  </div>
</template>

<script>
import { fetchConfigureList, updateConfigure } from '@/api/member'
export default {
  name: 'ComboEdit',
  data() {
    return {
      dateType: 1,
      packageList: [],
      serviceList: []
    }
  },
  computed: {
    canRemovePackage() {
      return this.packageList.length > 1
    },
    canRemoveService() {
      return this.serviceList.length > 1
    }
  },
  created() {
    this.fetchConfigureList()
  },
  methods: {
    // 获取套餐 服务配置列表
    fetchConfigureList() {
      fetchConfigureList()
        .then((res) => {
          this.packageList = res.data.packageList
          this.serviceList = res.data.serviceList
          if (this.packageList.length === 0) {
            this.insertOnePackage()
          }
          if (this.serviceList.length === 0) {
            this.insertOneSerivice()
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 校验文本框内容
    validateInputContent() {
      const hasEmptyPackage = this.packageList.some((item) => {
        for (const key in item) {
          if (!item[key]) return false
        }
        return true
      })
      const hasEmptyService = this.serviceList.some((item) => {
        for (const key in item) {
          if (!item[key]) return false
        }
        return true
      })
      return hasEmptyService && hasEmptyPackage
    },
    // 更新套餐 服务配置列表
    updateConfigure() {
      if (!this.validateInputContent()) {
        this.$message.warning('已添加项不能为空')
        return
      }
      updateConfigure({ packageList: this.packageList, serviceList: this.serviceList })
        .then((res) => {
          console.log(res)
          this.$message.success('套餐配置保存成功')
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 添加一条空套餐记录
    insertOnePackage() {
      this.packageList.push({
        duration: '',
        unit: 1,
        originalPrice: '',
        price: ''
      })
    },
    // 添加一条空服务记录
    insertOneSerivice() {
      this.serviceList.push({
        name: ''
      })
    },
    // 删除一条套餐记录
    removeOnePackage(item, index) {
      if (item.id) {
        this.$confirm('确认删除该项配置?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.packageList.splice(index, 1)
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      } else {
        this.packageList.splice(index, 1)
      }
    },
    // 删除一条服务记录
    removeOneService(item, index) {
      if (item.id) {
        this.$confirm('确认删除该项配置?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.serviceList.splice(index, 1)
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      } else {
        this.serviceList.splice(index, 1)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.list {
  margin-bottom: 60px;
}
.section {
  margin: 24px 0;
  margin-left: 32px;
  .control {
    display: inline-block;
    margin-bottom: 10px;
    margin-right: 10px;
    > span,
    > .el-input {
      display: inline-block;
      vertical-align: middle;
    }
    > span {
      margin: 0 10px;
      font-size: 14px;
    }
    > .el-input {
      width: 160px;
    }
    > .el-select {
      width: 100px;
    }
  }
}
</style>
