<!--
 * @Description: create
 * @Version: 1.0
 * @Autor: XuTongZhang
 * @Date: 2020-07-28 16:12:21
 * @LastEditors: XuTongZhang
 * @LastEditTime: 2020-08-11 15:48:44
-->
<template>
<div class="indexPage">
    <v-input :btn="btn" :list="list" @add="add"></v-input>
    <v-table :table="table" :tableList="tableList" :sortType="true" :queryData="queryData" @details="details" @editor="editor" @del="del"></v-table>
    <v-pager @page="callPage" :total="totalrecords"></v-pager>
    <el-dialog :visible.sync="dialogFormVisible" width="650px" :before-close="close" :close-on-click-modal="false">
        <el-form :model="form" ref="form" label-width="140px" :rules="rules" label-position="left">
            <el-form-item label="场景名称" prop="sceneName">
                <el-input v-if="state!==2" v-model="form.sceneName" placeholder="请输入场景名称" autocomplete="off"></el-input>
                <div v-else>{{form.sceneName}}</div>
            </el-form-item>
            <el-form-item label="场景描述" prop="sceneIntroduce">
                <el-input v-if="state!==2" type="textarea" :rows="4" resize="none" placeholder="请输入场景描述" v-model="form.sceneIntroduce"></el-input>
                <div v-else>{{form.sceneIntroduce}}</div>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" v-if="state!==2" @click="determine">确 定</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
export default {
  data () {
    return {
      tableList: [],
      dialogFormVisible: false,
      page: 1,
      totalrecords: 0,
      form: {},
      state: 0,
      pickList: [],
      rules: {
        sceneName: [{
          required: true,
          message: '请输入场景名称',
          trigger: 'blur'
        }],
        sceneIntroduce: [{
          required: true,
          message: '请输入场景描述',
          trigger: 'blur'
        }]
      },
      list: [],
      btn: [{
        name: '添加',
        type: 'success',
        method: 'add'
      }],
      table: {
        column: [{
          label: '序号',
          props: 'serialNumber'
        },
        {
          label: '场景名称',
          props: 'sceneName'
        },
        {
          label: '场景描述',
          props: 'sceneIntroduce'
        }
        ],
        handle: [{
          title: '查看',
          method: 'details',
          type: 'info'
        },
        {
          title: '编辑',
          method: 'editor',
          type: 'warning'
        },
        {
          title: '删除',
          method: 'del',
          type: 'danger'
        }
        ]
      }
    }
  },
  created () {
    this.queryData()
  },
  methods: {
    queryData (form = {}) {
      let page = this.page
      let reqdata = form
      this.$api
        .post('/scene/querySceneList', {
          reqdata,
          page
        })
        .then((res) => {
          this.totalrecords = res.totalrecords
          // this.tableList = res.list
          this.tableList = res.list.map((item, index) => {
            item.serialNumber = (this.page - 1) * 10 + index + 1
            return item
          })
        })
    },
    add () {
      this.open()
      this.info = {}
      this.state = 0
    },
    getDetails (row) {
      let reqdata = {
        id: row.id
      }
      this.$api
        .post('/scene/querySceneDetail', {
          reqdata
        })
        .then((res) => {
          this.open()
          this.form = res.object
        })
    },
    details (row) {
      this.getDetails(row)
      this.state = 2
    },
    editor (row) {
      this.getDetails(row)
      this.state = 1
    },
    del (row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let reqdata = {
          id: row.id
        }
        this.$api
          .post('/scene/deleteScene', {
            reqdata
          })
          .then((res) => {
            this.queryData()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    determine () {
      let a
      this.$refs['form'].validate((valid) => {
        a = valid
      })
      if (!a) return
      let url = this.state ? '/scene/updateScene' : '/scene/saveScene'
      let reqdata = this.form
      this.$api
        .post(url, {
          reqdata
        })
        .then((res) => {
          this.close()
          this.queryData()
        })
    },
    open () {
      this.dialogFormVisible = true
    },
    close () {
      this.dialogFormVisible = false
      this.form = {}
    },
    callPage (val) {
      this.page = val
      this.queryData()
    }
  },
  watch: {
    dialogFormVisible: function (val, oldVla) {
      if (this.state === 2) {
        if (this.$refs['form'] !== undefined) {
          this.$refs['form'].resetFields()
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
