<template>
  <div>
    <Modal v-model="showModal" width="1000" class="modal-footer-none">
      <div slot="header">{{modalTitle}}</div>
      <div>
        <Form id="index-right-form" :label-width="80" inline onsubmit="return false">
          <label class="label-sign"></label>
          <Form-item class="search-item" label="属性名称:">
            <Input v-model="formObj.attributeName" @on-enter="_formSearch" placeholder="请输入属性名称" clearable/>
          </Form-item>
          <Form-item class="search-btn">
            <Button @click="_formSearch" type="primary">搜索</Button>
          </Form-item>
        </Form>
          <btn-list :msg="btn" @on-result-change="_btnClick" class="contHide" style="margin-bottom: 10px;"/>
        <PTVXETable ref="pageTable" :tableHeight="tableHeight"
                    @on-result-change="_tableResultChange" :getPage="getPage" :iconMsg="iconMsg">
          <vxe-table-column
            :field="item.key"
            :title="item.title"
            :min-width="item.width?item.width:200"
            :fixed="item.fixed?item.fixed:undefined"
            v-for="item in pageColumns" :key="item.key">
            <template slot-scope="scope">
              <span v-if="item.key==='defaulted'">{{scope.row[item.key]?'是':'否'}}</span>
              <span v-else-if="item.key==='itemed'">{{scope.row[item.key] === 1?'是':'否'}}</span>
              <span v-else>{{scope.row[item.key]}}</span>
            </template>
          </vxe-table-column>
        </PTVXETable>
      </div>
    </Modal>
    <ConfigTemplateEdit ref="indexEdit" @on-result-change="_page"></ConfigTemplateEdit>
  </div>
</template>

<script>
import { soilAptitude } from '../../api'
import ConfigTemplateEdit from './ConfigTemplateEdit'

export default {
  components: { ConfigTemplateEdit },
  data() {
    return {
      id: '',
      modalTitle: '',
      showModal: false,
      btn: [
        { type: 'success', id: '', name: '添加配置' },
        { type: 'error', id: '', name: '删除' }
      ],
      tableHeight: document.documentElement.clientHeight - 180,
      pageColumns: [
        { title: '属性名称', key: 'attributeName' },
        { title: '属性', key: 'dataAttribute', width: 120 },
        { title: '公式配置', key: 'formula', width: 120 },
        { title: '是否是项目指标', key: 'itemed', width: 120 },
        { title: '列位置', key: 'columnPlace', width: 120 },
        { title: '合并开始列', key: 'mergeBegin', width: 120 },
        { title: '合并结束列', key: 'mergeEnd', width: 120 },
        { title: '合并行数', key: 'mergeRowNum', width: 120 },
        { title: '单位', key: 'unit', width: 120 }
      ],
      getPage: {},
      iconMsg: [{ type: 'md-create', id: '', name: '编辑' }],
      selectIds: [],
      formObj: {
        attributeName: undefined,
        templateId: undefined
      },
      // 资质信息
      aptitudeItemInfo: {}
    }
  },
  methods: {
    _open(data) {
      console.log(data)
      this.aptitudeItemInfo = data
      this.formObj = this.$resetFields(this.formObj)
      this.formObj.templateId = data.id
      this.id = data.id
      this.modalTitle = data.name + ' 指标管理'
      this.showModal = true
      this._page()
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await soilAptitude.originalTemplateConfig(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        console.log(result)
        this.getPage = result
        this.$refs.pageTable._hideLoading()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(true, data)
          break
      }
    },
    _editModal(edit, data) {
      if (edit) {
        this._getById(data)
      } else {
        this.$refs.indexEdit._open(this.id, '')
      }
    },
    _getById: async function(data) {
      const result = await soilAptitude.getOriginalTemplateConfig(data.id)
      console.log(result)
      if (result) {
        this.$refs.indexEdit._open('', result)
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加配置':
          this._editModal(false)
          break
        case '删除':
          this._deleteByIds()
          break
      }
    },
    _deleteByIds() {
      if (this.selectIds.length === 0) {
        // this.$msgTip('warning')
        this.$message.warning('请至少选择一条记录')
      } else {
        this.$Modal.confirm({
          title: '提示',
          content: '确定删除 ' + this.selectIds.length + ' 条记录？',
          onOk: () => {
            this._delOk(this.selectIds)
          }
        })
      }
    },
    _delOk: async function(ids) {
      const result = await soilAptitude.deleteOriginalTemplateConfig(ids)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    }
  }
}
</script>
