<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>标签名称：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入名称"
                v-model="postData.TagName"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>拥有案场权限：<em>*</em></span>
          <div class="flex-item">
            <div>
              <el-checkbox-group v-model="checkedList" @change="handleCheckedCitiesChange">
                <el-checkbox v-for="(item,index) in cases" :label="item.CaseId" :key="index">{{item.CaseName}}</el-checkbox>
              </el-checkbox-group>
            </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: {
        TagId: '', // 桌位id
        TagName: '', // 区域id
        caseids: '', // 案场id
        OrgId: '', // 机构id
      },
      checkedList: [],
    }
  },
  created () {},
  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
      },
      set (val) {
        this.postData.CaseId = val
      }
    }
  },
  components: {},
  methods: {
    handleCheckedCitiesChange (val) {
      this.postData.caseids = val.join(',')
    },
    submit () { // 提交数据
      if (this.postData.TagName === '') {
        this.$message({
          type: 'error',
          message: '标签名称不能为空'
        })
        return false
      }
      if (this.postData.caseids === '') {
        this.$message({
          type: 'error',
          message: '案场不能为空'
        })
        return false
      }
      this.postData.OrgId = this.OrgId
      this.$ajax(this.$api.caseManager.addCaseTag.url, {
        method: this.$api.caseManager.addCaseTag.method,
        data: { ...this.postData }
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.$router.push({ name: 'tagManager' })
      })
    },
    cancel () {
      this.$router.push({ name: 'tagManager' })
    }
  },
  mounted () { }
}
</script>

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