<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.InfoName"
                clearable>
              </el-input>
            </div>
          </div>
        </li>
        <li class="flex-h">
          <span>跳转链接：<em>*</em></span>
          <div class="flex-item">
            <div style="width:50%">
              <el-input
                placeholder="请输入链接"
                v-model="postData.InfoUrl"
                clearable>
              </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="postData.CaseId" placeholder="请选择">
                <el-option
                  v-for="item in caseList"
                  :key="item.CaseId"
                  :label="item.CaseName"
                  :value="item.CaseId">
                </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, createNamespacedHelpers } from 'vuex'
const { mapActions: mapCmsActions } = createNamespacedHelpers('cms')
export default {
  name: '',
  data () {
    return {
      ajaxOff: true,
      postData: {
        InfoName: '',
        InfoUrl: '',
        OrgId: '',
        CaseId: '',
        LocationId: 'index'
      }
    }
  },
  components: {},
  created () {
    this.updateLocationInfo()
    this.getDetail()
  },
  computed: {
    ...mapState({
      // positionList: x => x.cms.location,
      OrgId: x => x.app.user.OrgId,
      caseList: x => x.app.cases.list
    })
  },
  methods: {
    ...mapCmsActions(['updateLocationInfo']),
    submit () {
      if (this.ajaxOff) {
        if (this.postData.InfoName === '') {
          this.$message({
            message: '标题不能为空',
            type: 'error',
          })
          return false
        }
        if (this.postData.InfoUrl === '') {
          this.$message({
            message: '链接地址不能为空',
            type: 'error',
          })
          return false
        }
        // var reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/  // eslint-disable-line
        // if (!reg.test(this.postData.InfoUrl)) {
        //   this.$message({
        //     message: '链接地址格式不正确',
        //     type: 'error',
        //   })
        //   return false
        // }
        var strRegex = /^(ftp|https?)\:\/\/([\w\_\-]+)\.([\w\-]+[\.]?)*[\w]+\.[a-zA-Z]{2,10}(.*)/  // eslint-disable-line
        if (this.postData.InfoUrl.match(strRegex) === null) {
          this.$message({
            message: '链接地址格式不正确',
            type: 'error',
          })
          return false
        }
        this.postData.OrgId = this.OrgId
        this.$ajax(this.$api.cms.editInfo.url, {
          method: this.$api.cms.editInfo.method,
          urlData: {
            id: this.$route.query.id
          },
          data: this.postData
        }).then(res => {
          this.ajaxOff = true
          this.$message({
            message: '编辑成功',
            type: 'success',
            duration: 1000
          })
          this.$router.push({ name: 'indexMsg' })
        }).catch(msg => {

        })
      }
    },
    cancel () {
      this.$router.go(-1)
    },
    getDetail () {
      this.$ajax(this.$api.cms.infoDetail.url, {
        method: this.$api.cms.infoDetail.method,
        urlData: {
          id: this.$route.query.id
        }
      }).then(res => {
        console.log(res)
        // this.postData.InfoName = res.InfoName
        // this.postData.InfoUrl = res.InfoUrl
        // this.postData.CaseId = res.CaseId
        this.postData = res
      }).catch(msg => {

      })
    }
  }
}
</script>

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