<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="tableData"
      :element-loading-text="elementLoadingText"
      :height="height"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      >

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">

            <el-form-item label="用户名:">
              <span>{{ props.row.username }}</span>
            </el-form-item>

            <el-form-item label="密码:">
              <span>{{ props.row.password }}</span>
            </el-form-item>

            <el-form-item label="权限URL:">
              <span>{{ props.row.comurl }}</span>
            </el-form-item>

            <el-form-item label="下载URL:">
              <span>{{ props.row.downloadurl }}</span>
            </el-form-item>

            <el-form-item label="回传URL:">
              <span>{{ props.row.backurl }}</span>
            </el-form-item>

            <el-form-item label="文件位置:">
              <span>{{ props.row.localFilePath }}</span>
            </el-form-item>

            <el-form-item label="回传位置:">
              <span>{{ props.row.backFilePath }}</span>
            </el-form-item>

            <el-form-item label="秘钥过期:">
              <span>{{ props.row.keyTime }}</span>
            </el-form-item>


            <el-form-item label="通讯秘钥:" >
              <el-input   type="textarea" v-model="props.row.comKey" :disabled="true"  style="width: 300%"></el-input>
            </el-form-item>

          </el-form>
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="序号" width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="场站名称" prop="name"  />
      <el-table-column show-overflow-tooltip label="场站编码" prop="stationCode" />
      <el-table-column show-overflow-tooltip label="场站标识" prop="signCode" />
      <el-table-column show-overflow-tooltip label="一体化公司" prop="inCode" />

      <el-table-column show-overflow-tooltip label="场站类型" prop="type" :formatter="formatType" />
      <el-table-column show-overflow-tooltip label=" 推送天数" prop="days" />

      <el-table-column show-overflow-tooltip label="操作" width="180px">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            <el-tag type="success">编辑</el-tag>
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="page.currentPage"
      :layout="layout"
      :page-size="page.pageSize"
      :total="page.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>

    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="500px"
      @close="close"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="场站名称" prop="name">
          <el-input v-model.trim="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="场站编码" prop="stationCode">
          <el-input
            v-model.trim="form.stationCode"
            :disabled="isEdit"
            autocomplete="off"
          ></el-input>
        </el-form-item>


        <el-form-item label="场站标识" prop="signCode">
          <el-input v-model.trim="form.signCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="场站类型" prop="type">
          <el-select v-model.trim="form.type"  :disabled="isEdit" clearable placeholder="请选择">
            <el-option
              v-for="item in elType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="公司" prop="inCode">
          <el-input v-model.trim="form.inCode" autocomplete="off" :disabled="isEdit"></el-input>
        </el-form-item>


        <el-form-item label="用户名" prop="username">
          <el-input v-model.trim="form.username" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model.trim="form.password" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="权限URL" prop="password">
          <el-input v-model.trim="form.comurl" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="下载URL" prop="password">
          <el-input v-model.trim="form.downloadurl" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="回传URL" prop="password">
          <el-input v-model.trim="form.backurl" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="文件位置" prop="localFilePath">
          <el-input v-model.trim="form.localFilePath" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="回传文件位置" prop="backFilePath">
          <el-input v-model.trim="form.backFilePath" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="推送天数" prop="days">
          <el-input-number
            v-model.trim="form.days"
            :min="1"
            :max="15"
            label="推送天数"
          ></el-input-number>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { fetchList, addObj, putObj } from '@/api/station'

  export default {
    name: 'Station',

    data() {

      return {
        isEdit: false,
        dialogFormVisible: false,
        dialogType: '',
        title: '',
        tableData: [],
        companys: [],
        elType: [
          { label: '光电', value: 'E1' },
          { label: '风电', value: 'E2' },
        ],
        searchForm: {
          stationCode: null,
        },
        form: {},
        imgShow: true,
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条
        },
        rules: {
          name: [{ required: true, trigger: 'blur', message: '请输入标题' }],
          type: [{ required: true, trigger: 'blur', message: '请输入作者' }],
          stationCode: [
            {
              required: true,
              trigger: 'blur',
              message:'不能为空'

            },
          ],
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      async fetchData() {
        this.listLoading = true
        fetchList(
          Object.assign(
            {
              current: this.page.currentPage,
              size: this.page.pageSize,
            },
            this.searchForm
          )
        )
          .then((response) => {
            this.tableData = response.data.records
            this.page.total = response.data.total
            this.listLoading = false
          })
          .catch(() => {
            this.listLoading = false
          })
      },

      handleSizeChange(val) {
        this.page.pageSize = val
        this.page.currentPage = 1
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.page.currentPage = val
        this.fetchData()
      },
      handleQuery() {
        for (var v in this.searchForm) {
          if (this.searchForm[v] == '') {
            delete this.searchForm[v]
          }
        }

        this.page.currentPage = 1
        this.fetchData()
      },
      formatType(row, column) {
        for (let i = 0; i < this.elType.length; i++) {
          if (row.type == this.elType[i].value) {
            return this.elType[i].label
          }
        }
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('fetch-data')
      },
      handleAdd() {
        this.isEdit = false
        this.form = {}
        this.title = '新增'
        this.dialogType = 'add'
        this.dialogFormVisible = true
      },
      handleEdit(row) {
        this.isEdit = true
        var newObj = JSON.parse(JSON.stringify(row))
        this.form = newObj
        this.title = '修改'
        this.dialogType = 'edit'
        this.dialogFormVisible = true
      },

      saveOrUpdate() {
        if (this.dialogType == 'add') {
          this.save()
        } else {
          this.update()
        }
      },
      save() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.listLoading = true
            addObj(this.form)
              .then((response) => {
                this.dialogFormVisible = false
                this.fetchData()
                this.listLoading = false
              })
              .catch(() => {
                this.listLoading = false
              })
          } else {
            return false
          }
        })
      },
      update() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.listLoading = true
            putObj(this.form)
              .then((response) => {
                this.dialogFormVisible = false
                this.fetchData()
                this.listLoading = false
              })
              .catch(() => {
                this.listLoading = false
              })
          } else {
            return false
          }
        })
      },
    },
  }
</script>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
