<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="弹窗名称：">
        <el-input v-model="form.name" placeholder="请输入弹窗名称" />
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态">
          <el-option v-for="item in disabledOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" plain @click="getDialog('新增')"
          v-hasPermi="['operation:dialog:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="弹窗名称" prop="name" align="center" show-overflow-tooltip />
      <el-table-column label="弹窗图片" align="center" width="100px">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" />
        </template>
      </el-table-column>
      <el-table-column label="关联设备" align="center" :formatter="devFormatter" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog('查看', scope.row.id)">查看</el-button>
          <el-button v-if="scope.row.status === 0" type="text" @click="getChange(scope.row, 1, '下架')"
            v-hasPermi="['operation:dialog:down']">下架</el-button>
          <span v-else style="margin-left: 10px">
            <el-button type="text" @click="getDialog('编辑', scope.row.id)"
              v-hasPermi="['operation:dialog:edit']">编辑</el-button>
            <el-button type="text" @click="getChange(scope.row, 0, '上架')"
              v-hasPermi="['operation:dialog:up']">上架</el-button>
            <el-button type="delete" @click="getChange(scope.row, 2, '删除')"
              v-hasPermi="['operation:dialog:delete']">删除</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" :before-close="getClose">
      <el-form label-width="auto" :disabled="title === '查看' ? true : false">
        <el-form-item label="弹窗名称：">
          <el-input v-model="dialogForm.name" placeholder="请输入弹窗名称" />
        </el-form-item>
        <el-form-item label="关联设备：">
          <el-select v-model="dialogForm.deviceIds" multiple filterable placeholder="请选择关联设备">
            <el-option v-for="item in devOptions" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="弹窗图片：">
          <Upload listType="picture-card" :url="dialogForm.pic" @upload="upload"
            :disabled="title === '查看' ? true : false" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="getClose">取消</el-button>
        <el-button v-if="title !== '查看'" type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, detail, change, devList, submit } from '@/api/operation/dialog'
import { disabledMixin } from '@/mixin/index'
export default {
  mixins: [disabledMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数据
      total: 0,
      // 列表
      tableData: [],
      // 弹窗
      dialogVisible: false,
      // 弹窗名称
      title: '新增',
      // 弹窗表单
      dialogForm: {},
      // 设备列表
      devOptions: [],
      allDevs: []
    }
  },
  mounted() {
    this.getList()
    this.getDev(1)
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },

    // 上下架
    getChange(row, status, title) {
      this.$confirm(`是否${title}${row.name}?`, '提示', {
        type: 'warning'
      }).then(res => {
        change(row.id, status).then(res => {
          if (res.code === 0) {
            this.$message.success(`${title}成功！`)
            this.getList()
          }
        })
      }).catch(() => { })
    },

    // 弹窗
    getDialog(title, id) {
      this.dialogVisible = true
      this.title = title
      this.getDev(2)
      if (id) {
        detail(id).then(res => {
          if (res.code === 0) {
            this.dialogForm = res.data
            this.dialogForm.deviceIds = res.data.deviceIds.split(',')
            res.data.existDeviceList.map(i => {
              this.devOptions.unshift({
                value: i.clientTypeId,
                label: i.name
              })
            })
          }
        })
      }
    },

    // 设备列表
    getDev(type) {
      this.devOptions = []
      devList(type).then(res => {
        if (res.code === 0) {
          res.data.map(i => {
            let obj = {
              value: i.clientTypeId,
              label: i.name
            }
            type === 1 ? this.allDevs.push(obj) : this.devOptions.push(obj)
          })
        }
      })
    },

    // 上传
    upload(e) {
      this.dialogForm.pic = e.file
    },

    // 关闭
    getClose() {
      this.dialogVisible = false
      this.dialogForm = {}
    },

    // 确定
    getSubmit() {
      this.dialogForm.deviceIds = this.dialogForm.deviceIds.join(',')
      submit(this.dialogForm).then(res => {
        if (res.code === 0) {
          this.$message.success('提交成功！')
          this.getClose()
          this.getList()
        }
      })
    },

    // 字典翻译
    devFormatter(row) {
      return row.deviceIds ? row.deviceIds.split(',').map(i => this.selectDictLabel(this.allDevs, i)).join('，') : ''
    },

    statusFormatter(row) {
      return this.selectDictLabel(this.disabledOptions, row.status)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>