<template>
  <div class=" sign-and-weigh-box" style="background: white">
    <div style="text-align: center;margin-top: 150px">
      <el-button type="primary"   v-if="!is_open" @click="changeOpen(1)">启用自动扣减</el-button>
      <el-button type="danger"   v-if="is_open"  @click="changeOpen(2)">关闭自动扣减</el-button>
      <div style="margin-top: 40px;" ><p style="color:#909399;text-align: center;">开启后，系统设定的固定商品类型，在透析记录-透析处方-透析耗材中设置好，患者执行上机之后，将会自动产生出库单；<br/>系统设定之外的商品类型则需要手动新增出库单。</p></div>
    </div>
  </div>
</template>

<script>
  import { createStockAutomaticReduceConfig, getAutomaticReduceConfig } from '@/api/config'

  export default {
    name: 'automaticReduce',
    data() {
      return {
        is_open: false
      }
    },
    methods: {
      changeOpen(val){
        var message = ''
        if(val == 1){
          message = '启用库存自动扣减功能'
        }else{
          message = '关闭库存自动扣减功能'
        }

        this.$confirm(message , '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => {
          createStockAutomaticReduceConfig(val).then(response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg)
              return false
            } else {
              if (response.data.data.is_open == 1){
                this.is_open = true
                this.$message({
                  type: 'success',
                  message: '已启用'
                });
              }else{
                this.is_open = false
                this.$message({
                  type: 'success',
                  message: '已关闭'
                });
              }
            }
          })
        }).catch(() => {

        });

      },
      getConfig() {
        getAutomaticReduceConfig().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            var config = response.data.data.config
            if (config.is_open == 1) {
              this.is_open = true
            } else {
              this.is_open = false
            }
          }
        })
      }
    },
    created() {
      this.getConfig()
    }
  }
</script>


<style>
  .sign-and-weigh-box .sign-and-weigh-box-patients .cell {
    font-size: 12px;
  }

  .sign-and-weigh-box .sign-and-weigh-box-patients .current-row > td {
    background: #6fb5fa;
  }

</style>
