<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding marginLeft-10">
      <div class="layout-content-main  tree-position">
        <div class="position-right">
          <Row>
            <!--查询-->
            <Col span="24">
              <Form id="search-form" v-show="searchOpen" :label-width="80" inline onsubmit="return false">
                <label class="label-sign"></label>
                <Form-item class="search-item" label="模板名称:">
                  <Input v-model="formObj.alias" @on-enter="_formSearch" name="alias" placeholder="请输入模板名称" clearable></Input>
                  <input type="hidden" name="classifyId">
                </Form-item>
                <Form-item class="search-btn">
                  <Button @click="_formSearch" type="primary">搜索</Button>
                </Form-item>
              </Form>
            </Col>
            <!--操作-->
            <Col span="24">
              <btn-list :msg="btn" :open="searchOpen" :show-search-btn="true" @on-result-change="_btnClick"></btn-list>
            </Col>
            <!-- 表格 -->
            <Col span="24">
              <PTVXETable ref="pageTable" :page-columns="pageColumns"
                            :get-page="getPage" :opt-col-width="120" :icon-msg="iconMsg" @on-result-change="_tableResultChange">
                <vxe-table-column
                  v-for="item in pageColumns"
                  :key="item.key"
                  :field="item.key"
                  :title="item.title"
                  :min-width="item.width?item.width:200"
                  :fixed="item.fixed?item.fixed:undefined" sortable>
                  <template slot-scope="scope">
                    <span>{{scope.row[item.key]}}</span>
                  </template>
                </vxe-table-column>
              </PTVXETable>
            </Col>
          </Row>
        </div>
      </div>
    </div>
    <!-- 添加、编辑 -->
    <SoilRecordTemplateEdit ref="editModal" @on-result-change="_search"></SoilRecordTemplateEdit>
    <SoilRecordEdit ref="reportEdit" @on-result-change="_search"></SoilRecordEdit>
    <ConfigTemplate ref="configTemplateModal" @on-result-change="_page"></ConfigTemplate>
  </div>
</template>
<script>
import global from '../../../api/config'
import { soilAptitude } from '../../../api'
import SoilRecordTemplateEdit from './SoilReportTemplateEdit'
import SoilRecordEdit from './SoilReportEdit'
import ConfigTemplate from './ConfigTemplate'
export default {
  components: { SoilRecordTemplateEdit, SoilRecordEdit, ConfigTemplate },
  data() {
    return {
      pageColumns: [
        { title: '报告模板名称', key: 'alias' },
        { title: '类别', key: 'classType' },
        { title: '备注', key: 'remark' }
      ],
      formObj: {
        name: undefined
      },
      iconMsg: [
        { type: 'md-create', id: '', name: '编辑' },
        { type: 'ios-build-outline', id: '', name: '配置' },
        { type: 'ios-book', id: '', name: '预览/编辑' },
        { type: 'md-remove-circle', id: '', name: '删除' }
      ],
      getPage: {},
      tableStyleObj: {},
      btn: [
        { type: 'success', id: '', name: '添加' },
        { type: 'error', id: '', name: '删除' }
      ],
      searchOpen: true,
      selectIds: []
    }
  },
  computed: {
    // tableHeight: function() {
    //   if (this.searchOpen) {
    //     return this.$newTableHeight('search')
    //   } else {
    //     return this.$newTableHeight('noSearch')
    //   }
    // }
  },
  mounted() {
    // this._contHide()
    // this._classTree()
    this._page()
  },
  methods: {
    _btnClick(msg) {
      switch (msg) {
        case '添加':
          this._editModal()
          break
        case '删除':
          this._delAll()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _refresh() {
      // 刷新
      this.id = ''
      this.name = ''
      this._search()
      this._classTree()
    },
    _classData(result, msg) {
      $('input[name=classifyId]').val(result)
      this._formSearch()
    },
    _classTree() {
      this.$refs.classTree._Ztree()
    },
    _treeHide() {
      // 左侧树隐藏
      this.isTree = false
      this.tableStyleObj.marginLeft = '15px'
    },
    _treeShow() {
      this.isTree = true
      this.tableStyleObj.marginLeft = '215px'
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._reportEdit(data)
          break
        case '配置':
          this._configData(data)
          break
        case '下载':
          this._download(data.id)
          break
        case '预览/编辑':
          this._lookReport(data)
          break
        case '删除':
          this._deleteByIds([data.id])
          break
      }
    },

    _lookReport: async function(data) {
      const result = await soilAptitude.expReportTemplateGetById(data.id)
      if (result) {
        this._viewReport(result)
      }
    },

    _viewReport(data) {
      // window.open(global.baseURL + '/print/v1/oos?key=' + objectKey+'&edit=true', '', 'height=' + (screen.availHeight - 50) + ',' +
      //   'width=' + (screen.availWidth - 10) + ',top=0,left=0,toolbar=no,menubar=no,scrollbars=auto,resizeable=no,' +
      //   'location=no,status=no')
      if (data) {
        this.$openWindowModeless({
          objectKey: data.objectKey,
          id: data.id,
          idType: 11,
          isReport: 4
        })
      }
    },
    _contHide() {
      this._page()
    },
    _page: async function() {
      console.log('page')
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await soilAptitude.pageSummaryTemplate(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
      // const result = await
      // this.$refs.pageTable._page('search-form', 'SysFileTemplate/page')
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _editModal() {
      this.$refs.editModal._open()
    },
    _reportEdit(data) {
      this.$refs.reportEdit._open(data)
    },
    _configData(data) {
      this.$refs.configTemplateModal._open(data)
    },
    // 批量删除
    _delAll() {
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this._deleteByIds(ids)
      }
    },
    // 删除
    _deleteByIds(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {
          this._delete(ids)
        }
      })
    },
    _delete: async function(ids) {
      const result = await soilAptitude.expReportTemplateDelete(ids)
      if (result) {
        this._formSearch()
        this.$Message.success('删除成功！')
      }
    },
    _download(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要下载？',
        onOk: () => {
          window.open(
            global.baseURL + '/base/v1/file_template/download/' + id,
            '_blank'
          )
        }
      })
    },
    _search() {
      this._page()
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          // this.getPage = this.$store.state.SysFileTemplate.page
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'changeSize':
          this._page()
          break
      }
    }
  }
}
</script>
