<template>
  <div class="flex-row">
    <div style="flex: 1.5;" class="flex">
      <div class="flex">
        <criteria-paged :model="model" v-ref:paged :pager="!row && !areainfo" @sort="sort">
          <criteria partial='criteria' v-ref:criteria @condition-changed="$parent.selfSearch">
            <div novalidate class="form-inline auto" partial>
              <div class="form-group">
                <input type="text" class="form-control" v-model="model.f_title"
                       condition="f_title like '%{}%'" placeholder='标题'>
              </div>
              <div class="form-group">
                <input type="text" class="form-control" v-model="model.f_date"
                       condition="f_date like '%{}%'" placeholder='创建时间'>
              </div>
              <div class="form-group">
                <input type="text" class="form-control" v-model="model.f_operator"
                       condition="f_operator = '{}'" placeholder='创建人'>
              </div>
              <div class="form-group">
                <button class="btn btn-success" @click="search(),$parent.$parent.clean()">查询</button>
              </div>

              <div class="form-group">
                <button type="button" name="button" class="btn btn-success" @click.stop="$parent.$parent.add()">新增</button>
              </div>
            </div>
          </criteria>
          <data-grid :model="model" v-ref:grid partial='list'>
            <template partial='head'>
              <tr>
                <th>
                  <nobr>序号</nobr>
                </th>
                <th>
                  <nobr>标题</nobr>
                </th>
                <th>
                  <nobr>创建时间</nobr>
                </th>
                <th>
                  <nobr>创建人</nobr>
                </th>
                <th>
                  <nobr>操作</nobr>
                </th>
                <th>
                  <nobr>下载查看</nobr>
                </th>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">{{$index + 1}}</td>
              <td style="text-align: center;">{{row.f_title}}</td>
              <td style="text-align: center;">{{row.f_date}}</td>
              <td style="text-align: center;">{{row.f_operator}}</td>
              <td style="text-align: center;">
                <button type="button" name="button" class="btn btn-link"
                        @click.stop="$parent.$parent.$parent.modifyInfo(row)">修正
                </button>
                <button type="button" name="button" class="btn btn-link"
                                 @click.stop="$parent.$parent.$parent.revoke(row)">撤销
              </button>
              </td>
              <td style="text-align: center;">
                <button type="button" name="button" class="btn btn-link"
                        @click.stop="$parent.$parent.$parent.download(row)">点击下载
                </button>
              </td>
            </template>
          </data-grid>
        </criteria-paged>
      </div>
      <div style="height: 500px;" v-show="row || areainfo" class="select-overspread">
        <knowledge-general-info v-if="row || areainfo" :row="row" @clean="clean" :areainfo="areainfo" @success="renew"
                           @build-success="buildFileSuccess"></knowledge-general-info>
      </div>
      <div style="height: 500px;" v-if="adds">
        <knowledge-add v-if="adds" :useraccount="model.rows"  @clean="addsClean" @success="addsSuccess"></knowledge-add>
      </div>

    </div>
    </div>
  <modal :show.sync="add_show"  backdrop="false">
    <header slot="modal-header" class="modal-header">
      <h4 class="modal-title">新增知识库</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <form class="form-horizontal" >
        <div class="form-group">
          <label for="f_title" class="control-label">标题</label>
          <input type="text"  class="form-control" v-model="model.f_title"
                 placeholder='标题'>
        </div>
        <div class="row">
          <div class="col-sm-8">
            <upload :blodid="blodid" :reload-all="true"></upload>
          </div>
        </div>
      </form>
    </article>

    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="btn btn-success" @click='addConfirm'>确认</button>
      <button type="button" class="btn btn-default" @click='addclose'>取消</button>
    </footer>
  </modal>
  <modal :show.sync="update_show"  backdrop="false">
    <header slot="modal-header" class="modal-header">
      <h4 class="modal-title">修改知识库</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <form class="form-horizontal" >
        <div class="form-group">
          <label for="f_title" class="control-label">标题</label>
          <input type="text"  class="form-control" v-model="updaterow.f_title"
                 placeholder='标题'>
        </div>
        <div class="form-group">
          <label for="f_reason" class="control-label">修改原因</label>
          <input type="text"  class="form-control" v-model="updaterow.f_reason"
                 placeholder='修改原因'>
        </div>
        <div class="row">
          <div class="col-sm-8">
            <upload :blodid="blodid" :reload-all="true"></upload>
          </div>
        </div>
      </form>
    </article>

    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="btn btn-success" @click='updateConfirm'>确认</button>
      <button type="button" class="btn btn-default" @click='updateclose'>取消</button>
    </footer>
  </modal>
