<template>
  <div class="auto">
    <criteria-paged :model='model' v-ref:paged>
      <criteria :model='model' partial='criteria'>
        <div novalidate class="form-inline col-sm-12" partial>
          <div class="form-group form-input-group col-sm-12">
            <div class="form-group col-sm-6">
              <v-select placeholder='参数类型'
                        :options='$parent.$parent.brand'
                        @change='$parent.$parent.typeSearch'
                        close-on-select>
              </v-select>
              <label>只读:</label><input type="checkbox" v-model="$parent.$parent.isReadOnly" class="checkbox-inline"/>
            </div>
            <div class="form-group col-sm-2">
              <button class="btn btn-success width-100" @click="$parent.$parent.isAddShow()">
                新增参数

              </button>
            </div>
          </div>
          <div class="form-inline col-sm-12" v-if="$parent.$parent.addParam">
            <div class="form-group form-input-group col-sm-2">
              <input class="input_search" style="width: 100%" placeholder='真实参数名' type="text"
                     v-model="$parent.$parent.addParamData.f_param_name">
            </div>
            <div class="form-group form-input-group col-sm-2">
              <input class="input_search" style="width: 100%" placeholder='备注' type="text"
                     v-model="$parent.$parent.addParamData.f_param_message">
            </div>
            <div class="form-group form-input-group col-sm-2">
              <input class="input_search" style="width: 100%" placeholder='参数类型' type="text"
                     v-model="$parent.$parent.addParamData.f_param_type">
            </div>
            <div class="form-group form-input-group col-sm-2">
              <input class="input_search" style="width: 100%" placeholder='参数值' type="text"
                     v-model="$parent.$parent.addParamData.f_param_value">
            </div>
            <div class="form-group form-input-group col-sm-1">
              <button class="btn btn-success width-100" @click="$parent.$parent.dataSubmit()">
                {{$parent.$parent.btnName}}
              </button>
            </div>
            <div class="form-group form-input-group col-sm-1">
              <button class="btn btn-success width-100" @click="$parent.$parent.close()">
                取消{{$parent.$parent.btnName}}
              </button>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>序号</th>
            <th>参数名</th>
            <th>备注</th>
            <th>参数类型</th>
            <th>参数值</th>
            <th>操作</th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center">{{row.id}}</td>
            <td style="text-align:center">{{row.f_param_name}}</td>
            <td style="text-align:center">{{row.f_param_message}}</td>
            <td style="text-align:center">{{row.f_param_type}}</td>
            <td style="text-align:center">{{row.f_param_value}}</td>
            <td style="text-align:center" v-if="row.f_isreadonly=='0'">
              <button class="btn btn-link " @click.stop="$parent.$parent.$parent.del(row.id)"> 删除</button>
              <button class="btn btn-link " @click.stop="$parent.$parent.$parent.isUpdateShow(row)"> 修改</button>
            </td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'

  //组合数据填充类型选择下拉框
  let getherData = (self) => {
    let data_type_array = []
    let data_type = []
    data_type.push({label: '全部', value: ''})
    for (let i = 0; i < self.model.rows.length; i++) {
      if (data_type_array.indexOf(self.model.rows[i].f_param_type) == -1) {
        data_type_array.push(self.model.rows[i].f_param_type)
        data_type.push({label: self.model.rows[i].f_param_type, value: self.model.rows[i].f_param_type})
      }
    }
    return data_type
  }

  let funSearch = async (self) => {
    await self.model.search(self.conditionSQL, self.model).then(() => {
      let bakArray = self.typeModel
      self.typeModel = getherData(self)
      if (bakArray.length > self.typeModel.length)
        self.typeModel = bakArray
    })
  }
  export default {
    title: "参数管理",
    props: ['id', 'type', 'rowData'],
    data() {
      return {
        dataValue: 0,
        dataType: 0,
        model: new PagedList("rs/sql/getExtraParams", 20),
        index: 0,
        typeModel: [],
        addParam: false,
        addParamData: {
          f_param_name: '',
          f_param_message: '',
          f_param_type: '',
          f_param_value: ''
        },
        isReadOnly: false,
        selectValue: {},
        btnName: '新增'
      }
    },
    ready() {
      //不知道怎么回事，Parames无法触发监控指令，所以出此下策
      if (this.$parent.setParames) {
        this.$parent.setParames(this)
      }
      this.dataValue = this.id
      //传入该组件被使用时的值类型 0 为型号 1 为用户表
      this.dataType = this.type
      this.typeModel = [{label: '全部', value: ''}]
      this.addParamData.f_param_name = '默认名字'
    },
    methods: {
      search(condition) {
        this.conditionSQL = condition
        funSearch(this)
      },
      typeSearch(value) {
        this.selectValue = value
        this.isReadOnly = false
        let condition2 = value == '' ? '1=1 AND f_data_type=\'' + this.dataType + '\' AND f_data_id=' + this.dataValue + ' AND f_state=\'有效\''
          : '1=1 AND f_data_type=0 AND f_data_id=' + this.dataValue + ' AND f_param_type = \'' + value + '\' AND f_state=\'有效\''
        this.search(condition2)
      },
      //修改
      modify() {
        this.$showMessage("是否确定修改该参数?", ['confirm', 'cancel']).then((res) => {
          if (res == 'confirm') {
            this.$resetpost('rs/logic/updateExtraParams', {
              data: {
                id: this.addParamData.id,
                //数据识别列
                f_data_id: this.dataValue,
                //数据类型列
                f_data_type: this.dataType,
                f_param_name: this.addParamData.f_param_name,
                f_param_message: this.addParamData.f_param_message,
                f_param_type: this.addParamData.f_param_type,
                f_param_value: this.addParamData.f_param_value
              }
            }, {
              resolveMsg: null,
              rejectMsg: null
            }).then((res) => {
              if (res.data === '成功') {
                this.$showMessage("修改成功！")
                this.search('1=1 AND f_data_type=' + this.dataType + ' AND f_data_id=' + this.dataValue + ' AND f_state=\'有效\'')
                this.clearaddParamData()
                this.close()
              } else if (res === '值不存在') {
                this.$showMessage("修改失败(该参数可能被他人删除!)")
                this.search('1=1 AND f_data_type=' + this.dataType + ' AND f_data_id=' + this.dataValue + ' AND f_state=\'有效\'')
                this.clearaddParamData()
                this.close()
              }
            }).catch(() => {
              this.$showMessage("修改时出错!错误信息为:" + err)
            })
          }
        })
      },
      //删除
      del(id) {
        this.$showMessage('是否确定删除该参数?(删除的参数无法恢复，但可在数据库查询)', ['confirm', 'cancel']).then((res) => {
          if (res == 'confirm') {
            this.$resetpost('rs/logic/deleteExtraParams', {data: {id: id}}, {
              rejectMsg: null,
              resolveMsg: null
            }).then((res) => {
              if (res.data > 0) {
                this.$showMessage("删除成功！")
                this.search('1=1 AND f_data_type=' + this.dataType + ' AND f_data_id=' + this.dataValue + ' AND f_state=\'有效\'', this.model)
              } else {
                this.$showMessage("删除失败!该参数不存在!(可能已被他人删除该参数)")
              }
            }).catch((err) => {
              this.$showMessage("删除时出错!错误信息为:" + err)
            })
          }
        })
      },
      //新增
      add() {
        this.$showMessage("确定新增该参数?", ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            this.$resetpost('rs/logic/installExtraParams', {
              data: {
                f_data_id: this.dataValue,
                f_data_type: this.dataType,
                f_param_name: this.addParamData.f_param_name,
                f_param_message: this.addParamData.f_param_message,
                f_param_type: this.addParamData.f_param_type,
                f_param_value: this.addParamData.f_param_value
              }
            }, {rejectMsg: null, resolveMsg: null}).then((res) => {
              if (res.data > 0) {
                this.$showMessage("增加参数成功！")
                this.search('1=1 AND f_data_type=' + this.dataType + ' AND f_data_id=' + this.dataValue + ' AND f_state=\'有效\'')
                this.clearaddParamData()
                this.close()
              } else if (res.data == 'readOnly') {
                this.$showMessage("增加参数失败,您增加了一条重复的只读参数.")
              }
            }).catch((err) => {
              this.$showMessage("新增时出错!错误信息为:" + err)
            })
          }
        })
      },
      dataSubmit() {
        if (this.btnName == '新增') {
          this.add()
        } else if (this.btnName == '修改') {
          this.modify()
        } else {
          console.log("失败！")
        }
      },
      isAddShow() {
        //清空
        this.clearaddParamData()
        this.btnName = '新增'
        this.addParam = true
      },
      isUpdateShow(data) {
        //清空
        this.clearaddParamData()
        //修改则进入该方法，如果未显示，则显示
        this.addParamData.id = data.id
        this.addParamData.f_param_name = data.f_param_name
        this.addParamData.f_param_message = data.f_param_message
        this.addParamData.f_param_type = data.f_param_type
        this.addParamData.f_param_value = data.f_param_value
        this.btnName = '修改'
        this.addParam = true
      },
      close() {
        this.addParam = false
        this.clearaddParamData()
      },
      clearaddParamData() {
        this.addParamData.f_param_name = ''
        this.addParamData.f_param_message = ''
        this.addParamData.f_param_type = ''
        this.addParamData.f_param_value = ''
        this.addParamData.id = ''
        this.btnName = ''
      }
    },
    computed: {
      brand() {
        return this.typeModel
      }
    },
    watch: {
      'isReadOnly'(val) {
        let read = val ? '1' : '0'
        let condition2 = this.selectValue == '' ? '1=1 AND f_data_type=\'' + this.dataType + '\' AND f_data_id=' + this.dataValue + ' AND f_state=\'有效\' AND f_isreadonly=\'' + read + '\''
          : '1=1 AND f_data_type=\'' + this.dataType + '\' AND f_data_id=' + this.dataValue + ' AND f_param_type = \'' + this.selectValue + '\' AND f_state=\'有效\' AND f_isreadonly=\'' + read + '\''
        this.search(condition2)
      }
    }
  }
</script>

<style scoped>
</style>
