<template>
  <el-dialog title="设置" :visible.sync="visibility" :close-on-click-modal="isClose"
             :close-on-press-escape="isClose">
    <div style="text-align: center;">

      <div>
        <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;">
          启用“药品管理”后，在开透析医嘱时，医生可以选择药品后将自动产生出库单</p></div>
      </div>
    </div>

    <span slot="footer" class="dialog-footer">
    <el-button @click="hide()">取 消</el-button>
  </span>
  </el-dialog>

</template>

<script>
  import { createDrugStockAutomaticReduceConfig, getDrugStockAutomaticReduceConfig } from '@/api/drug/drug_stock'

  export default {
    name: 'settingDialog',
    data() {
      return {
        is_open: false,
        visibility: false,
        isClose: false
      }
    },
    props: {
      propForm: {
        type: Object
      }
    },
    methods: {
      hide: function() {
        this.visibility = false
      },
      show: function() {
        this.getConfig()
        this.visibility = true

      }, changeOpen(val) {
        var message = ''
        if (val == 1) {
          message = '启用药品自动扣减功能'
        } else {
          message = '关闭药品自动扣减功能'
        }

        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => {
          createDrugStockAutomaticReduceConfig(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() {
        getDrugStockAutomaticReduceConfig().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
            }
          }
        })
      }
    }

  }

</script>

<style scoped>

</style>
