<template>
  <div style="padding: 20px;">
    <el-form inline>
      <el-form-item label="区域">
        <el-input v-model="name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="success" @click="add">新 增 区 域</el-button>
      </el-form-item>
    </el-form>
    <br>
    <el-table
      height="600"
      :data="list"
      border>
      <el-table-column label="区域名称" prop="functionName"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="open(scope.row)" size="mini" type="primary">添加下级功能区</el-button>
          <el-button @click="edit(scope.row)" size="mini" >编辑</el-button>
          <el-popconfirm
            title=" 确定要删除这个功能区吗？"
            @confirm="del(scope.row)"
          >
            <el-button style="margin-left: 10px" type="danger" size="mini" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <table-footer
      :page-num="pageNum"
      :totals="total"
      :size="pageSize"
      @sizeChange="sizeChange"
      @pageChange="pageChange"></table-footer>
    <el-dialog
      :title="showType ? '编 辑' : '新 增'"
      :visible.sync="dialogVisible"
      @close="reset"
      width="20%">
      <div>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
          <el-form-item label="功能名称" prop="functionName">
            <el-input v-model="form.functionName"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
          <el-button @click="reset">取 消</el-button>
          <el-button type="primary" @click="onSubmit('form')">{{showType ? '更 新' : '新 增'}}</el-button>
        </span>
    </el-dialog>
    <el-dialog title="选择功能区" :visible.sync="showModal" @close="modalReset">
      <el-table  ref="multipleTable" :data="functionList" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column label="功能区名称" prop="functionName"></el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
          <el-button @click="modalReset">取 消</el-button>
          <el-button type="primary" :disabled="isButton" @click="onCheckSave">保存</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>


import {getFunctionMonitList, getAreaFunctionList, addAreaFunction, editAreaFunction, delAreaFunction, saveCameraFunctionArea, getCameraFunctionArea} from "../../utils/api";
import TableFooter from "../../components/TableFooter";

export default {
  name: "FunctionArea",
  components: {
    TableFooter
  },
  data() {
    return {
      name: '',
      pageNum: 1,
      pageSize: 20,
      list: [],
      total: 0,
      showType: false,
      dialogVisible: false,
      rules: {
        functionName: [ { required: true, message: '区域名称不能为空', trigger: 'blur' } ],
      },
      form: {
        functionName: '',
        remark: '',
      },
      functionList: [],
      showModal: false,
      isButton: true,
      selectIdList: [],
      selectId: '',
    }
  },
  methods: {
    // 修改size
    sizeChange(val) {
      this.pageSize = val;
      this.init();
    },
    // 修改页数
    pageChange(val) {
      this.pageNum= val;
      this.init();
    },
    init() {
      let params = {
        current: this.pageNum,
        size: this.pageSize,
        functionName: this.name,
      }
      getAreaFunctionList(params).then(res => {
        if(res.code === 10000) {
          this.list = res.data.records;
          this.total = res.data.total;
        }
      })
    },
    search() {
      this.pageNum = 1;
      this.init()
    },
    reset() {
      this.form = {
        functionName: '',
        remark: '',
      }
      this.dialogVisible = false;
      this.showType = false;
    },
    add() {
      this.dialogVisible = true;
      this.showType = false;
    },
    edit(row) {
      this.dialogVisible = true;
      this.showType = true;
      this.form = {
        id: row.id,
        functionName: row.functionName,
        remark: row.remark
      }
    },
    del(row) {
      let params = {
        ids: row.id
      }
      delAreaFunction(params).then(res => {
        if(res.code === 10000) {
          this.init();
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.showType) {
            // 编辑
            let params = {
              id: this.form.id,
              functionName: this.form.functionName,
              remark: this.form.remark
            }
            editAreaFunction(params).then(res => {
              this.reset()
              if (res.code === 10000) {
                this.init();
                this.$message.success(res.message)
              } else {
                this.$message.error(res.message)
              }
            })
          } else {
            // 新增
            let params = {
              functionName: this.form.functionName,
              remark: this.form.remark
            }
            addAreaFunction(params).then(res => {
              if(res.code === 10000) {
                this.init();
                this.$message.success(res.message);
              } else {
                this.$message.error(res.message)
              }
            }).finally(() => {
              this.reset()
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },

    initFunction() {
      let params = {
        current: 1,
        size: 2000
      }
      getFunctionMonitList(params).then(res => {
        if(res.code === 10000) {
          this.functionList = res.data.records;
        }
      })
    },
    handleSelectionChange(val) {
      this.isButton = false;

      let arr = []
      val.forEach(item => {
        arr.push(item.id)
      })

      this.selectIdList = arr;
    },
    open(row) {
      this.selectId = row.id;
      this.showModal = true;
      let params = {
        id: row.id
      }
      getCameraFunctionArea(params).then(res => {
        if(res.code === 10000) {
          if(res.data.length > 0) {
            let list = [];
            this.functionList.forEach(item => {
              res.data.forEach(val => {
                if(item.id == val) {
                  list.push(item)
                }
              })
            })
            if(list) {
              list.forEach((row) => {
                this.$refs.multipleTable.toggleRowSelection(row, true)
              })
            }
          }
        }
      })
    },
    modalReset() {
      this.showModal = false;
      this.selectIdList = [];
      this.selectId = '';
      this.$refs.multipleTable.clearSelection();
    },
    onCheckSave() {
      let params = {
        functionAreaId: this.selectId,
        functionIds: this.selectIdList.join(',')
      }
      saveCameraFunctionArea(params).then(res => {
        if(res.code === 10000) {
          this.$message.success(res.message);
        } else {
          this.$message.error(res.message);
        }
      }).finally(() => {
        this.modalReset()
      })
    }

  },
  mounted() {
    this.init()
    this.initFunction()
  }
}
</script>

<style scoped>

</style>
