<!--气瓶使用-->
<template>
  <div class="app-container approval_handle">
    <div class="approval_handle-page">
      <template>
        <el-tabs v-model="activeName"  @tab-click="handleTabClick">
          <el-tab-pane label="领用气瓶" name="1"></el-tab-pane>
          <el-tab-pane label="归还气瓶" name="2" v-if="returnGasList[0]"></el-tab-pane>
        </el-tabs>
      </template>
      <el-table border v-loading="loading" :data="tableData">
        <el-table-column label="识别码" align="left" prop="electronicTag"/>
        <el-table-column label="气瓶编号" align="left" prop="airNumber"></el-table-column>
        <el-table-column label="气体名称" align="left" prop="airName"></el-table-column>
        <el-table-column label="气瓶规格" align="left" prop="configName"></el-table-column>
        <el-table-column label="当前气压" align="left" prop="currentPressure">
          <template slot-scope="scope">
            <span>{{scope.row.currentPressure}}Mpa</span>
          </template>
        </el-table-column>
        <el-table-column label="领用时间" align="left" prop="receivingTime" v-if="activeName==2"></el-table-column>
        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="120">
          <template slot-scope="scope">
            <div class="table-button-box">
              <p class="table-button-null"></p>
              <p class="table-button-p"
                 v-if="activeName==1"
                 v-hasPermiRouter="['bottle:useRecordAdmin:add']"
                 @click="handleClick('',scope.row,'receive')"
              >领用</p>
              <p class="table-button-p"
                 v-if="activeName==2"
                 v-hasPermiRouter="['bottle:useRecordAdmin:edit']"
                 @click="handleClick('',scope.row,'give')"
              >归还</p>
              <p class="table-button-null"></p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-sizes="[20, 30, 40, 50]"
                  :total="total"
                  layout="total, prev, pager, next, sizes, jumper"
                  :page.sync="queryParams.page"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList"
      />
    </div>
    <!--    弹窗-->
    <el-dialog :title=dialogTitle :visible.sync="dialogVisible" width="500px" append-to-body
               :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div style="margin-bottom: 20px">{{dialogContent}}</div>
        <el-input v-show="activeName==1"  v-model="form.beforeUse" placeholder="请输入气表实际压力，精确到小数点后一位" />
        <el-input v-show="activeName==2" v-model="form.afterUse" placeholder="请输入气表实际压力，精确到小数点后一位" />
      </el-form>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="cancel">取消</p>
        <p class="dialog-footer-button-primary" @click="submitForm">{{subName}}</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mangerVoice } from '@/api/commonality/noPermission'
import { getToken } from "@/utils/auth";
import {
  gasApplyList,
  receivingGasList,
  returnGas,
  returnRecord,
  useRecord
} from '@/api/gasManage_syd/index'
export default {
  name: "Approval",
  data() {
    return {
      activeName:'1',
      dialogTitle:'领用气瓶提示',
      dialogContent:'您在领用气瓶前需要确认气表压力与系统显示的压力值是否一致，如不一致则需要在下方填写实际气压值。',
      subName:'领用',
      dialogVisible:false,
      //页面状态
      pageType:1,
      loading:false,
      headers: {
        Authorization: "Bearer " + getToken()
      },
      // 查询参数
      queryParams: {
        page: 1,
        pageSize:20,
      },
      total:0,
      tableData:[{}],
      dateRange:[],
      pageData2:{},
      form:{
        beforeUse:'',//领用
        afterUse:'',//归还
      },
      currentPressure:'',
      rules:{
        txt:[
          {required: true, message: '请输入播放文字', trigger: 'blur'},
          { required: true, message: "请输入播放文字", validator: this.spaceJudgment, trigger: "blur" }
        ],
      },
      returnGasList:[],
    };


  },
  methods: {
    handleTabClick(tab, event) {
      this.getList()
    },
    handleClick(index,row,doType){
      let _this=this;
      if(doType=='receive'){//领用

        this.dialogTitle='领用气瓶提示';
        this.dialogContent='您在领用气瓶前需要确认气表压力与系统显示的压力值是否一致，如不一致则需要在下方填写实际气压值。';
        this.subName='领用';
        this.dialogVisible=true;


        this.form.bottleName=row.airName;
        this.form.isSurplus=1;
        this.form.subjectName=row.subjectName;
        this.form.subjectId=row.subjectId;
        this.form.storageId=row.id;

        this.form.beforeUse='';
        this.currentPressure=row.currentPressure;

      }else if(doType=='give'){//归还
        this.dialogTitle='归还气瓶提示';
        this.dialogContent='请输入当前气压值。';
        this.subName='归还';
        this.dialogVisible=true;
        _this.form.storageId=row.id
        _this.form.afterUse='';
        /* this.$confirm('请确认已将气瓶放置在气瓶存放点，您是否确认归还?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          returnRecord(this.form).then( response => {
            let res=response.data;

          });
        }).catch(() => {}); */
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.form = {
        beforeUse:'',//领用
        afterUse:'',//归还
      };
      this.dialogVisible = false;
    },
    /*领用提交*/
    submitForm(){
      let _this=this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(_this.activeName==1){//领用
            if(!this.form.beforeUse){
              this.form.beforeUse=_this.currentPressure
            }
            useRecord(this.form).then( response => {
              let res=response.data;
              if(response.code==200){
                _this.dialogVisible=false;
                _this.getList()
              }
            });
          }else if(_this.activeName==2){//归还
            returnRecord(this.form).then( response => {
              let res=response.data;
              if(response.code==200){
                _this.dialogVisible=false;
                _this.getList()
              }
            });
          }

        }
      });
    },
    getList(){
      let _this=this;
      if(_this.activeName==1){//领用
        receivingGasList(_this.queryParams).then( response => {
          let res=response.rows;
          _this.tableData=res;
          _this.total=response.total;
        });
      }else if(_this.activeName==2){//归还
        returnGas(_this.queryParams).then( response => {
          let res=response.rows;
          _this.tableData=res;
          _this.total=response.total;
        });
      }
      this.geTreturnGas();
    },
    geTreturnGas(){
      returnGas({page: 1, pageSize:20,}).then( response => {
        this.$set(this,'returnGasList',response.rows);
      });
    },
  },
  mounted() {
    this.getList()
  }
};
</script>

<style scoped lang="scss">
.approval_handle {
  display: flex!important;
  flex-direction: column;
  .approval_handle-page{
    flex:1;
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px 20px 20px!important;
    border-radius:10px;

    .button-box{
      width:200px;
      display: flex;
    }
  }

}
</style>
