<template>
  <div class="testdiv">
    <div class="divborder newTableDiv" style="margin-top:0px;">
      <template>
          <Table class="tableClass" :columns="tableColumns1" :data="tableData1" v-show="show">
              <template slot-scope="{ row, index }" slot="xuhao">
                  <span>{{ row.xuhao }}</span>
              </template>
              <template slot-scope="{ row, index }" slot="rule">
                  <span>{{ row.rule }}</span>
              </template>
              <template slot-scope="{ row, index }" slot="k_value">
                <span><InputNumber class="publicinput" type="text" v-model="tableData1[index].k_value"/></span>
              </template>
              <template slot-scope="{ row, index }" slot="continuous">
                <span><Checkbox style="text-align:left;zoom:200%;" v-model="tableData1[index].continuous"></Checkbox></span>
              </template>
              <template slot-scope="{ row, index }" slot="discrete">
                <span><Checkbox style="text-align:left;zoom:200%;" v-model="tableData1[index].discrete"></Checkbox></span>
              </template>
              <template slot-scope="{ row, index }" slot="trigger_andon">
                <span><Checkbox style="text-align:left;zoom:200%;" v-model="tableData1[index].trigger_andon"></Checkbox></span>
              </template>
          </Table>
      </template>
      <div class="buttonall">
        <!--<Button type="primary" size="large" @click="cancle">刷新</Button>-->
        <Button type="primary" size="large" @click="save" style="float:right;margin:20px;">{{$t('900306')}}</Button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '@/libs/api.request'

export default{
    data () {
        return {
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            tableData1: [],
            page: 1,
            pagesize: 10,
            tableColumns1: [
                {
                    type: 'index2',
                    title: 'No.',
                    key: 'xuhao',
                    slot: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    },
                    width: 90
                },
                {
                    title: this.$t('1950526'),
                    slot: 'rule',
                    key: 'rule'
                },
                {
                    title: this.$t('1950527'),
                    slot: 'k_value',
                    key: 'k_value'
                },
                {
                    title: this.$t('1950528'),
                    slot: 'continuous',
                    key: 'continuous'
                },
                {
                    title: this.$t('1950529'),
                    slot: 'discrete',
                    key: 'discrete'
                },
                {
                    title: this.$t('1950530'),
                    slot: 'trigger_andon',
                    key: 'trigger_andon'
                }
            ],
            show: true
        }
    },
    created () {
        this.search()
    },
    methods: {
        search (page, pagesize) {
            var data = this.tableData1
            axios
                .request({
                    url: '/qms/AbnormalRule/GetRules',
                    data,
                    method: 'get'
                })
                .then(res => {
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        id: item.id,
                        rule: item.rule,
                        k_value: item.k_value,
                        continuous: item.continuous === 1,
                        discrete: item.discrete === 1,
                        trigger_andon: item.trigger_andon === 1
                    })
                }
            })
            this.tableData1 = data
        },
        cancle () {
            this.search()
            this.show = false
            this.show = true
        },
        save () {
            var params = []
            this.tableData1.forEach(ele => {
                var model = {}
                model.continuous = ele.continuous === true ? 1 : 0
                model.discrete = ele.discrete === true ? 1 : 0
                model.trigger_andon = ele.trigger_andon === true ? 1 : 0
                model.k_value = ele.k_value
                model.id = ele.id
                params.push(model)
            })
            var data = { rules: params }
            axios
                .request({
                    url: '/qms/AbnormalRule/SaveRules',
                    data,
                    method: 'post'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        this.$Message.success(this.$t('800223'))
                        this.search()
                        this.show = false
                        this.show = true
                    } else {
                        this.search()
                        this.show = false
                        this.show = true
                        this.$Message.error(this.$t(res.data.msg))
                    }
                })
        }
    }
}

</script>
<style lang="less" scoped>

</style>
