<template>
  <view>
    <cu-custom bgColor="bg-gradual-blueness" :isBack="true">
      <block slot="backText">返回</block>
      <block slot="content">气体分析采样</block>
    </cu-custom>
    <view style="text-align: right;">
      <button class="cu-btn bg-blue margin-tb-sm sm" style="margin-right: 20upx" @click="addModalShow" v-if="activeType==='add'">添加</button>
      <button class="cu-btn bg-blue margin-tb-sm sm" style="margin-right: 20upx" v-if="activeType==='add' && APPLY_STATUS == '1'" @click="submitQualified">初采合格</button>
    </view>
    <view class="form" v-if="list.length>0">
      <view class="wui-form-list" v-for="(item,index) in list" :key="index">
        <view class="cu-form-group">
          <view class="title">取样时间：</view>
          {{ item.TIME }}
        </view>
        <view class="cu-form-group">
          <view class="title">取样部位：</view>
          {{ item.SAMPLING }}
        </view>
        <view class="cu-form-group">
          <view class="title">分析人：</view>
          {{ item.ANALYST_NAME }}
        </view>
        <view class="cu-form-group">
          <view class="title">有毒有害物质：</view>
          {{ item.SUBSTANCE }}
        </view>
        <view class="cu-form-group">
          <view class="title">可燃气：</view>
          {{ item.COMBUSTIBLE }}
        </view>
        <view class="cu-form-group">
          <view class="title">含氧量：</view>
          {{ item.OXYGEN }}
        </view>
      </view>
      <view class="cu-bar btn-group" style="margin-top: 30upx;" v-show="list.length !== 0">
        <button class="cu-btn bg-grey margin-tb-sm lg" @click="$noMultipleClicks(goback)">返回</button>
      </view>
    </view>
    <view v-else-if="dataFlag=='noData'" class="dy-null">
      <view class="dy-null-img">
        <image :src="background[dataFlag].url" mode=""></image>
      </view>
      <view class="dy-null-title">
        {{background[dataFlag].msg}}
      </view>
    </view>
    <view class="cu-tabbar-height"></view>
    <view class="padding flex flex-direction"></view>
    <view :class="['cu-modal',{'show':addModal}]">
      <view class="cu-dialog">
        <view class="cu-bar bg-white justify-end">
          <view class="content">气体分析采样添加</view>
          <view class="action" @tap="addModal = false">
            <text class="cuIcon-close text-red"></text>
          </view>
        </view>
        <view>
          <view class="wrapper">
            <view class="handCenter">
              <view class="form">
                <view class="wui-form-list">
                  <view class="cu-form-group">
                    <view class="title">取样时间：</view>
                    <ruiDatePicker v-if="addModal" :start="todayDate" fields="minute" :value="addForm.TIME || '请选择'" @change="changeApplicationDate"></ruiDatePicker>
                  </view>
                  <view class="cu-form-group">
                    <view class="title">取样部位：</view>
                    <input v-model="addForm.SAMPLING" placeholder="请输入取样部位"></input>
                  </view>
                  <view class="cu-form-group">
                    <view class="title">分析人：</view>
                    <input :disabled="true" v-model="addForm.ANALYST_NAME" placeholder="请输入分析人"></input>
                  </view>
                  <view class="cu-form-group">
                    <view class="title">有毒有害物质：</view>
                    <input v-model="addForm.SUBSTANCE" placeholder="请输入有毒有害物质"></input>
                  </view>
                  <view class="cu-form-group">
                    <view class="title">可燃气：</view>
                    <input v-model="addForm.COMBUSTIBLE" placeholder="请输入可燃气"></input>
                  </view>
                  <view class="cu-form-group">
                    <view class="title">含氧量：</view>
                    <input v-model="addForm.OXYGEN" placeholder="请输入含氧量"></input>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
        <view class="cu-bar bg-white justify-end">
          <view class="action">
            <button class="cu-btn bg-green margin-left" @click="$noMultipleClicks(confirmAdd)">提交</button>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import {
  basePath,
  loginUser,
  loginSession,
  formatDate,
  corpinfoId,
  loginUserId,
} from '@/common/tool.js';
import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue';

