<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24" />
      </Col>
      <!--按钮-->
      <Col span="24" />
      <!--      <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />-->
<!--      <el-button type="primary" style="margin: 5px" @click="_addField">-->
<!--        新增-->
<!--      </el-button>-->
      </Col>
      <!--table-->
      <Col span="24" />
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        />

        <el-table-column v-for="item in pageColumns" :key="item.id" :label="item.title">
          <template slot-scope="scope">
            <span
              v-if="item.key==='dbFieldName'"
              @click.stop="_handleRow(scope)"
            >
              <el-input
                v-model="scope.row.dbFieldName"
                size="medium"
                placeholder=""
                disabled="disabled"
                @blur="_handleTestValueEdit(scope.row.id,scope.row.dbFieldName)"
              />
            </span>


            <span
              v-else-if="item.key==='dbFieldTxt'"
              @click.stop="_handleRow(scope)"
            >
              <el-input
                v-model="scope.row.dbFieldTxt"
                size="medium"
                placeholder=""
                disabled="disabled"
                @blur="_handleTestValueEdit(scope.row.id,scope.row.dbFieldTxt)"
              />
            </span>


            <span
              v-else-if="item.key==='isShowForm'"
              @click.stop="_handleRow(scope)"
            >
              <el-checkbox v-model="scope.row.isShowForm" @change="_checkShowForm(scope.row.id,scope.row.isShowForm)" />
            </span>


            <span
              v-else-if="item.key==='fieldLength'"
              @click.stop="_handleRow(scope)"
            >
              <span v-if="scope.row['dbFieldName']==='id'">
                <el-input
                  v-model="scope.row.fieldLength"
                  size="medium"
                  placeholder=""
                  type="number"
                  disabled="disabled"
                  @keydown.native="channelInputLimit"
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.fieldLength)"
                />
              </span>
              <span v-else>
                <el-input
                  v-model="scope.row.fieldLength"
                  size="medium"
                  type="number"
                  placeholder=""
                  @keydown.native="channelInputLimit"
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.fieldLength)"
                />
              </span>
            </span>



            <span
              v-else-if="item.key==='fieldShowType'"
              @click.stop="_handleRow(scope)"
            >
              <span v-if="scope.row['dbFieldName']==='id'">
                <el-select slot="prepend" v-model="scope.row.fieldShowType" disabled="disabled" placeholder="请选择">
                  <el-option label="文本框" value="text" />
                  <el-option label="密码" value="password" />
                  <el-option label="单选框" value="Integer" />
                  <el-option label="多选" value="Double" />
                  <el-option label="日期（yyyy-MM-dd）" value="date" />
                  <el-option label="日期（yyyy-MM-dd HH:mm:ss）" value="datetime" />
                </el-select>
              </span>
              <span v-else>
                <el-select slot="prepend" v-model="scope.row.fieldShowType" placeholder="请选择">
                  <el-option label="文本框" value="text" />
                  <el-option label="密码" value="password" />
                  <el-option label="单选框" value="Integer" />
                  <el-option label="多选" value="Double" />
                  <el-option label="日期（yyyy-MM-dd）" value="date" />
                  <el-option label="日期（yyyy-MM-dd HH:mm:ss）" value="datetime" />
                </el-select>

              </span>
            </span>






            <span
              v-else-if="item.key==='queryMode'"
              @click.stop="_handleRow(scope)"
            >
              <span v-if="scope.row['dbFieldName']==='id'">
                <el-select slot="prepend" v-model="scope.row.queryMode" disabled="disabled" placeholder="请选择">
                  <el-option label="普通查询" value="single" />
                  <el-option label="范围查询" value="group" />
                </el-select>
              </span>
              <span v-else>
                <el-select slot="prepend" v-model="scope.row.queryMode" placeholder="请选择">
                  <el-option label="普通查询" value="single" />
                  <el-option label="范围查询" value="group" />
                </el-select>

              </span>
            </span>



            <span
              v-else-if="item.key==='fieldExtendJson'"
              @click.stop="_handleRow(scope)"
            >
              <span v-if="scope.row['dbFieldName']==='id'">
                <el-input
                  v-model="scope.row.fieldExtendJson"
                  size="medium"
                  placeholder=""
                  disabled="disabled"
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.fieldExtendJson)"
                />
              </span>
              <span v-else>
                <el-input
                  v-model="scope.row.fieldExtendJson"
                  size="medium"
                  placeholder=""
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.fieldExtendJson)"
                />
              </span>
            </span>

            <span
              v-else-if="item.key==='fieldValueRuleCode'"
              @click.stop="_handleRow(scope)"
            >
              <span v-if="scope.row['dbFieldName']==='id'">
                <el-input
                  v-model="scope.row.fieldValueRuleCode"
                  size="medium"
                  placeholder=""
                  disabled="disabled"
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.fieldValueRuleCode)"
                />
              </span>
              <span v-else>
                <el-input
                  v-model="scope.row.fieldValueRuleCode"
                  size="medium"
                  placeholder=""
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.fieldValueRuleCode)"
                />
              </span>
            </span>




            <span
              v-else-if="item.key==='dbIsNull'"
              @click.stop="_handleRow(scope)"
            >
              <span v-if="scope.row['dbFieldName']==='id'">
                <el-input
                  v-model="scope.row.dbIsNull"
                  size="medium"
                  placeholder=""
                  disabled="disabled"
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.dbIsNull)"
                />
              </span>
              <span v-else>
                <el-input
                  v-model="scope.row.dbIsNull"
                  size="medium"
                  placeholder=""
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.dbIsNull)"
                />
              </span>
            </span>


            <span v-else>{{ scope.row[item.key] }}</span>
          </template>
        </el-table-column>
      </el-table>
      </Col>
    </Row>
    <!--添加编辑-->
    <typeEdit ref="editModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { onlFormHead } from '../../api'