</template>

<script>

  import {PagedList} from 'vue-client'
  let revoke = async function (self) {
    let param = {
      id:self.updaterow.id,
      f_state: '0',
    }
    let param2 = {
      f_operation_type:'撤销',
      f_operator: self.$login.f.name,
      f_operation_id: self.updaterow.id,
      f_change_type:'知识库',
      f_reason:'撤销',
    }
    console.log('撤销知识库', param)
    self.$resetpost('rs/entity/t_knowledge_stopgas', param).then(() => {
      // this.params.rows.splice(index, 1)
      console.log('撤销成功！')
      self.$resetpost('rs/entity/t_operationrecord', param2).then(() => {
        console.log('撤销记录成功！')
      })
      this.$refs.paged.$refs.criteria.search()
      this.f_error_reason = ''
    })
    self.search()
  }
  let update = async function (self) {
    let param = {
      id:self.updaterow.id,
      f_title: self.updaterow.f_title,
      f_operation_type:'修改',
      f_operator: self.$login.f.name,
      f_change_type: '知识库',
      f_reason: self.updaterow.f_reason,
    }
    console.log('修改知识库', param)
    self.$resetpost('rs/logic/knowledgerecord', param).then(() => {
      // this.params.rows.splice(index, 1)
      console.log('更新成功！')
      this.$refs.paged.$refs.criteria.search()
      this.f_error_reason = ''
    })
    self.update_show = false
  }
  let add = async function (self) {
    console.log('self:',self)
    let param = {
      f_title: self.model.f_title,
      f_date: self.$login.toStandardDateString(),
      f_operator: self.$login.f.name,
      f_type:'知识库',
      f_state:'1',
    }
    console.log('发布知识库', param)
    self.$resetpost('rs/logic/addknowledge', param).then(() => {
      // this.params.rows.splice(index, 1)
      console.log('发布成功！')
      this.$refs.paged.$refs.criteria.search()
      this.f_error_reason = ''
    })

    self.add_show = false
    self.search()
  }
  export default {
    'title': '燃气知识',
    data () {
      return {

        model: new PagedList('rs/sql/getGasKnowledge', 20, {orderitem: '`f_date DESC`'}),

        row: null, // 选择一条用户信息，对其进行修改
        rows: [],
        adds:false,   //控制增加弹出窗口
        add_show:false,
        update_show:false,
        updaterow:null,
      }
    },
    ready () {
      this.search()
    },
    methods: {
      search () {

        let condition = "1=1"
        console.log(condition)
        this.model.search(condition)
        //his.$refs.paged.$refs.criteria.search()
      },
      modifyInfo (row) {
        console.log('modifyrow')
        this.updaterow = row
        this.update_show = true
      },
      revoke(row){
        this.updaterow=row
        this.$showMessage(`是否确认撤销公告`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            revoke(this)
          }

        })
      },
      updateclose(){

        this.update_show=false
        this.search()
      },
      updateConfirm(){
        this.$showMessage(`是否确认修改知识库`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            update(this)
          }

        })
      },
      look(){
        console.log('look')
      },
      download(row){
        console.log(row.filepath)
      },

      add(){
        console.log('add')
        this.add_show = true
      },
      modifyUser (row) {
        this.row = row
        this.areainfo = null
        this.$refs.paged.$refs.grid.model.rows = [row]
      },
      clean () {
        console.log('clean')
     /*   this.row = null
        this.areainfo = null
        this.$refs.paged.$refs.grid.select(null)
        this.$refs.paged.$refs.grid.model.rows = this.rows*/
        Object.keys(this.$refs.paged.$refs.criteria.model).forEach((key) => {
          this.$refs.paged.$refs.criteria.model[key] = ''
        })
        console.log('condition:', this.$refs.paged.$refs.criteria.condition)
        this.model.search(this.$refs.paged.$refs.criteria.condition)
      },
      renew () {
        this.row = null
        this.areainfo = null
        this.clearCondition()
        this.search()
        //建档或修正档案成功刷新地址列表
        this.$refs.addressfiles.$refs.paged.$refs.cri.search()
      },
      clearCondition () {
        console.log('清空条件。。', this.$refs.paged)
        Object.keys(this.$refs.paged.$refs.criteria.model).forEach((key) => {
          this.$refs.paged.$refs.criteria.model[key] = ''
        })
      },

      bulkOperations (name, type) {
        if (name === '批量销户') {
          this.batcan = true
          this.batenable = false
          this.newArea = false
        } else if (name === '批量启用') {
          this.batenable = true
          this.batcan = false
          this.newArea = false
        }
      },
      addsSuccess () {
        this.clearCondition()
        this.clean()
      },
      addsClean () {
        this.adds = false
        this.search()
      },
      buildFile (row) {
        // console.log('我是建档。。。', row)
        this.row = row
      },
      // 小区批量建档
      bulidFiles (row) {
        this.row = null
        this.areainfo = row
        // 将列表的数据清空
        this.$refs.paged.$refs.grid.model.rows = this.rows
      },
      // 小区批量建档成功
      buildFileSuccess (data) {
        // console.log('建档案成功', data)
        this.model.rows.unshift(data)
      },
      selfSearch (args) {

        this.model.search(args.condition, args.model)
      },
      sort (field, rule) {
        // 将所有排序方式设为不排序，实现相互排斥
        for (let key in this.orderFields) {
          if (key === field) {
            this.orderFields[key] = rule
          } else {
            this.orderFields[key] = 'no'
          }
        }
        // 如果新规则不排序，还原为默认排序
        if (rule === 'no') {
          this.model.paramSource.orderitem = `'${this.orderDefault}'`
        } else {
          this.model.paramSource.orderitem = `'${field} ${rule}'`
        }

        this.search()
      },

      getLocation(row){
        // 可能会存在添加，完善，建档
        console.log('进入', row)

        addressInfoGen(this, row)

      },
      areaFiles(row){
        row.isEdit = true
        this.bulidFiles(row)
      },
      addclose(){
        this.add_show=false
      },
      addConfirm(){
        this.$showMessage(`是否确认添加新的知识库`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            add(this)
          }

        })
      },
      initAdjustables () {
        console.log('初始化调压箱', this.$GetSaleParam)
        let arr = []
        let filter = this.$login.f.f_orgids
        this.$GetSaleParam.getAdjustable(filter).forEach((item) => {
          let temp = {}
          temp.label = `[${item.value.f_adjustable_id}]-${item.label}`
          temp.value = `${item.value.id}`
          arr.push(temp)
        })
        this.adjustables = [{label: '全部', value: ''}, ...arr]
      },
    },
    computed: {
      userstates () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户状态')]
      },
      areas () {
        let rs = []
        for (let i = 0; i < this.$login.f.f_allArea.length; i++) {
          let temp = {
            label: this.$login.f.f_allArea[i].label,
            value: this.$login.f.f_allArea[i].label
          }
          rs.push(temp)
        }
        return [{label: '全部', value: ''}, ...rs]
      },
      firestates () {
        return [
          {label: '全部', value: ''},
          {label: '点火', value: '1'},
          {label: '未点火', value: '0'}
        ]
      },
      jurisdiction () {
        return this.$login.r
      }
    },
    watch: {
      // 每次查询将查询到的数据存储起来，供选择操作后的替换
      'model.state' (val) {
        if (val === '正确') {
          this.rows = this.model.rows
        }
      }
    },
    events: {
      onFileUpload: function (file, res) {
        let data = {
          tablename: 't_userinfo',
          filepath: res.path
        }
        this.$resetpost('rs/logic/import', data, {resolveMsg: null, rejectMsg: '上传失败'}).then((res) => {
          console.log('dddddddd', this)
          this.close()
          this.$showAlert(`一共上传${res.data.total}条数据, 成功${res.data.successnum}条`, 'success', 2000)
          this.search()
        })
      }
    }
  }
</script>
<style>
  .nav-tabss ul > li > a {
    padding: 5px 10px;
  }
</style>