export default {
  components: {
    ruiDatePicker,
  },
  data() {
    return {
      noClick: true,
      dataFlag: 'noData',
      list: [],
      addModal:false,
      addForm:{
        TIME: formatDate(new Date(), 'yyyy-MM-dd hh:mm'),
        SAMPLING: '',
        ANALYST_NAME: '',
        ANALYST_ID: '',
        SUBSTANCE: '',
        COMBUSTIBLE: '',
        OXYGEN: '',
      },
      rules: [
        {name: 'TIME', message: '请选择取样时间'},
        {name: 'SAMPLING', message: '请输入取样部位'},
        {name: 'ANALYST_NAME', message: '请输入分析人'},
        {name: 'SUBSTANCE', message: '请输入有毒有害物质'},
        {name: 'COMBUSTIBLE', message: '请输入可燃气'},
        {name: 'OXYGEN', message: '请输入含氧量'},
      ],
      todayDate: formatDate(new Date(), 'yyyy-MM-dd hh:mm'),
      CONFINEDSPACE_ID: '',
      activeType: '',
      APPLY_STATUS: '',
    }
  },
  onLoad(event) {
    this.background = require('@/common/background.json');
    this.CONFINEDSPACE_ID = event.id
    this.activeType = event.type
    this.getData();
    this.addForm.ANALYST_NAME = loginUser.NAME;
    this.addForm.ANALYST_ID = loginUserId;
    loginSession();
  },
  methods: {
    getData() {
      var _this = this;
      uni.showLoading({
        title: '请稍候'
      })
      uni.request({
        url: basePath + '/app/confinedspace/gas/list?showCount=1&currentPage=999',
        method: 'POST',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          CONFINEDSPACE_ID: _this.CONFINEDSPACE_ID,
        },
        success: (res) => {
          if ("success" === res.data.result) {
            this.list = res.data.varList
            this.APPLY_STATUS = res.data.APPLY_STATUS
            uni.hideLoading();
          } else if ("exception" === res.data.result) {
            uni.showToast({
              title: '错误',
              duration: 2000
            });
          }
        }
      });
    },
    addModalShow(){
      this.addModal = true;
      this.addForm = {
        TIME: formatDate(new Date(), 'yyyy-MM-dd hh:mm'),
        SAMPLING: '',
        ANALYST_NAME: loginUser.NAME,
        ANALYST_ID: loginUserId,
        SUBSTANCE: '',
        COMBUSTIBLE: '',
        OXYGEN: '',
      }
    },
    changeApplicationDate(e){
      this.addForm.TIME = e
    },
    confirmAdd(){
      let required = true
      this.rules.map(({name, message}) => {
        if (!this.addForm[name]) {
          uni.showToast({
            icon: 'none',
            title: message,
            duration: 1500
          });
          required = false
        }
      })
      if (!required) {
        return
      }
      uni.showLoading({
        title: '请稍候'
      })
      uni.request({
        url: basePath + "/app/confinedspace/gas/save",
        method: 'POST',
        dataType: 'json',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          ...this.addForm,
          CONFINEDSPACE_ID: this.CONFINEDSPACE_ID,
        },
        success: (res) => {
          uni.showToast({
            icon: 'none',
            title: '保存成功',
            duration: 2000
          });
          this.addModal = false
          this.getData()
        },
        fail: (err) => {
          uni.hideLoading();
          uni.showModal({
            content: err.errMsg,
            showCancel: false
          });
        }
      })
    },
    submitQualified(){
      uni.showLoading({
        title: '请稍候'
      })
      uni.request({
        url: basePath + "/app/confinedspace/editStatus",
        method: 'POST',
        dataType: 'json',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          CONFINEDSPACE_ID: this.CONFINEDSPACE_ID,
          VECTORY: '1',
          APPLY_STATUS: '1',
          ACTION_USER:loginUser.NAME,
          loginUserId: loginUserId
        },
        success: (res) => {
          uni.showToast({
            icon: 'none',
            title: '保存成功',
            duration: 2000
          });
          this.goback()
        },
        fail: (err) => {
          uni.hideLoading();
          uni.showModal({
            content: err.errMsg,
            showCancel: false
          });
        }
      })
    },
    goback() {
      uni.navigateBack({
        delta: 1
      });
      uni.hideLoading();
    },
  }
}
</script>

<style>
.cu-modal{
  z-index: 0;
}
</style>
