<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.platform" placeholder="请选择流量平台" :disabled="disabled()">
          <el-option v-for="item in typeOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="关联设备：">
        <el-select v-model="deviceIds" filterable 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="disabled()">
          <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="请选择资源平台" :disabled="disabled()">
          <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="disabled()">
          <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 label='赠送会员：'>
        <el-switch v-model="form.isFreeFlow" />
      </el-form-item>
      <el-form-item v-if="form.isFreeFlow" label="关联会员套餐：">
        <el-select v-model="form.freeOptions" placeholder="请选择会员套餐：">
          <el-option v-for="item in form.freeOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </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: {},
      // 关联设备
      deviceIds: [],
      // 区分
      typeList: [],
      // 流量套餐
      goodsOptions: [],
      // 爱听专区
      sceneOptions: [],
      // 资源平台
      platformOptions: [{
        value: 6,
        label: 'QQ音乐'
      }, {
        value: 9,
        label: '酷狗音乐'
      }, {
        value: 10,
        label: '爱听音乐'
      }],
      // 是否推荐
      recommendOptions: [{
        value: 0,
        label: '是'
      }, {
        value: 1,
        label: '否'
      }],
      // 流量平台
      typeOptions: [{
        value: 0,
        label: '树米'
      }, {
        value: 1,
        label: '联通'
      }],
      // 校验
      rules: {
        price: [{
          validator: checkedPrice,
          trigger: 'blur'
        }],
        discount: [{
          validator: checkedDiscount,
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    typeList(val) {
      if (val[0] !== '12') {
        this.getSceneList()
      }
    },
    'form.businessType'(val) {
      if (val !== 5) {
        delete this.form.sort
      }
      this.getDeviceGoodsList(this.deviceIds ?? [], val, this.form.isFreeFlow ?? false)

    },
    'form.platform'(val) {
      this.getDevList({ type: val })
      this.getGoodSList(val)
    },
    'form.isFreeFlow'(val) {
      this.getDeviceGoodsList(this.deviceIds ?? [], this.form.businessType ?? -1, val)
    }
  },
  mounted() {
    this.typeList = this.$route.query.typeList.split(',')
    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.deviceIds = res.data.deviceIds.split(',')
        }
      })
    },

    // 流量套餐
    getGoodSList(e) {
      goodsList(e).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
            })
          })
        }
      })
    },
    // 流量套餐
    getDeviceGoodsList(deviceIds, businessType, isFreeFlow) {
      if (this.typeList[0] != "18" || (deviceIds ?? []).length === 0 || (businessType ?? -1) < 0 || (isFreeFlow ?? false) != true) {
        console.log('设备id或业务类型为空', this.typeList[0], deviceIds, businessType, isFreeFlow)
        return
      }
      console.log('开始请求', this.typeList[0], deviceIds.join(","), businessType, isFreeFlow)

      deviceGoodsList(this.typeList[0], businessType, deviceIds.join(",")).then((res) => {
        console.log("流量套餐", JSON.stringify(res.data));
        if (res.code === 0) {
          this.form.freeOptions = res.data;
        }
      });
    },

    // 提交
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.deviceIds = this.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}`);
    },

    // 禁止修改
    disabled() {
      return this.form.id ? true : false
    }
  }
}
</script>

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