<template>
  <div class="flowrate">
    <div class="header">
      <el-form :inline="true">
        <el-button type="primary" @click="onEdit()">修改</el-button>
        <el-button type="primary" @click="onSearch()">查询</el-button>
        <el-form-item>
          <el-select v-model="page.parentId">
            <el-option label="全部单位名称" value="" />
            <el-option label="短信服务中心" value="66" />
            <el-option label="市场部" value="96" />
            <el-option label="博物馆" value="99" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <Table :judgment="judgment" :data="tableData" :list="tableList" :total="total" :loading="loading" @sizeChange="onSizeChange" @selectionChange="onSelectionChange">
      <template #type="data">
        <div v-show="data.item === 'parentId'">
          <span v-show="data.scope.parentId === 66">短信服务中心</span>
          <span v-show="data.scope.parentId === 96">市场部</span>
          <span v-show="data.scope.parentId === 99">博物馆</span>
        </div>
      </template>
    </Table>
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form>
        <el-form-item label="单位">
          <el-select v-model="tableItem.parentId" :disabled="true">
            <el-option label="短信服务中心" :value="66" />
            <el-option label="市场部" :value="96" />
            <el-option label="博物馆" :value="99" />
          </el-select>
        </el-form-item>
        <el-form-item label="短信账户">
          <el-input v-model="tableItem.apiName" :disabled="true" />
        </el-form-item>
        <el-form-item label="日流量">
          <el-input v-model="tableItem.dflux" />
        </el-form-item>
        <el-form-item label="月流量">
          <el-input v-model="tableItem.mflux" />
        </el-form-item>
        <el-form-item label="年流量">
          <el-input v-model="tableItem.yflux" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onChangeEdit()">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { custCustApiListAPI, custCustApiUpdateAPI } from '@/api/Account/FlowRate'
import Table from '@/components/Table/index.vue'

export default {
  name: 'FlowRate',

  components: {
    Table
  },

  data() {
    return {
      page: {
        limit: 10,
        offset: 0,
        parentId: ''
      },
      total: 0,

      tableData: [],
      tableList: [
        {
          id: 1,
          label: '序号',
          prop: 'uid'
        },
        {
          id: 2,
          label: '单位',
          prop: 'parentId',
          flag: true
        },
        {
          id: 3,
          label: '短信账号',
          prop: 'apiName',
          flag: false
        },
        {
          id: 4,
          label: '日流量',
          prop: 'dflux',
          flag: false
        },
        {
          id: 5,
          label: '月流量',
          prop: 'mflux',
          flag: false
        },
        {
          id: 6,
          label: '年流量',
          prop: 'yflux',
          flag: false
        },
        {
          id: 7,
          label: '年流量',
          prop: 'createtime',
          flag: false
        }
      ],
      tableItemList: [],
      tableItem: {},

      loading: false,
      dialogVisible: false,
      judgment: true
    }
  },

  created() {
    this.custCustApiList()
  },

  methods: {
    async custCustApiList() {
      this.loading = true
      const res = await custCustApiListAPI(this.page)
      this.tableData = res.rows.map((item, index) => {
        return { ...item, uid: index + 1 }
      })
      this.total = res.total
      this.loading = false
    },
    onSearch() {
      this.custCustApiList()
    },
    handleClose() {
      this.dialogVisible = false
    },
    onEdit() {
      if (this.tableItemList.length < 1 || this.tableItemList.length > 1) {
        return this.$message({
          message: '只能选中一项哦',
          type: 'warning'
        })
      }
      this.tableItem = {
        parentId: this.tableItemList[0].parentId,
        id: this.tableItemList[0].id,
        unitName: this.tableItemList[0].unitName,
        apiName: this.tableItemList[0].apiName,
        chargingMethod: this.tableItemList[0].chargingMethod,
        cashtype: this.tableItemList[0].cashtype,
        subspeed: this.tableItemList[0].subspeed,
        mobiletrieslimit: this.tableItemList[0].mobiletrieslimit,
        priority: this.tableItemList[0].priority,
        dflux: this.tableItemList[0].dflux,
        mflux: this.tableItemList[0].mflux,
        yflux: this.tableItemList[0].yflux
      }
      this.dialogVisible = true
    },
    async onChangeEdit() {
      await custCustApiUpdateAPI(this.tableItem)
      this.custCustApiList()
      this.dialogVisible = false
    },
    onSelectionChange(value) {
      this.tableItemList = value
    },
    onSizeChange(value) {
      this.page.limit = value
      this.custCustApiList()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
