<template>
  <div class="security">
    <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 v-show="flag" :data="tableData" :list="tableList" :total="total" :loading="loading" @sizeChange="onSizeChange" @currentChange="onCurrentChange" @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>
        <div v-show="data.item === 'turnStatus' && data.scope.turnStatus !== null">
          {{ data.scope.turnStatus === 1 ? '不启用' : '启用' }}
        </div>
        <div v-show="(data.scope.sendF1 || data.scope.sendF2 || data.scope.sendF3) && data.item === 'frequency'">
          {{ data.scope.sendF1 + '条/1分钟' + data.scope.sendF2 + '条/1小时' + data.scope.sendF3 + '条/1天' }}
        </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-select v-model="tableItem.turnStatus">
            <el-option label="启用" :value="0" />
            <el-option label="不启用" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="发送速率">
          <el-input v-model="tableItem.subspeed" :disabled="true" />
        </el-form-item>
        <el-form-item class="frequency" label="发送频率">
          <el-input v-model="tableItem.sendF1" /><span>条/1分钟</span>
          <el-input v-model="tableItem.sendF2" /><span>条/1小时</span>
          <el-input v-model="tableItem.sendF3" /><span>条/1天</span>
        </el-form-item>
        <el-form-item label="发送时段">
          <el-time-picker
            v-model="timeData"
            is-range
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
            format="HH:mm:ss"
            value-format="HH:mm:ss"
            @input="changeTime"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

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

export default {
  name: 'Security',

  components: {
    Table
  },

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

      tableData: [],
      tableList: [
        {
          id: 1,
          label: '序号',
          prop: 'uid',
          flag: false
        },
        {
          id: 2,
          label: '单位',
          prop: 'parentId',
          flag: true
        },
        {
          id: 3,
          label: '短信账号',
          prop: 'apiName',
          flag: false
        },
        {
          id: 4,
          label: '关键词',
          prop: '',
          flag: false
        },
        {
          id: 5,
          label: '白名单',
          prop: '',
          flag: false
        },
        {
          id: 6,
          label: '黑名单',
          prop: '',
          flag: false
        },
        {
          id: 7,
          label: '携号转网',
          prop: 'turnStatus',
          flag: true
        },
        {
          id: 8,
          label: '发送时段',
          prop: 'sendTime',
          flag: false
        },
        {
          id: 9,
          label: '发送速率',
          prop: 'subspeed',
          flag: false
        },
        {
          id: 10,
          label: '发送频率',
          prop: 'frequency',
          flag: true
        },
        {
          id: 11,
          label: '创建时间',
          prop: 'createtime',
          flag: false
        }
      ],
      tableItemList: [],
      tableItem: {},

      flag: false,
      loading: false,
      dialogVisible: false
    }
  },

  created() {
    this.getCustCustApiList()
    this.flag = true
  },

  methods: {
    async getCustCustApiList() {
      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.getCustCustApiList()
    },
    onSelectionChange(value) {
      this.tableItemList = value
    },
    onEdit() {
      if (this.tableItemList.length < 1 || this.tableItemList.length > 1) {
        return this.$message({
          message: '只能选中一项哦',
          type: 'warning'
        })
      }
      this.tableItem = {
        unitName: this.tableItemList[0].unitName,
        apiName: this.tableItemList[0].apiName,
        id: this.tableItemList[0].id,
        subspeed: this.tableItemList[0].subspeed,
        sendF1: this.tableItemList[0].sendF1,
        sendF2: this.tableItemList[0].sendF2,
        sendF3: this.tableItemList[0].sendF3,
        turnStatus: this.tableItemList[0].turnStatus
      }
      this.timeData = this.tableItemList[0].sendTime.split('-')
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
    },
    async onSubmit() {
      await custCustApiUpdateAPI(this.tableItem)
      this.getCustCustApiList()
      this.dialogVisible = false
    },
    onCurrentChange(value) {
      this.page.offset = value
      this.getCustCustApiList()
    },
    onSizeChange(value) {
      this.page.limit = value
      this.getCustCustApiList()
    },
    changeTime(value) {
      this.tableItem.sendTime = value.join('-')
    }
  }
}
</script>

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