<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
                maxlength="5"
                >
              </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: '', // 区域图标灰白
      },
    }
  },
  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
      }
    }
  },
  created () {
    this.getCaseAreaInfo()
  },
  components: {},
  methods: {
    getCaseAreaInfo () { // 获取区域信息
      this.$ajax(this.$api.caseManager.getCaseAreaInfo.url, {
        method: this.$api.caseManager.getCaseAreaInfo.method,
        urlData: { id: this.$route.query.id }
      }).then(res => {
        this.postData = res
      })
    },
    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.$ajax(this.$api.caseManager.editCaseArea.url, {
        method: this.$api.caseManager.editCaseArea.method,
        data: { ...this.postData },
      }).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>
