<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.AreaName"
                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>
              <el-upload
                class="avatar-uploader"
                :action='$api.file.image.url'
                :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>黑白图片：</span>
          <div class="flex-item">
            <div>
              <el-upload
                class="avatar-uploader"
                :action='$api.file.image.url'
                :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 } from 'vuex'

export default {
  name: '',
  data () {
    return {
      postData: {
        AreaId: '', // 区域id
        CaseId: '', // 案场id
        OrgId: '', // 机构id
        AreaName: '', // 区域名称
        AreaIcon: '', // 区域图标
        AreaIconWhite: '', // 区域图标灰白
      },
      caseList: [{
        value: '0',
        label: '案场1'
      }, {
        value: '1',
        label: '案场3'
      }, {
        value: '2',
        label: '案场3'
      }],
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    CaseId: {
      get () {
        return this.postData.CaseId || this.defaultCaseId
      },
      set (val) {
        this.postData.CaseId = val
      }
    }
  },
  components: {},
  methods: {
    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.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>
