<template>
  <ai-list class="eyeList" isTabs>
    <template slot="content">
      <ai-search-bar bottomBorder>
        <template #left>
          <el-button type="primary" icon="iconfont iconAdd" size="small" @click="add('添加设备配置', {})">添加</el-button>
          <el-button icon="el-icon-delete" class="delete-btn del-btn-list" :disabled="!ids.length" @click="remove(ids)">删除</el-button>
        </template>
        <template slot="right">
          <el-input
            v-model="search.condition"
            class="search-input"
            size="small"
            v-throttle="() => {search.current = 1, getList()}"
            placeholder="请输入CorpId"
            clearable
            @change="getList"
            @clear="search.current = 1, search.condition = '', getList()"
            suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        v-loading="loading"
        style="margin-top: 16px;"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList"
        @selection-change="v=>ids=v.map(e=>e.id)">
        <el-table-column slot="options" width="140px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="add('编辑设备配置', row)">编辑</el-button>
              <el-button type="text" @click="refresh(row)">刷新</el-button>
              <el-button type="text" @click="remove(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column slot="flag" align="center" label="状态" width="100">
          <template v-slot="{ row }">
            <el-switch v-model="row.flag" @change="onChange(row)" active-value="1" inactive-value="0"
                        active-color="#5088FF" inactive-color="#D0D4DC"></el-switch>
          </template>
        </el-table-column>
      </ai-table>
      <ai-dialog :title="dialogTitle" :visible.sync="dialog" width="800px"  @onConfirm="addForm" @closed="dialogForm={}">
        <el-form ref="addForm" :model="dialogForm" :rules="rules" size="small"  label-width="160px">
          <el-form-item label="CorpId" prop="corpId">
            <el-input v-model.trim="dialogForm.corpId" placeholder="请输入..." clearable :maxLength="50"/>
          </el-form-item>
          <el-form-item required label="状态">
             <el-radio-group v-model="dialogForm.flag">
              <el-radio :label="0">关闭</el-radio>
              <el-radio :label="1">开启</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="AppId">
            <el-input v-model.trim="dialogForm.appId" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
          <el-form-item label="RSA">
            <el-input v-model.trim="dialogForm.rsa" placeholder="请输入..." clearable :maxLength="5000"  type="textarea" :rows="5"/>
          </el-form-item>
          <el-form-item label="SECRET">
            <el-input v-model.trim="dialogForm.secret" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
          <el-form-item label="TOKEN">
            <el-input v-model.trim="dialogForm.token" placeholder="请输入..." clearable :maxLength="200"  type="textarea" :rows="2"/>
          </el-form-item>
          <el-form-item label="结点ids">
            <el-input v-model.trim="dialogForm.orgIds" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
          <el-form-item label="版本号">
            <el-input v-model.trim="dialogForm.version" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
          <el-form-item label="大喇叭账号">
            <el-input v-model.trim="dialogForm.dlbName" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
          <el-form-item label="大喇叭密码">
            <el-input v-model.trim="dialogForm.dlbPwd" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
          <el-form-item label="大喇叭Token">
            <el-input v-model.trim="dialogForm.dlbToken" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-list>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'eyeList',
    props: {
      instance: Function,
      dict: Object,
      areaId: String
    },

    data () {
      return {
        search: {
          current: 1,
          condition: '',
          size: 10,
        },
        ids: [],
        tableData: [],
        total: 0,
        loading: false,
        dialog: false,
        dialogTitle: '',
        dialogForm: {}
      }
    },

    computed: {
      ...mapState(['user']),

      param () {
        return this.search
      },
      rules() {
        return {
          corpId: [{required: true, message: "请输入CorpId"}],
        }
      },
      colConfigs() {
        return [
          {type: "selection"},
          { prop: 'corpId', label: 'CorpId', fixed: 'left', width: 220 },
          { slot: 'flag', align: 'center', label: '状态', fixed: 'left', width: 120 },
          { prop: 'appId', align: 'center', label: 'AppId', width: 240 },
          { prop: 'rsa', align: 'center', label: 'RSA', width: 240 },
          { prop: 'secret', align: 'center', label: 'SECRET', width: 180 },
          { prop: 'token', align: 'center', label: 'TOKEN', width: 120 },
          { prop: 'orgIds', align: 'center', label: '结点ids', width: 120 },
          { prop: 'version', align: 'center', label: '版本号', width: 120 },
          { prop: 'dlbName', align: 'center', label: '大喇叭账号', width: 140 },
          { prop: 'dlbPwd', align: 'center', label: '大喇叭密码', width: 140 },
          { prop: 'dlbToken', align: 'center', label: '大喇叭Token', width: 180 },
          { prop: 'createTime', align: 'center', label: '创建时间', fixed: 'right', width: 180 },
          { slot: 'options'},
        ]
      }
    },

    created () {
      this.getList()
    },
    methods: {
      getListInit() {
        this.search.current = 1
        this.getList()
      },
      getList () {
        this.instance.post(`/app/appzyaccountconfig/list`, null, {
          params: {
            ...this.search,
            type: 0
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
            this.loading = false
          } else {
            this.loading = false
          }
        }).catch(() => {
          this.loading = false
        })
      },
      remove(id) {
        this.$confirm('确定删除该数据？').then(() => {
          this.instance.post(`/app/appzyaccountconfig/delete?ids=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },
      add(title, item) {
        this.dialog = true
        this.dialogTitle = title
        this.dialogForm = item
        if(title == '添加设备配置') {
          this.dialogForm.flag = 1
          this.dialogForm.version = '1.0.0'
        }
      },
      addForm() {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            this.dialogForm.type = 0
            this.instance.post(`/app/appzyaccountconfig/addOrUpdate`, this.dialogForm).then((res) => {
              if (res.code == 0) {
                this.$message.success(`${this.dialogForm.id ? '编辑成功' : '添加成功'}`)
                this.getListInit()
                this.dialog = false;
              }
            });
          } else {
            return false;
          }
        });
      },
      refresh(row) {
        this.$confirm('确定刷新该数据token？').then(() => {
          this.instance.post(`/app/appzyaccountconfig/initQlyToken?id=${row.id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('刷新成功！')
              this.getList()
            }
          })
        })
      },
      onChange(row) {
        this.instance.post(`/app/appzyaccountconfig/setFlag`, null, {
          params: {
            id: row.id,
            flag: row.flag
          }
        }).then((res) => {
          if (res.code == 0) {
            this.$message.success(+row.flag ? '已启用' : '已禁用');
            this.getList();
          }
        })
      },
    }
  }
</script>
