<template>
  <div class="subPage">
    <form class="mainForm">
      <ul>
        <li class="flex-h">
          <span>桌位名称：</span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入名称"
                v-model="postData.TableNo"
                maxlength="6"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <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-select v-model="postData.AreaId" placeholder="请选择">
                <el-option
                  v-for="item in caseAreas"
                  :key="item.AreaId"
                  :label="item.AreaName"
                  :value="item.AreaId">
                </el-option>
              </el-select>
            </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 {
      caseAreaList: [],
      postData: {
        TableId: '', // 桌位id
        AreaId: '', // 区域id
        CaseId: '', // 案场id
        OrgId: '', // 机构id
        TableNo: '', // 桌位编号
        Order: '', // 排序
      },
    }
  },
  created () {
    this.getAreaList()
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      caseids: x => x.app.cases.list.map(i => i.CaseId).join(','),
      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
        this.postData.AreaId = ''
      }
    },
    caseAreas () {
      return this.caseAreaList.filter(x => x.CaseId === this.CaseId)
    },
  },
  components: {},
  methods: {
    getAreaList () {
      this.$ajax(this.$api.caseManager.getCaseAreaList.url, {
        method: this.$api.caseManager.getCaseAreaList.method,
        queryData: {
          caseid: this.caseids, // 案场id
          page: 1, // 当前页码
          pagesize: 10000, // 请求数据量
        }
      }).then(res => {
        this.caseAreaList = res.list
        // this.postData.AreaId = res.list[0].AreaId
      })
    },
    submit () { // 提交数据
      if (this.postData.TableNo === '') {
        this.$message({
          type: 'error',
          message: '桌位名称不能为空'
        })
        return false
      }
      if (this.postData.AreaId === '' || this.CaseId === '') {
        this.$message({
          type: 'error',
          message: '请选择案场/区域'
        })
        return false
      }
      this.postData.OrgId = this.OrgId
      this.$ajax(this.$api.caseManager.addCaseTable.url, {
        method: this.$api.caseManager.addCaseTable.method,
        data: { ...this.postData, CaseId: this.CaseId }
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.$router.push({ name: 'caseTableManager' })
      })
    },
    cancel () {
      this.$router.push({ name: 'caseTableManager' })
    }
  },
  mounted () { }
}
</script>

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