<template>
  <view class="container">
    <uni-section type="line" title="基础信息选择"> </uni-section>
    <uni-forms label-position="top">
      <uni-forms-item label="区域" name="areaCode">
        <uni-data-select
          :clear="false"
          v-model="areaCode"
          :localdata="areaCodeList"
          @change="handleAreaChange"
        ></uni-data-select>
      </uni-forms-item>
      <uni-forms-item v-if="areaCode" label="岗位" name="postCode">
        <uni-data-select
          :clear="false"
          v-model="postCode"
          :localdata="postCodeList"
          @change="handlePostChange"
        ></uni-data-select>
      </uni-forms-item>
      <uni-forms-item v-if="postCode && postCode === weighingPostCode" label="计量点" name="scaleNo">
        <uni-data-select :clear="false" v-model="scaleNo" :localdata="scaleNoList"></uni-data-select>
      </uni-forms-item>
    </uni-forms>
    <u-button type="primary" @click="submitForm">保存</u-button>
    <u-loading-page :loading="loadingPage"></u-loading-page>
  </view>
</template>

<script>
// import { getAreaCodeConfig, getScaleNoConfig, getPostList } from '@/nxTemp/apis/sys.api'
export default {
  data() {
    return {
      weighingPostCode: '监磅',
      areaCode: '',
      scaleNo: '',
      postCode: '',
      areaCodeList: [],
      scaleNoList: [],
      postCodeList: [],
      loadingPage: false
    }
  },
  async onLoad() {
    this.loadingPage = true
    try {
      this.areaCodeList = (await getAreaCodeConfig()).map(item => ({ text: item.areaName, value: item.areaCode }))
      this.postCodeList = (await getPostList()).map(item => ({ text: item.name, value: item.name }))
      if (this.areaCode) {
        this.getScaleNoList()
      }
      this.loadingPage = false
    } catch (error) {
      this.loadingPage = false
    }
  },
  mounted() {
    this.areaCode = this.$store.state.areaCode
    this.scaleNo = this.$store.state.scaleNo
    this.postCode = this.$store.state.postCode
  },
  methods: {
    async getScaleNoList() {
      if (this.postCode && this.postCode === this.weighingPostCode) {
        // this.scaleNo = ''
        this.scaleNoList = (await getScaleNoConfig({ areaCode: this.areaCode })).map(item => ({
          text: item.pointName,
          value: item.pointCode
        }))
      }
    },
    async handleAreaChange() {
      this.postCode = ''
      this.scaleNo = ''
    },
    async handlePostChange() {
      this.scaleNo = ''
      this.getScaleNoList()
    },
    async goIndex() {
      await this.$store.dispatch('getRoleMenus', this.$store.state.postCode)
      uni.reLaunch({
        url: 'pages/lims/index/index'
      })
    },
    submitForm() {
      if (this.areaCode && this.postCode && this.postCode !== this.weighingPostCode) {
        this.$store.commit('SET_AREA_CODE', this.areaCode)
        this.$store.commit('SET_POST_CODE', this.postCode)
        this.goIndex()
      } else if (this.postCode && this.postCode === this.weighingPostCode) {
        if (this.scaleNo) {
          this.$store.commit('SET_AREA_CODE', this.areaCode)
          this.$store.commit('SET_POST_CODE', this.postCode)
          this.$store.commit('SET_SCALE_NO', this.scaleNo)
          this.goIndex()
        } else {
          uni.showToast({
            title: '请选择',
            icon: 'none'
          })
        }
      } else {
        uni.showToast({
          title: '请选择',
          icon: 'none'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 30rpx;
}
::v-deep .uni-section-header__decoration {
  background-color: $uni-color-primary !important;
}
</style>
