<template>
  <div class="queryTemplate">
    <Search title="编辑查询模板" :is-search="false">
      <button class="btnAner">测试</button>
      <button class="btnAn" @click="editSubmit">保存</button>
    </Search>
    <el-form ref="ruleForm" style="margin-top: 10px" :inline="true" :model="ruleForm" label-width="auto" class="demo-ruleForm">
      <el-row :gutter="24" class="b24">
        <el-col :span="6">
          <el-form-item label="查询模板名称" prop="queryTemplateName">
            <el-input v-model="ruleForm.queryTemplateName" size="medium" placeholder="请输入查询模板名称" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="数据源" prop="dataSourceID">
            <el-select v-model="ruleForm.dataSourceID" placeholder="请选择" clearable>
              <el-option v-for="item in dataSourceList" :key="item.dataSourceID" :label="item.dataSourceName" :value="item.dataSourceID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="服务名称" prop="serviceID">
            <el-select v-model="ruleForm.serviceID" placeholder="请选择" clearable>
              <el-option v-for="item in serviceList" :key="item.serviceID" :label="item.serviceName" :value="item.serviceID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="每页显示" prop="pageRows">
            <el-input v-model="ruleForm.pageRows" size="medium" placeholder="请输入每页显示行数" type="number" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="查询模板描述" prop="queryTemplateDescribe">
            <el-input v-model="ruleForm.queryTemplateDescribe" size="medium" placeholder="请输入查询模板描述" clearable />
          </el-form-item>
        </el-col>
        <!-- <el-form-item
        label="查询配置"
        style="margin-bottom: 24px"
      >
        <el-input
          v-model="ruleForm.queryTemplatetConfig"
          class="desc"
          type="textarea"
          resize="none"
          :autosize="{ minRows: 4, maxRows: 4 }"
          placeholder="描述内容···"
          size="small"
        />
      </el-form-item> -->
        <el-col :span="24">
          <el-form-item label="查询语句">
            <el-input v-model="ruleForm.queryTemplate" class="desc" type="textarea" resize="none" :autosize="{ minRows: 26, maxRows: 26 }" placeholder="描述内容···" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import Search from '@/layout/components/Search/index.vue'
import { GeneralDataReception, myQuery } from '@/api/dataIntegration'
export default {
  components: {
    Search
  },
  data () {
    return {
      queryTemplateID: null,
      ruleForm: {
        queryTemplateName: '',
        queryTemplateDescribe: '',
        serviceID: null,
        queryTemplate: '',
        pageRows: null
      },
      dataSourceList: [],
      serviceList: [],
      rules: {
        queryTemplateName: [{ required: true, message: '请输入查询模板名称', trigger: 'blur' }],
        // protocolID: [{ required: true, message: '请输入查询协议', trigger: 'blur' }],
        queryTemplateDescribe: [{ required: true, message: '请输入查询模板描述', trigger: 'blur' }],
        queryTemplatetConfig: [{ required: true, message: '请选查询配置', trigger: 'change' }],
        queryTemplate: [{ required: true, message: '请选择查询语句', trigger: 'change' }]
      }
    }
  },
  created () {
    // this.ruleForm = this.$route.query
    if (this.$route.query.queryTemplateID == null) {
      this.$router.push('/systemSettings/queryTemplate')
      return
    }
    this.queryTemplateID = Number(this.$route.query.queryTemplateID)
    this.ruleForm.queryTemplateID = this.queryTemplateID
    this.ruleForm.event = 2
    this.queryQueryTemplateByID(this.queryTemplateID)
    this.getDataSourceList()
    this.queryServiceList()
  },
  methods: {
    async editSubmit () {
      const params = {
        ...this.ruleForm,
        submitID: this.ruleForm.serviceID
      }
      delete params.serviceID
      try {
        const res = await GeneralDataReception({
          serviceId: SERVICE_ID.sysQueryTabId,
          dataContent: JSON.stringify(params)
        })
        if (Number(res.code) === 0) {
          this.$message.success(res.message ?? '成功')
          this.$router.push('/systemSettings/queryTemplate')
        } else {
          this.$message.error(res.message ?? '失败')
        }
      } catch (error) {
        console.log('错误', error)
      }
    },
    async queryQueryTemplateByID (queryTemplateID) {
      try {
        const templateInfo = await myQuery(DATACONTENT_ID.sysQueryEditId, queryTemplateID)
        Object.entries(templateInfo[0]).forEach(([key, value]) => {
          this.ruleForm[key] = value
        })
      } catch (error) {
        this.$message.error(error)
      }
    },
    async getDataSourceList () {
      const dataSourceList = await this.getSelectOption(DATACONTENT_ID.sysSourceId)
      this.dataSourceList = dataSourceList.map(item => {
        item[item.setlabel] = item.k
        item[item.setvalue] = item.v
        return item
      })
    },
    async queryServiceList () {
      const serviceList = await this.getSelectOption(DATACONTENT_ID.sysServiceTabId)
      this.serviceList = serviceList.map(item => {
        item[item.setlabel] = item.k
        item[item.setvalue] = item.v
        return item
      })
    },
    async getSelectOption (ID) {
      try {
        const listValues = await myQuery(ID)
        return listValues
      } catch (error) {
        this.$message.error(error)
        return []
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.queryTemplate {
  padding: 20px 30px 0 24px;
  height: 87vh;
  background: #ffffff;
  border-radius: 4px;
  .btnAner {
    width: 64px;
    height: 32px;
    background: #ffffff;
    border: 1px solid #9ebbf7;
    border-radius: 4px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #2d67e3;
    margin-right: 16px;
  }
  ::v-deep .demo-ruleForm {
    > .el-row > .el-col {
      .el-form-item {
        margin-bottom: 24px;
        // margin-right: 0px;
        .el-input,
        .el-select {
          width: 100%;
          // color: #101116;
        }
        input {
          &::-webkit-outer-spin-button,
          &::-webkit-inner-spin-button {
            -webkit-appearance: none !important;
          }
          &[type="number"] {
            -moz-appearance: textfield !important;
          }
        }
      }
    }
  }
  .b24 {
    ::v-deep .el-input__inner {
      height: 32px;
    }
  }
  ::v-deep .el-form-item__content {
    width: calc(100% - 96px);
  }
  ::v-deep .el-form-item {
    width: 100%;
  }
  // .desc {
  //   ::v-deep .el-input__inner {
  //     height: 96px;
  //   }
  // }
}
</style>
