<template>
  <!-- 设备管理 -->
  <div>
    <actionBar menuTitle="设备管理" :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm"
      @openModel="openModel" @searchProtocol="searchProtocol"></actionBar>
    <tables :key="Math.random()" :suspension="false" :tableList="tableList" :tableData="tableData"
      @buttonData="buttonData"></tables>
    <!-- 分页 -->
    <div class="paging_bottom" v-if="Total">
      <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
        @changeCurrent="changeCurrent">
      </pagination>
    </div>
    <el-dialog :title="staffTitle" :visible.sync="staffDialogVisible" width="600px" :close-on-click-modal="false"
      @close="closeDialog">
      <forms ref="childRules" :formNewList="formRuleList" :ruleForm="ruleForm" labelWidth="100px"
        @handleScroll="handleScroll" @remoteMethod="remoteMethod"></forms>
      <span slot="footer" class="dialog-footer">
        <el-button plain @click="staffDialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getUser
  } from '@/api/user'
  import {
    getCylinder,
    addCylinder,
    putCylinder,
    delCylinder,
  } from '@/api/facility'
  import actionBar from '@/components/actionBar'
  import tables from '@/components/tables'
  import pagination from '@/components/pagination'
  import forms from '@/components/forms'
  import {
    formRules,
    employee
  } from "./facility.js";
  export default {
    components: {
      actionBar,
      tables,
      pagination,
      forms
    },
    data() {
      return {
        staffTitle: '添加',
        staffDialogVisible: false,
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        operateList: [{
          type: 'add',
          title: '添加',
          icon: 'el-icon-plus',
        }],
        formList: [{
          type: 'input',
          label: 'SN',
          field: 'sn',
          placeholder: 'SN',
        }],
        searchRuleForm: {
          sn: '',
        },
        tableList: employee(),
        tableData: [],
        Total: 0,
        formRuleList: [],
        operationType: '',
        searchValue: {},
        ruleForm: {
          sn: '',
          type: 1,
          optType: '',
          provUserId: '',
        },
        enterpriseId: null,
        confirmLoading: false,
        page: 1,
        staffName: '',
        limitNo: true,
      }
    },
    mounted() {
      const dataList = formRules();
      this.formRuleList = dataList;
      this.getList()
    },
    methods: {
      // 获取用户列表
      getUserList() {
        var params = {
          page: this.page,
          pageSize: 10,
          name: this.staffName,
        }
        getUser(params).then(res => {
          if (res.code == 200) {
            let arr = res.data.list
            let arrList = []
            arr.forEach(item1 => {
              var arrData = {
                label: null,
                value: null,
              }
              arrData.label = item1.nickName
              arrData.value = item1.provUserId
              arrList.push(arrData)
            })
            if (this.limitNo == true) {
              this.formRuleList.forEach(item => {
                if (item.type == 'searchSelect') {
                  item.options = item.options.concat(arrList);
                  let some = [];
                  item.options.forEach(el => {
                    if (!some.some(e => e.value == el.value)) {
                      some.push(el)
                    }
                  })
                  item.options = some
                }
              })
            }
            if (arrList.length >= 10) {
              this.page = ++this.page;
            } else {
              // 已经没数据了 不需要增加数据
              this.limitNo = false;
            }
          }
        })
      },
      // 触底事件
      handleScroll() {
        if (this.limitNo) {
          this.getUserList()
        }
      },
      // 重置选择员工
      resetSelect() {
        this.formRuleList.forEach(item => {
          if (item.type == 'searchSelect') {
            item.options = []
          }
        })
        this.page = 1
        this.limitNo = true
        this.staffName = ''
      },
      // 搜索
      remoteMethod(value) {
        this.resetSelect()
        this.staffName = value
        this.getUserList()
      },
      // 搜索
      searchProtocol(value) {
        this.searchValue = value
        this.getList()
      },
      // 获取设备列表
      getList() {
        var params = {
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        getCylinder(params).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list
            this.Total = res.data.count
          }
        })
      },
      openModel(type) {
        this.operationType = type
        if (type == 'add') {
          this.staffTitle = '添加'
          this.ruleForm.type = 1
        }
        this.resetSelect()
        this.getUserList()
        this.staffDialogVisible = true
      },
      // 弹窗表单添加
      handleAdd() {
        let flag = this.$refs['childRules'].validateForm();
        if (flag) {
          this.confirmLoading = true
          if (this.operationType == 'add') {
            var params = {
              ...this.ruleForm
            }
            addCylinder(params).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
              this.staffDialogVisible = false
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
          } else if (this.operationType == 'edit') {
            this.editItem()
          }
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      editItem() {
        var params = {
          id: this.enterpriseId,
          ...this.ruleForm
        }
        putCylinder(params).then(res => {
          if (res.code == 200) {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.getList()
          }
          this.staffDialogVisible = false
          this.confirmLoading = false
        }).catch(() => {
          this.confirmLoading = false
        })
      },
      // 删除
      deleteItem(id) {
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delCylinder({
            id: id,
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getList()
            }
          })
        }).catch(() => {});
      },
      buttonData(row, type) {
        this.enterpriseId = row.id
        this.operationType = type
        if (type == 'edit') {
          this.resetSelect()
          this.staffDialogVisible = true
          this.staffTitle = '编辑'
          const arrList = JSON.parse(JSON.stringify(row))
          this.$nextTick(() => {
            this.ruleForm.sn = arrList.sn
            this.ruleForm.type = arrList.type
            this.ruleForm.optType = arrList.optType
            this.ruleForm.provUserId = arrList.provUserId
            this.formRuleList.forEach(item => {
              if (item.type == 'searchSelect') {
                const arrData = {
                  label: null,
                  value: null,
                }
                arrData.label = arrList.user.nickName
                arrData.value = arrList.user.provUserId
                item.options.push(arrData)
              }
            })
            this.getUserList()
          })
        } else if (type == 'del') {
          this.deleteItem(row.id)
        }
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
      closeDialog() {
        this.$refs['childRules'].resetCheck();
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
