<template>
  <div class="flex">
    <p class="bg-info text-center" style="padding: 8px;">参数管理
      <span class="btn btn-link add-postition" @click="add('t_parameter')">新增</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);return false;">查询</button>
          </div>
        </div>
      </form>
    </div>
    <div class="span">
      <data-grid :model="model" v-ref:params @select-changed="selected" class="list_area table_sy">
        <template partial='head'>
          <tr>
            <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.orgname ? row.orgname + '专用' : '全局'}}</td>
          <td class="flex-around">
            <button type="button" name="button" class="btn btn-link"
                    @click.stop="$parent.$parent.modifyParam(row, 't_parameter')">修改</button>
            <button type="button" name="button" class="btn btn-link"
                    @click.stop="$parent.$parent.deleteParam(row, 't_parameter')">删除</button>
          </td>
        </template>
      </data-grid>
    </div>

    <p v-if="selectItem" class="bg-info text-center" style="padding: 8px;margin: 8px 0px;">
      {{selectItem.name}}
      <span class="btn btn-link add-postition" @click="add('t_paramvalue')">新增</span>
    </p>
    <div v-if="selectItem" class="span">
      <data-grid :model="params" v-ref:selects >
        <template partial='head'>
          <tr>
            <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 class="flex-around">
            <button type="button" name="button" class="btn btn-link" @click.stop="$parent.$parent.modifyParam(row, 't_paramvalue')">修改</button>
            <button type="button" name="button" class="btn btn-link" @click.stop="$parent.$parent.deleteParam(row, 't_paramvalue')">删除</button>
          </td>
        </template>
      </data-grid>
    </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="modifyItem.name" @keyup.enter="confirm">
            </div>
            <div class="col-sm-6 form-group" v-if="modifyItem.EntityType === 't_parameter'">
              <div class="checkbox">
                <label>
                  <input type="checkbox" v-model="modifyItem.isGlobal" :checked="modifyItem.isGlobal">是否全局
                </label>
              </div>
            </div>
          </div>
          <div class="row" v-if="modifyItem.EntityType === 't_parameter' && !modifyItem.isGlobal">
            <div class="col-sm-6 form-group">
              <label class="font_normal_body">所属公司</label>
              <res-select restype='organization'
                          :is-mul="false"
                          :initresid="[conditionModel.f_cond_orgid]"
                          @res-select="getModifyOrg">
              </res-select>
            </div>
          </div>
        </form>
      </article>

      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="button_search button_spacing" @click='confirm'>确认</button>
        <button 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 {
        conditionModel: {
          paramName: null,
          f_cond_orgid: null
        },

        model: {  // 参数列表显示的数据
          rows: []
        },

        selectItem: null, // 选择的参数
        params: { // 选择的参数的具体内容
          rows: []
        },
        selectContent: null, // 选中的一条数据（包含参数或参数的具体内容）
        modifyItem: {
          name: '',
          isGlobal: true,
          f_filialeids: null
        }, // 要修改的参数

        copyShow: false,

        show: false, // 是否显示添加修改模态框

        copySource: {
          rows: []
        },

        curorgid: [this.$login.f.orgid],
        jurisdiction: this.$login.r
      }
    },
    ready () {
      this.loadParams()
    },
    watch: {
      'modifyItem.isGlobal' (val) {
        if (val) {
          this.modifyItem.f_filialeids = null
        }
      }
    },
    methods: {
      // 加载全部参数
      loadParams () {
        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_getParam', {data: condition}, {resolveMsg: null, rejectMsg: '获取参数出错！！'})
        this.model.rows = res.data
      },
      selected (val) {
        this.selectItem = val.val
        this.getParamValue(val.val)
      },
      async getParamValue(obj) {
        let http = new HttpResetClass()
        let res = await http.load('POST', 'rs/sql/system_getParamValue', {data: {param_id: obj.id}}, {resolveMsg: null, rejectMsg: '获取参数出错！！'})
        this.params.rows  = res.data
      },
      filter_query (paramName,orgid) {
        console.log('过滤查询',paramName, orgid)
        this.selectItem = null
        let param = {}
        if (paramName && orgid) {
          param = {
            condition: `p.name like '%${paramName}%'`,
            f_filialeid: orgid
          }
        } else if (orgid) {
          param = {
            condition: '1=1',
            f_filialeid: orgid
          }
        } else if (paramName) {
          param = {
            condition: `p.name like '%${paramName}%'`,
            f_filialeid: this.$login.f.orgid
          }
        } else {
          param = {
            condition: '1=1',
            f_filialeid: this.$login.f.orgid
          }
        }
        this.queryParam(param)
      },
      // 添加参数或添加参数内容
      add (type) {
        this.modifyItem.name = ''
        this.modifyItem.isGlobal = true
        this.modifyItem.EntityType = type
        this.show = true
      },
      // 修改参数或添加参数内容
      modifyParam (row, type) {
        this.selectContent = row
        this.modifyItem.name = row.name
        this.modifyItem.id = row.id
        this.modifyItem.EntityType = type
        this.modifyItem.isGlobal = !row.orgname
        this.show = true
      },
      // 删除
      deleteParam (row, type) {
        if (type === 't_parameter') {
          this.$showMessage('删除后不可恢复，确认删除吗？', ['confirm', 'cancel']).then((res) => {
            if (res === 'confirm') {
              this.$resetpost('rs/logic/deleteParam', row).then((res) => {
                this.filter_query(this.conditionModel.paramName, this.conditionModel.f_cond_orgid)
                this.selectItem = null
              })
            }
          })
        } else if (type === 't_paramvalue') {
          this.$showMessage('删除后不可恢复，确认删除吗？', ['confirm', 'cancel']).then((res) => {
            if (res === 'confirm') {
              this.$resetpost('rs/logic/deleteValue', {tablename: 't_paramvalue', id: row.id}).then((res) => {
                this.getParamValue(this.selectItem)
              })
            }
          })
        }
      },
      // 修该内容的提交
      confirm () {
        let data = {}
        if (this.modifyItem.EntityType === 't_parameter') {
          // 有id为修改
          if (this.modifyItem.id) {
            data = {
              id: this.modifyItem.id,
              name: this.modifyItem.name
            }
          } else { // 否则为新增
            data = {
              name: this.modifyItem.name
            }
            //添加当前分公司
          }
          data.f_filialeids = this.modifyItem.f_filialeids
          this.$resetpost('rs/logic/system_saveParam', data).then((res) => {
            //无id
            this.filter_query(this.conditionModel.paramName, this.conditionModel.f_cond_orgid)
            this.show = false
            this.modifyItem = {name: '', isGlobal: true, f_filialeids: null}
          })
        } else if (this.modifyItem.EntityType === 't_paramvalue') {
          if (this.modifyItem.id) {
            data = {
              id: this.modifyItem.id,
              name: this.modifyItem.name,
              processid: this.modifyItem.processid
            }
          } else {
            data = {
              name: this.modifyItem.name,
              processid: this.selectItem.id
            }
          }
          this.$resetpost('rs/entity/t_paramvalue', data).then((res) => {
            this.show = false
            // 如果没有id，需要在数组中添加
            this.getParamValue(this.selectItem)
          })
        }

      },
      // 关闭模态框, 数据还原
      close () {
        this.copyShow = false
        this.show = false
        this.modifyItem = {name: '', isGlobal: true, f_filialeids: null}
      },

      getModifyOrg (val) {
        this.modifyItem.f_filialeids = val[0]
      },

      getorgCur (val) {
        this.conditionModel.f_cond_orgid = val[0]
      }
    }
  }
</script>
