const mixin = {
  data() {
    return {
      // 是否推荐
      recommendOptions: [{
        value: 0,
        label: '是'
      }, {
        value: 1,
        label: '否'
      }],
      // 当前状态
      statusOptions: [{
        value: 1,
        label: '上架'
      }, {
        value: 4,
        label: '下架'
      }]
    }
  }
}

const flowPackageMixin = {
  data() {
    return {
      // 运营商
      operatorOptions: [{
        value: 0,
        label: '移动'
      }, {
        value: 1,
        label: '联通'
      }, {
        value: 2,
        label: '双网双号'
      }]
    }
  }
}

const musicPackageMixin = {
  data() {
    return {
      // 资源平台
      platformOptions: [{
        value: 1,
        label: 'QQ音乐'
      }]
    }
  }
}

import { goodsList } from '@/api/service/musicPackage'
const goodsMixin = {
  data() {
    return {
      // 流量套餐
      goodsOptions: []
    }
  },
  mounted() {
    goodsList().then(res => {
      if (res.code === 0) {
        this.goodsOptions = res.data
      }
    })
  },
}

import {
  list,
  remove,
  shelve
} from '@/api/service/musicPackage'
const indexMixin = {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 列表
      tableData: [],
      total: 0
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}?`, '提示', {
        'confirmButtonText': '确定',
        'cancelButtonText': '取消',
        type: 'warning'
      }).then(() => {
        remove(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      }).catch(() => {})
    },

    // 下架
    getChange(row) {
      this.$confirm(`是否下架${row.name}?`, '提示', {
        'confirmButtonText': '确定',
        'cancelButtonText': '取消',
        type: 'warning'
      }).then(() => {
        shelve(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('下架成功！')
            this.getList()
          }
        })
      }).catch(() => {})
    },

    // 当前状态
    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 是否推荐
    recommendFormatter(row) {
      return this.selectDictLabel(this.recommendOptions, row.isRecommend)
    },
    // 关联设备
    devFormatter(row) {
      return row.deviceIds ? row.deviceIds.split(',').map(i => this.selectDictLabel(this.devOptions, i)).join(',') : ''
    },
  },
}

import {
  detail,
  edit,
  create
} from '@/api/service/musicPackage'
const detailMixin = {
  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 {
      // 关联设备
      deviceIds: [],
      // 校验
      rules: {
        price: [{
          validator: checkedPrice,
          trigger: 'blur'
        }],
        discount: [{
          validator: checkedDiscount,
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    deviceIds(val) {
      this.form.deviceIds = val.join(',')
    }
  },
  mounted() {
    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(',')
        }
      })
    },
    // 提交
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          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
        }
      })
    }
  }
}

export {
  mixin,
  flowPackageMixin,
  musicPackageMixin,
  indexMixin,
  detailMixin,
  goodsMixin
}
