<template>
  <div class="app-container">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="关联设备：">
        <el-select v-model="form.deviceIds" multiple placeholder="请选择关联设备">
          <el-option v-for="item in devOptions" :key="item.value" :label="item.label" :value="item.value.toString()" />
        </el-select>
      </el-form-item>
      <el-form-item label="套餐名称：">
        <el-input v-model="form.name" placeholder="请输入套餐名称" />
      </el-form-item>
      <el-form-item label="服务类型：">
        <el-select v-model="form.businessType" placeholder="请选择服务类型" :disabled="form.id && typeList[0] === '12'">
          <el-option v-for="item in serviceTypeOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="typeList[0] === '13'" label="资源平台：">
        <el-select v-model="form.platformId" placeholder="请选择资源平台">
          <el-option v-for="item in platformOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item v-else label="流量套餐：">
        <el-select v-model="form.goodsId" placeholder="请选择流量套餐" :disabled="form.id ? true : false">
          <el-option v-for="(item, index) in goodsOptions" :key="index" :label="item" :value="index" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.platformId === 10" label="栏目：">
        <el-select v-model="form.groupId" placeholder="请选择爱听栏目">
          <el-option v-for="item in sceneOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="原价：" prop="price">
        <el-input-number v-model="form.price" :min="0" :max="500" :controls="false" :precision="2"
          placeholder="请输入原价" />
      </el-form-item>
      <el-form-item label="活动价：" prop="discount">
        <el-input-number v-model="form.discount" :min="0" :max="500" :controls="false" :precision="2"
          placeholder="请输入活动价" />
      </el-form-item>
      <el-form-item label="服务时长：">
        <el-select v-model="form.indate" placeholder="请选择服务时长">
          <el-option v-for="item in serviceTimeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否推荐：">
        <el-select v-model="form.isRecommend" placeholder="请选择是否推荐">
          <el-option v-for="item in recommendOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="排序：" v-if="form.businessType === 5">
        <el-input-number v-model="form.sort" :min="1" />
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="getSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { devMixin, serviceTimeMixin, serviceTypeMixin } from '@/mixin/index'
import { list } from '@/api/operation/scene'
import { detail, edit, create, goodsList } from '@/api/service/package'
export default {
  mixins: [devMixin, serviceTimeMixin, serviceTypeMixin],
  data() {
    let checkedPrice = (rule, value, callback) => {
      if (Number(value) > 500) {
        callback(new Error('原价不得超过500'))
      } else {
        if (this.form.discount !== undefined) {
          this.$refs.form.validateField('discount')
        }
        callback()
      }
    }
    let checkedDiscount = (rule, value, callback) => {
      if (Number(value) > Number(this.form.price)) {
        callback(new Error('活动价不得大于原价！'))
      } else {
        callback()
      }
    }
    return {
      // 表单
      form: {},
      // 区分
      typeList: this.$route.query.typeList.split(','),
      // 流量套餐
      goodsOptions: [],
      // 爱听专区
      sceneOptions: [],
      // 关联设备
      deviceIds: [],
      // 资源平台
      platformOptions: [{
        value: 6,
        label: 'QQ音乐'
      }, {
        value: 9,
        label: '酷狗音乐'
      }, {
        value: 10,
        label: '爱听音乐'
      }],
      // 是否推荐
      recommendOptions: [{
        value: 0,
        label: '是'
      }, {
        value: 1,
        label: '否'
      }],
      // 校验
      rules: {
        price: [{
          validator: checkedPrice,
          trigger: 'blur'
        }],
        discount: [{
          validator: checkedDiscount,
          trigger: 'blur'
        }]
      }
    }
  },
  mounted() {
    this.typeList[0] === '12' ? this.getGoodSList() : this.getSceneList()
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.getList()
    }
  },
  methods: {
    // 详情
    getList() {
      detail(this.form.id).then(res => {
        if (res.code === 0) {
          this.form = res.data
          this.form.deviceIds = res.data.deviceIds.split(',')
        }
      })
    },

    // 流量套餐
    getGoodSList() {
      goodsList().then(res => {
        if (res.code === 0) {
          this.goodsOptions = res.data
        }
      })
    },

    // 爱听专区
    getSceneList() {
      list().then(res => {
        if (res.code === 0) {
          res.data.map(i => {
            this.sceneOptions.push({
              value: i.id,
              label: i.name
            })
          })
        }
      })
    },


    // 提交
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.deviceIds = this.form.deviceIds.join(',')
          if (this.form.id) {
            edit(this.form).then(res => {
              if (res.code === 0) {
                this.$message.success('修改成功！')
                this.cancel()
              }
            })
          } else {
            create(this.form).then(res => {
              if (res.code === 0) {
                this.$message.success('新增成功！')
                this.cancel()
              }
            })
          }
        } else {
          return false
        }
      })
    },

    // 取消
    cancel() {
      let url = this.typeList[0] === '13' ? 'musicPackage' : 'flowPackage'
      this.$tab.closeOpenPage(`/service/${url}?typeList=${this.typeList}`);
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 500px;
}
</style>