<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==='fieldValidType'"
              @click.stop="_handleRow(scope)"
            >
              <span v-if="scope.row['dbFieldName']==='id'">
                <el-select slot="prepend" v-model="scope.row.fieldValidType" disabled="disabled" placeholder="请选择">
                  <el-option label="空" value="空" />
                  <el-option label="唯一校验" value="唯一校验" />
                  <el-option label="6到16位数字" value="6到16位数字" />
                  <el-option label="6到16位任意字符" value="6到16位任意字符" />
                  <el-option label="网址" value="网址" />
                  <el-option label="电子邮件" value="电子邮件" />
                  <el-option label="手机号码" value="手机号码" />
                </el-select>
              </span>
              <span v-else>
                <el-select slot="prepend" v-model="scope.row.fieldValidType" placeholder="请选择">
                  <el-option label="空" value="空" />
                  <el-option label="唯一校验" value="唯一校验" />
                  <el-option label="6到16位数字" value="6到16位数字" />
                  <el-option label="6到16位任意字符" value="6到16位任意字符" />
                  <el-option label="网址" value="网址" />
                  <el-option label="电子邮件" value="电子邮件" />
                  <el-option label="手机号码" value="手机号码" />
                </el-select>

              </span>
            </span>



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

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



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


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


            <span
              v-else-if="item.key==='dictText'"
              @click.stop="_handleRow(scope)"
            >
              <span v-if="scope.row['dbFieldName']==='id'">
                <el-input
                  v-model="scope.row.dictText"
                  size="medium"
                  placeholder=""
                  disabled="disabled"
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.dictText)"
                />
              </span>
              <span v-else>
                <el-input
                  v-model="scope.row.dictText"
                  size="medium"
                  placeholder=""
                  @blur="_handleTestValueEdit(scope.row.id,scope.row.dictText)"
                />
              </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: '字段Href', key: 'fieldHref' },
        { title: '验证规则', key: 'fieldValidType' },
        { title: '校验必填', key: 'fieldMustInput' },
        { title: '字典Table', key: 'dictTable' },
        { title: '字典Code', key: 'dictField' },
        { title: '字典Text', key: 'dictText' }
      ],
      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>
