<template>
  <!-- 钢瓶档案 -->
  <div>
    <actionBar menuTitle="钢瓶档案" :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm"
      @openModel="openModel" @searchProtocol="searchProtocol">
    </actionBar>
    <tables :key="Math.random()" :suspension="true" :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="cylinderDialog" width="800px" :close-on-click-modal="false"
      @close="closeDialog">
      <forms ref="childRules" :formNewList="cylinderRule" :ruleForm="cylinderForm" labelWidth="160px"></forms>
      <span slot="footer" class="dialog-footer" v-if="operationType != 'logs'">
        <el-button plain @click="cylinderDialog = false">取 消</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getGasCylinder,
    getGasCylinderDetails,
    addGasCylinder,
    putGasCylinder,
    delGasCylinder,
  } from '@/api/inspection'
  import actionBar from '@/components/actionBar'
  import tables from '@/components/tables'
  import pagination from '@/components/pagination'
  import forms from '@/components/forms'
  import {
    employee,
    cylinderformRules
  } from "./cylinder.js";
  export default {
    components: {
      actionBar,
      tables,
      pagination,
      forms
    },
    data() {
      return {
        formList: [{
          type: 'input',
          label: '单位内编号',
          field: 'innerCode',
          placeholder: '单位内编号',
        }, ],
        searchRuleForm: {
          innerCode: '',
        },
        operateList: [{
          type: 'add',
          title: '添加钢瓶',
          icon: 'el-icon-plus',
        }],
        tableList: employee(),
        tableData: [],
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        Total: 0,
        staffTitle: '添加钢瓶',
        searchValue: {},
        cylinderDialog: false,
        cylinderRule: cylinderformRules(),
        cylinderForm: {},
        confirmLoading: false,
        operationType: '',
        clientId: '',
      }
    },
    mounted() {
      this.getList()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.searchValue = value
        this.getList()
      },
      // 获取钢瓶档案列表
      getList() {
        var params = {
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        getGasCylinder(params).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list
            this.Total = res.data.count
          }
        })
      },
      // 获取钢瓶档案详情
      getDetails(innerCode) {
        this.loading = true
        getGasCylinderDetails(innerCode).then(res => {
          if (res.code == 200) {
            this.ruleForm = res.data
          }
          this.loading = false
        })
      },
      openModel(type) {
        this.operationType = type
        if (type == 'add') {
          this.staffTitle = '添加钢瓶'
        }
        const arr = [...this.cylinderRule]
        arr.forEach((item, index) => {
          item.disabled = false
        })
        this.cylinderRule = arr
        this.cylinderDialog = true
      },
      buttonData(row, type) {
        if (type == 'logs') {
          this.cylinderForm = row
          this.staffTitle = '钢瓶详情'
          const arr = [...this.cylinderRule]
          arr.forEach((item, index) => {
            item.disabled = true
          })
          this.cylinderRule = arr
          this.cylinderDialog = true
          // this.getDetails(row.inner_code)
        } else if (type == 'edit') {
          this.cylinderForm = row
          this.staffTitle = '编辑钢瓶'
          this.clientId = row.id
          this.cylinderDialog = true
        } else if (type == 'del') {
          this.getCancelOrder(row.id)
        }
        this.operationType = type
      },
      // 弹窗表单添加
      handleAdd() {
        let flag = this.$refs['childRules'].validateForm();
        if (flag) {
          var params = {
            ...this.cylinderForm,
          }
          if (this.operationType == 'edit') {
            params.id = this.clientId
            putGasCylinder(params).then(res => {
              if (res.code == 200) {
                this.getList()
                this.cylinderDialog = false
              }
            })
          } else {
            addGasCylinder(params).then(res => {
              if (res.code == 200) {
                this.getList()
                this.cylinderDialog = false
              }
            })
          }
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      // 删除钢瓶
      getCancelOrder(ID) {
        this.$confirm('此操作将永久删除该钢瓶, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delGasCylinder({
            id: ID,
          }).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getList()
            }
          })
        }).catch(() => {
          // console.log('取消')
        });
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
      closeDialog() {
        const arr = [...this.cylinderRule]
        arr.forEach((item, index) => {
          item.disabled = false
        })
        this.cylinderRule = arr
        this.cylinderForm = {}
        this.$refs['childRules'].resetCheck();
      }
    }
  }
</script>

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