<template>
  <ai-list class="videoList" 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="!Boolean(ids.length)" @click="remove(ids)">删除</el-button>
        </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="edit('编辑设备配置', 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>
      </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="用户ID">
            <el-input v-model.trim="dialogForm.slwUserId" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
          <el-form-item label="地区编码">
            <el-input v-model.trim="dialogForm.slwAreaId" placeholder="请输入..." clearable :maxLength="50" />
          </el-form-item>
          <el-form-item label="TOKEN">
            <el-input v-model.trim="dialogForm.slwToken" 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,
          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 },
          { prop: 'flag', align: 'center', label: '状态', fixed: 'left', width: 120 },
          { prop: 'slwUserId', align: 'center', label: '用户ID', width: 180 },
          { prop: 'slwAreaId', align: 'center', label: '地区编码', width: 180 },
          { prop: 'slwToken', align: 'center', label: 'TOKEN', width: 220 },
          { 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: '创建时间', 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: 1
          }
        }).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
        }
      },
      addForm() {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            this.dialogForm.type = 1
            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/initSlwToken?id=${row.id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('刷新成功！')
              this.getList()
            }
          })
        })
      },
    }
  }
</script>
