<template>
  <ed-drawer ref="BasicDialog" :title="title"
          :visible="visible"
          @close="closeDialog"
          @ok="saveFee">
    <el-card class="box-card">
      <el-form ref="form" :model="form"
               class="demo-form-inline">
        <el-form-item label="收费方式"
                      prop="fee_type">
          <el-select v-model="form.fee_type">
            <el-option label='测试' value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用(条/元)"
                      prop="fee">
          <el-input-number v-model="form.fee" :min="0.01" :step="0.01"/>
        </el-form-item>
      </el-form>
    </el-card>
  </ed-drawer>
</template>

<script>
import {getOneData, updateData, insertData} from "../../../../api/easyData";

export default {
  name: "InterfaceFee",
  data() {
    return {
      dataId: '',
      title: '',
      row: {},
      visible: false,
      form: {fee_type: '', fee: ''},
    }
  },
  methods: {
    show(row, title) {
      this.row = row
      this.title = title
      this.visible = true
      this.initRequestData()
    },
    initRequestData() {
      let param = {
        dataSourceName: 'interface_fee',
        wheres: [{
          fieldName: 'interface_id',
          fieldValue: this.row.id,
          operator: '='
        }]
      }
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
      });
      getOneData(param).then((res) => {
        if (res.success) {
          if (res.data) {
            let model = {}
            let keyInfo = res.data.keyInfoObject
            this.dataId = res.data.id
            model.fee_type = keyInfo.fee_type
            let dataInfo = res.data.dataInfoArray
            model.fee = dataInfo[0].fee
            this.form = Object.assign(model, {})
          }
        } else {
          this.$message.warning(res.msg);
        }
      }).finally(() => {
        loading.close()
      })
    },
    closeDialog() {
      this.visible = false
    },
    saveFee() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
          });
          let keyInfo = {}
          keyInfo.interface_id = this.row.id
          keyInfo.fee_type = this.form.fee_type
          let param = {
            dataSourceName: 'interface_fee',
            key: keyInfo
          }
          let data = {}
          data.fee = this.form.fee
          let dataInfo = []
          dataInfo.push(data)
          param.data = dataInfo
          if (this.dataId === '' || typeof (this.dataId) == "undefined") {
            insertData(param).then((res) => {
              if (res.success) {
                this.$message.success("保存成功");
              } else {
                this.$message.error(res.msg);
              }
            }).finally(() => {
              loading.close()
            })
          } else {
            param.id = this.dataId
            updateData(param).then((res) => {
              if (res.success) {
                this.$message.success("保存成功");
              } else {
                this.$message.error(res.msg);
              }
            }).finally(() => {
              loading.close()
            })
          }

        }
      })
    },
  }
}
</script>

<style scoped>
</style>
