<template>
  <div class="app-container">
    <el-form ref="createTemp" :rules="rules" :model="createTemp" label-position="right" label-width="250px" style="margin-top: 40px;margin-bottom:30px;">

      <el-row>
        <el-col :span="12">
          <el-form-item label="可删除记录天数：" prop="delDays">
            <el-input ref="delDays" v-model="createTemp.delDays" :disabled="isRoleEdit==false" class="filter-item" placeholder="请输入可删除记录天数" type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="可编辑记录天数：" prop="updDays">
            <el-input ref="updDays" v-model="createTemp.updDays" :disabled="isRoleEdit==false" class="filter-item" placeholder="请输入可删除记录天数" type="text" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer" style="bottom: 0;">
      <el-button v-if="isRoleEdit" class="success" :disabled="isokDisable" @click="handleSave">保存</el-button>
      <el-button v-if="isRoleEdit" class="warning" :disabled="isokDisable" @click="handleRecovery">恢复默认</el-button>
    </div>
  </div>

</template>

<script>
import { GetDataByName, PostDataByName, failproccess, checkButtons, calculativeWidth, ExecDataByConfig } from '@/api/common'
import Cookies from 'js-cookie'
import { MessageBox } from 'element-ui'
export default {
  name: 'DefaultParameter',
  data() {
    return {
      isRoleEdit: [],

      getdataListParm: {
        name: 'getParam',
        page: 1,
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {
          pastureid: Cookies.get('pastureid')
        }
      },
      tableKey: 0,
      listLoading: true,
      createTemp: {},
      rules: {},
      isokDisable: false,
      requestParam: {}

    }
  },
  created() {
    this.get_table_data()
    this.get_auto_buttons()
  },

  methods: {
    get_auto_buttons() {
      const Edit = 'DefaultParameter'
      const isRoleEdit = checkButtons(JSON.parse(sessionStorage.getItem('buttons')), Edit)
      this.isRoleEdit = isRoleEdit
    },
    get_table_data() {
      this.listLoading = true
      GetDataByName(this.getdataListParm).then(response => {
        console.log('table1数据', response.data.list)
        if (response.data.list !== null) {
          this.createTemp = response.data.list[0]
        } else {
          this.list = []
        }
        setTimeout(() => {
          this.listLoading = false
        }, 100)
      })
    },
    handleSave() {
      console.log('点击了保存')
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)

      var send_data = {
        "name": "updateParam",
        "parammaps": this.createTemp
      }
      PostDataByName(send_data).then(response => {
        if (response.msg === 'fail') {
          this.$notify({ title: '失败', message: response.data, type: 'warning', duration: 2000 })
        } else {
          this.$notify({ title: '成功', message: '成功', type: 'success', duration: 2000 })
          this.get_table_data()
        }
      })


      // ExecDataByConfig(this.requestParam).then(response => {
      //   console.log('保存发送参数', this.requestParam)
      //   if (response.msg !== 'fail') {
      //     this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
      //     this.get_table_data()
      //   } else {
      //     failproccess(response, this.$notify)
      //   }
      // })
    },
    handleRecovery() {
      console.log('点击了恢复默认')
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      MessageBox.confirm('是否确认恢复默认设置?', {
        confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
      }).then(() => {

        var send_data = {
          "name": "updateParam",
          "parammaps": { delDays: "7", id: this.createTemp.id, updDays: "7" }

        }
        PostDataByName(send_data).then(response => {
          if (response.msg === 'fail') {
            this.$notify({ title: '失败', message: response.data, type: 'warning', duration: 2000 })
          } else {
            this.$notify({ title: '成功', message: '成功', type: 'success', duration: 2000 })
            this.get_table_data()
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
