<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>选择案场：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-select v-model="CaseId" placeholder="请选择">
                <el-option
                  v-for="item in cases"
                  :key="item.CaseId"
                  :label="item.CaseName"
                  :value="item.CaseId">
                </el-option>
              </el-select>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>渠道名：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入渠道名"
                v-model="postData.ChannelName"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li style="text-align:center">
          <el-button type="primary" size="mini" @click="submit">保存</el-button>
          <el-button type="danger" size="mini" @click="cancel">取消</el-button>
        </li>
      </ul>
    </form>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: '',
  data () {
    return {
      postData: {
        ChannelId: '', // 渠道id
        ChannelName: '', // 渠道名称
        CaseId: '', // 案场id
        OrgId: '', // 机构id
      }
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      OrgId: x => x.app.user.OrgId,
    }),
    CaseId: {
      get () {
        return this.postData.CaseId === '' ? this.defaultCaseId || '' : this.postData.CaseId
      },
      set (val) {
        this.postData.CaseId = val
      }
    }
  },
  created () {
    this.getChannelInfo()
  },
  components: {},
  methods: {
    getChannelInfo () {
      this.$ajax(this.$api.channelManager.getChannelInfo.url, {
        method: this.$api.channelManager.getChannelInfo.method,
        urlData: { channelId: this.$route.query.id }
      }).then(res => {
        this.postData = res
      })
    },
    submit () { // 提交数据
      if (this.postData.ChannelName === '') {
        this.$message({
          type: 'error',
          message: '渠道名称不能为空'
        })
        return false
      }
      this.postData.OrgId = this.OrgId
      if (this.postData.CaseId === '') this.postData.CaseId = this.CaseId
      this.$ajax(this.$api.channelManager.editChannel.url, {
        method: this.$api.channelManager.editChannel.method,
        data: this.postData
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.$router.push({ name: 'channelList' })
      })
    },
    cancel () {
      this.$router.push({ name: 'channelList' })
    }
  },
  mounted () { }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
