<template>
  <div class="flex">
    <p  class="bg-info text-center" style="padding: 8px;">
      单值管理
      <span class="btn btn-link add-postition" @click="add()">新增</span>
    </p>
    <div class="auto">
      <form class="form-horizontal" style="margin-bottom: 5px;">
        <div class="row">
          <div class="col-sm-4 form-group">
            <label class="font_normal_body">使用公司</label>
            <res-select restype='organization'
                        is-mul="false"
                        :initresid="curorgid"
                        @res-select="getorgCur">
            </res-select>
          </div>
          <div class="col-sm-4 form-group">
            <label class="font_normal_body">参数名称</label>
            <input type="text" class="input_search" style="width: 60%"  placeholder='请输入查找的参数名' v-model="conditionModel.paramName" @keyup.enter="filter_query(param, f_orgid_current)">
          </div>
          <div class="col-sm-4 form-group button-range"  style="float:left;">
            <button type="button" class="button_search" @click="filter_query(conditionModel.paramName,conditionModel.f_cond_orgid)">查询</button>
          </div>
        </div>
      </form>
    </div>
    <div class="span">
      <partial-view v-ref:single-load>
        <data-grid :model="model" v-ref:grid class="list_area table_sy">
          <template partial='head'>
            <tr>
              <th>序号</th>
              <th>名称</th>
              <th>参数值</th>
              <th>所属</th>
              <th>操作</th>
            </tr>
          </template>
          <template partial='body'>
            <td style="text-align:center">{{$index+1}}</td>
            <td style="text-align:center">{{row.name}}</td>
            <td style="text-align:center">{{row.value}}</td>
            <td style="text-align:center">{{row.orgname ? row.orgname + '专用' : '全局'}}</td>
            <td class="flex-around">
              <button type="button" name="button" class="btn btn-link"
                      @click.stop="$parent.$parent.$parent.modifyParam(row)">修改</button>
              <button type="button" name="button" class="btn btn-link"
                      @click.stop="$parent.$parent.$parent.deleteItem(row)">删除</button>
            </td>
          </template>
        </data-grid>
      </partial-view>
    </div>
    <modal v-if="show" :show.sync="show" v-ref:modal backdrop="false">
      <header slot="modal-header" class="modal-header">
        <button type="button" class="close" @click="close"><span>&times;</span></button>
        <h4 class="modal-title">单值配置</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <form class="form-horizontal">
          <div class="row">
            <div class="col-sm-6 form-group">
              <label class="font_normal_body">单值名称</label>
              <input type="text" class="input_search" style="width: 60%" v-model="selectItem.name">
            </div>
            <div class="col-sm-6 form-group">
              <label  class="font_normal_body">单值内容</label>
              <input type="text" class="input_search" style="width: 60%" v-model="selectItem.value">
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6 form-group" v-if="!selectItem.isGlobal">
              <label class="font_normal_body">所属公司</label>
              <res-select restype='organization'
                          :initresid="[conditionModel.f_cond_orgid]"
                          :is-mul="false"
                          @res-select="getModifyOrg">
              </res-select>
            </div>
            <div class="checkbox col-sm-6 form-group">
              <label>
                <input type="checkbox" v-model="selectItem.isGlobal" :checked="selectItem.isGlobal">是否全局
              </label>
            </div>
          </div>
        </form>

      </article>

      <footer slot="modal-footer" class="modal-footer">
        <button v-show="show" type="button" class="button_search button_spacing" @click='confirm'>确认</button>
        <button v-show="show" type="button" class="button_clear button_spacing" @click='close'>取消</button>
      </footer>
    </modal>
  </div>
</template>
<script>

  import {HttpResetClass} from 'vue-client'

  export default {
    title: '用户操作',
    data () {
      return {
        selectItem: {
          name: '',
          value: '',
          isGlobal: true,
          f_filialeids: null
        },

        // 查询条件
        conditionModel: {
          paramName: null,
          f_cond_orgid: null
        },

        params: null,
        model: {
          rows: []
        },
        // 控制单值模态框
        show: false,
        curorgid: [this.$login.f.orgid]
      }
    },
    ready () {
      // this.model.rows = AppData.singleValues
      this.loadSingle()
    },
    watch: {
      'selectItem.isGlobal' (val) {
        if (val) {
          this.selectItem.f_filialeids = null
        }
      }
    },
    methods: {

      loadSingle () {
        let param = {
          condition: '1=1',
          f_filialeid: this.$login.f.orgid
        }
        this.queryParam(param)
      },

      async queryParam (condition) {
        this.model.rows = []
        let http = new HttpResetClass()
        let res = await http.load('POST', 'rs/sql/system_getSingle', {data: condition}, {resolveMsg: null, rejectMsg: '获取参数出错！！'})
        this.model.rows = res.data
      },

      filter_query (paramName,orgid) {
        console.log('过滤查询',paramName, orgid)
        let param = {}
        if (paramName && orgid) {
          param = {
            condition: `s.name like '%${paramName}%'`,
            f_filialeid: orgid
          }
        } else if (orgid) {
          param = {
            condition: '1=1',
            f_filialeid: orgid
          }
        } else if (paramName) {
          param = {
            condition: `s.name like '%${paramName}%'`,
            f_filialeid: this.$login.f.orgid
          }
        } else {
          param = {
            condition: '1=1',
            f_filialeid: this.$login.f.orgid
          }
        }
        this.queryParam(param)

      },
      deleteItem (row) {
        this.$showMessage('删除后不可恢复，确认删除吗？', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            this.$resetpost('rs/logic/deleteValue', {tablename: 't_singlevalue', id: row.id}).then((res) => {
              this.filter_query(this.conditionModel.paramName, this.conditionModel.f_cond_orgid)
            })
          }
        })
      },
      add () {
        this.show = true
      },
      modifyParam (row) {
        console.log('修改单值', row)
        this.selectItem.name = row.name
        this.selectItem.id = row.id
        this.selectItem.value = row.value
        this.selectItem.isGlobal = !row.orgname
        this.show = true
      },
      confirm () {
        this.$resetpost('rs/logic/system_saveSingle', this.selectItem).then((res) => {
          // 如果没有id，需要在数组中添加
          this.filter_query(this.conditionModel.paramName, this.conditionModel.f_cond_orgid)
          this.show = false
          this.selectItem = {
            name: '',
            value: '',
            isGlobal: true,
            f_filialeids: null
          }
        })
      },
      getorgCur (val) {
        this.conditionModel.f_cond_orgid = val[0]
      },
      getModifyOrg (val) {
        this.selectItem.f_filialeids = val[0]
      },
      // 关闭模态框
      close () {
        this.show = false
        this.selectItem = {
          name: '',
          value: '',
          isGlobal: true,
          f_filialeids: null
        }
      }
    }

  }
</script>
