<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.AreaName"
                clearable
                maxlength="5"
                >
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>案场：<em>*</em></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>图片：<em>*</em></span>
          <div class="flex-item">
            <div class="red-hint">建议上传30*30尺寸的图片</div>
            <div>
              <el-upload
                class="avatar-uploader"
                action='string'
                :before-upload="toolClass.beforeUpload"
                :http-request="toolClass.upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess">
                <img v-if="postData.AreaIcon" :src="postData.AreaIcon" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>黑白图片：<em>*</em></span>
          <div class="flex-item">
            <div class="red-hint">建议上传30*30尺寸的图片</div>
            <div>
              <el-upload
                class="avatar-uploader"
                action='string'
                :before-upload="toolClass.beforeUpload"
                :http-request="toolClass.upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccesser">
                <img v-if="postData.AreaIconWhite" :src="postData.AreaIconWhite" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </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, mapActions } from 'vuex'

export default {
  name: '',
  data () {
    return {
      postData: {
        AreaId: '', // 区域id
        CaseId: '', // 案场id
        OrgId: '', // 机构id
        AreaName: '', // 区域名称
        AreaIcon: '', // 区域图标
        AreaIconWhite: '', // 区域图标灰白
      },
    }
  },
  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: {},
  created () {
    this.picSize({ w: 30, h: 30 })
  },
  methods: {
    ...mapActions([
      'picSize'
    ]),
    handleAvatarSuccesser (res, file) {
      this.postData.AreaIconWhite = res.result.url
    },
    handleAvatarSuccess (res, file) {
      this.postData.AreaIcon = res.result.url
    },
    submit () { // 提交数据
      if (this.postData.AreaName === '') {
        this.$message({
          type: 'error',
          message: '区域名称不能为空'
        })
        return false
      }
      if (this.CaseId === '') {
        this.$message({
          type: 'error',
          message: '案场不能为空'
        })
        return false
      }
      if (this.postData.AreaIcon === '') {
        this.$message({
          type: 'error',
          message: '请上传区域图片'
        })
        return false
      }
      if (this.postData.AreaIconWhite === '') {
        this.$message({
          type: 'error',
          message: '请上传区域黑白图片'
        })
        return false
      }
      this.postData.OrgId = this.OrgId
      this.$ajax(this.$api.caseManager.addCaseArea.url, {
        method: this.$api.caseManager.addCaseArea.method,
        queryData: { ...this.postData, CaseId: this.CaseId }
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.$router.push({ name: 'caseAreaManager' })
      })
    },
    cancel () {
      this.$router.push({ name: 'caseAreaManager' })
    }
  },
  mounted () { }
}
</script>

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