<template>
  <div id="unit" class="flex-row">
    <div class="flex basic-main" style="max-width:65%;">
      <param-set-default-list v-ref:defaultlist></param-set-default-list>
    </div>
    <div class="flex binary-right" style="flex:1; ">
      <work-busy :is-busy="seaching"></work-busy>
      <div class="row form-horizontal select-overspread container-fluid auto" partial>
        <div novalidate class="form-inline auto" partial>
          <div class="col-sm-8">
            <label class="font_normal_body">选择要设置参数的表具</label>
            <v-select
              placeholder='气表品牌'
              style="width:60%"
              :value.sync="f_gasbrand_id"
              :value-single="true"
              v-model="f_gasbrand_id"
              :options='meterbrand'
              close-on-select></v-select>
          </div>
        </div>
      </div>
      <div id="orglist">
        <tree-grid class=" list_area table_sy" style="overflow: auto" v-ref:list :model='model'>
          <template partial='head'>
            <tr>
              <th>参数名称</th>
            </tr>
          </template>
          <template partial='body'>
            <td>
              <tree-grid-item :row="row" iconsize="15px">
              <span partial>
                {{ row.data.name }}
                 <span style="float: right">
                   {{row.data.default_value ? `当前默认值为：${row.data.default_value}` : ''}}
                  <button class="button_spacing button_clear" v-if="!row.data.children"
                          @click.stop="$parent.$parent.modifyRes(row.data,row.parent.data.name)">修改默认值</button>
                </span>
              </span>
              </tree-grid-item>
            </td>
          </template>
        </tree-grid>
      </div>
    </div>
    <modal :show.sync="showDefault" title="修改默认值" v-ref:modal  backdrop="false" @modal-close="setClose">
      <article slot="modal-body" class="modal-body">
        <div class="col-sm-12" style="height: auto">
          <div class="col-sm-4">
            <label class="font_normal_body" style="text-align:right;float:right">{{ row.name }}：</label>
          </div>
          <div class="col-sm-8" v-if="row.type!=='option'">
            <input :type="row.type" v-model="paramSetVal" :value.sync="paramSetVal" style="width:70%" placeholder=""
                   class="input_search">
          </div>
          <div class="col-sm-8" v-if="row.type==='option'">
            <v-select
              :placeholder='row.name'
              :value.sync="paramSetVal"
              style="width:70%"
              width="70%"
              :value-single="true"
              v-model="paramSetVal"
              :options='row.params'
              close-on-select
            >
            </v-select>
          </div>
        </div>
        <center>
          <span> {{ row.remark }}</span>
        </center>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-success" @click='setConfirm' :disabled="!paramSetVal">确认</button>
        <button type="button" class="btn btn-default" @click='setClose'>取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>
import ParamSetDefaultList from './ParamSetDefaultList'

let handleTree = function (data, defaultData) {
  return data.map(item => {
    let children = item.params.map(c => {
      for (let i = 0; i < defaultData.length; i++) {
        if (c.name === defaultData[i].f_param_name && item.name === defaultData[i].f_group_name && defaultData[i].f_param_title === c.title) {
          c.default_value = defaultData[i].f_param_value
          c.default_id = defaultData[i].default_id
          break
        }
      }
      delete c['size']
      return c
    })
    return {label: item.name, name: item.name, value: item.name, children: children}
  })
}
let initGen = async function (self) {
  await self.$LoadParams.loadParam()
  let res = self.$GetSaleParam.getGasbrand()
  let arr = []
  res.forEach((item) => {
    if (item.value.f_meter_type === '物联网表') {
      let temp = {}
      temp.label = item.label
      temp.value = item.value.id
      arr.push(temp)
    }
  })
  self.meterbrand = arr
}
export default {
  title: '参数默认值设置',
  data () {
    return {
      model: {},
      search_str: '',
      seaching: false,
      meterbrand: [],
      quanxian: this.$login.r,
      f_gasbrand_id: '',
      showDefault: '',
      row: {},
      groupName: '',
      paramSetVal: ''
    }
  },
  ready () {
    initGen(this)
  },
  methods: {
    setConfirm () {
      let data = {
        f_gasbrand_id: this.f_gasbrand_id,
        f_group_name: this.groupName,
        f_param_name: this.row.name,
        f_param_title: this.row.title,
        f_new_value: this.paramSetVal,
        f_param_value: this.paramSetVal,
        f_old_value: this.row.default_value,
        default_id: this.row.default_id,
        f_describe: `${this.$login.f.name}将 ${this.row.name} 默认值设置为 ${this.paramSetVal}`,
        f_operator: this.$login.f.name,
        f_operatorid: this.$login.f.id,
        f_orgid: this.$login.f.orgid,
        f_orgname: this.$login.f.orgs,
        f_depid: this.$login.f.depids,
        f_depname: this.$login.f.deps
      }
      this.$resetpost('/rs/logic/setDefaultValue', data, {
        resolveMsg: null,
        rejectMsg: null
      }).then(() => {
        this.searchData()
        this.setClose()
      })
    },
    setClose () {
      this.row = {}
      this.paramSetVal = ''
      this.groupName = ''
      this.showDefault = false
    },
    async searchData () {
      this.seaching = true
      this.$resetpost('webmeter/rs/logic/getParamsByGasBrand', {data: {gasBrandId: this.f_gasbrand_id}}, {
        resolveMsg: null,
        rejectMsg: null
      }).then(async ({data}) => {
        let allDefaultValue = await this.$resetpost('/rs/sql/getAllDefaultValues', {data: {gasBrandId: this.f_gasbrand_id}}, {
          resolveMsg: null,
          rejectMsg: null
        })
        this.model = handleTree(data, allDefaultValue.data)
        this.$refs.defaultlist.search()
      })
      this.seaching = false
    },
    // 修改资源
    modifyRes (obj, groupName) {
      this.row = obj
      this.paramSetVal = ''
      this.groupName = groupName
      this.row.remark = `描述信息：${obj.default_value ? `当前默认值为：${obj.default_value}；` : ''} ${obj.max ? `最大值为${obj.max};` : ''}${obj.max ? `最小值为${obj.min};${obj.remark}` : obj.remark}`
      this.showDefault = true
    },
    async confirm () {

    }
  },
  components: {ParamSetDefaultList},
  computed: {},
  watch: {
    'f_gasbrand_id' (newVal) {
      if (newVal) {
        this.searchData()
      }
    }
  },
  events: {}

}
</script>
<style>
#orglist div {
  margin-left: 0;
  color: #000;
}

#orglist {
  overflow: scroll;
}

#orglist span {
  color: #000;
}
</style>