import typeEdit from './OnlineFormDevEdit'
export default {
  components: { typeEdit },
  data() {
    return {
      formObj: {
        tableName: '',
        tableTxt: '',
        tableType: ''
      },
      currentRow: {},
      searchOpen: true,
      btn: [{ type: 'success', id: '', name: '添加' }],
      pageColumns: [
        { title: '字段名称', key: 'dbFieldName', fixed: true },
        { title: '字段备注', key: 'dbFieldTxt' },
        { title: '表单显示', key: 'isShowForm' },
        { title: '列表显示', key: 'isShowList' },
        { title: '是否只读', key: 'isReadOnly' },
        { title: '控件类型', key: 'fieldShowType' },
        { title: '控件长度', key: 'fieldLength' },
        { title: '是否查询', key: 'isQuery' },
        { title: '查询类型', key: 'queryMode' },
        { title: '扩展参数', key: 'fieldExtendJson' },
        { title: '填值规则', key: 'fieldValueRuleCode' }
      ],
      getPage: {},
      tableTypeList: [
        { label: '单表', value: '0' },
        { label: '主表', value: '1' },
        { label: '附表', value: '2' }
      ],
      iconMsg: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除' }
      ],
      iconMsgDis: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除', disabled: true }
      ],
      tableData: [],
      multipleSelection: []
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search')
      } else {
        return this.$tableHeight('noSearch')
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },

    _addField() {
      this.$emit('on-result-change', '添加')
    },
    channelInputLimit(e) {
      const key = e.key
      // 不允许输入'e'和'.'
      if (key === 'e' || key === '.') {
        e.returnValue = false
        return false
      }
      return true
    },
    // 操作的序号
    _handleRow(data) {
      this.currentRow = data.row
    },
    // 单个填写检测值
    _handleTestValueEdit(id, name) {
      if (name !== '' && undefined !== name) {
        console.log(name)
      }
    },

    _checkShowForm(id, text) {
      alert(id)
      alert(text)
    },

    _formBlank() {
      this.formObj = {}
    },
    _initTableData(data) {
      this.tableData = data
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await onlFormHead.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await onlFormHead.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await onlFormHead.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>
