<template>
  <div class="app-container">
    <div class="app-container">
      <div class="filter-container">
        <el-button v-if="isWorkerAdd" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="form_add">新增</el-button>
      </div>
      <el-table
        v-loading="table.listLoading"
        element-loading-text="给我一点时间"
        :data="table.list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable"
        row-key="id"
        default-expand-all
        @sort-change="tableSort"
      >
        <el-table-column label="序号" align="center" type="index" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + (table.pageNum-1) * table.pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="类别名称" min-width="110px" header-align="center" align="center" prop="typeName" />
        <el-table-column label="顺序" min-width="110px" sortable header-align="center" align="center" prop="sort" />
        <el-table-column label="启用" min-width="80px" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enable" disabled active-color="#13ce66" inactive-color="#ff4949" :active-value="'1'" :inactive-value="'0'" @change="handleEnableChange(scope.$index, scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" header-align="center" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button v-if="isWorkerUpdate" type="primary" size="mini" @click="form_edit(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="table.total>0" :total="table.total" :page.sync="table.getdataListParm.offset" :limit.sync="table.getdataListParm.pagecount" @pagination="getList" />

      <el-dialog :title="textMap[dialogStatus]" :visible.sync="create.dialogFormVisible" :close-on-click-modal="false" width="50%">
        <el-form ref="createTemp" :rules="rules" :model="create.temp" label-position="right" label-width="150px">
          <el-form-item label="维修工类别:" prop="typeName">
            <el-row>
              <el-col :span="12">
                <el-input ref="typeName" v-model="create.temp.typeName" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="顺序:" prop="sort">
            <el-row>
              <el-col :span="12">
                <el-input ref="sort" v-model="create.temp.sort" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="启用:" prop="enable">
            <el-row>
              <el-col :span="12">
                <el-switch ref="enable" v-model="create.temp.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="'1'" :inactive-value="'0'" />
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="dialogStatus=='create'" type="success" @click="add_dialog_save_again()">保存并新增</el-button>
          <el-button type="primary" @click="dialogStatus=='create'?add_dialog_save():edit_dialog_save()">保存并关闭</el-button>
          <el-button @click="create.dialogFormVisible = false;getList()">取消并关闭</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import { PostDataByName, GetDataByName, GetDataByNames, checkButtons, failproccess } from '@/api/common'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { sortChange } from '@/utils/index.js'
import { isIntegerZero } from '@/utils/validate'
export default {
  name: 'MaintenanceWorkerCategory',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      isWorkerAdd: [], isWorkerUpdate: [],
      table: {
        tableKey: 0, total: 0, listLoading: true, list: [], pageNum: 0, pageSize: 10,
        getdataListParm: {
          name: 'getMaintence', page: 1, offset: 1, pagecount: 10, returntype: 'Map',
          parammaps: {}
        }
      },
      requestParam: { name: 'createapisql', offset: 0, pagecount: 0, parammaps: [] },
      create: {
        temp: { typeName: '', sort: '', enable: '1' }, dialogFormVisible: false
      },
      dialogStatus: '',
      rules: {
        typeName: [{ type: 'string', required: true, message: '必填', trigger: 'change' }],
        sort: [{ validator: isIntegerZero, trigger: 'blur' }]
      },
      textMap: { update: '编辑', create: '新增' },
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' }
    }
  },
  created() {
    this.getList()
    this.get_auto_buttons()
  },

  methods: {
    tableSort(column) {
      sortChange(column, this.list)
    },
    get_auto_buttons() {
      // 新增
      const WorkerAdd = 'basic:worker:add'
      const isWorkerAdd = checkButtons(this.$store.state.user.buttons, WorkerAdd)
      this.isWorkerAdd = isWorkerAdd
      // 编辑
      const WorkerUpdate = 'basic:worker:update'
      const isWorkerUpdate = checkButtons(this.$store.state.user.buttons, WorkerUpdate)
      this.isWorkerUpdate = isWorkerUpdate
    },
    getList() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        this.table.list = response.data.list
        this.table.pageNum = response.data.pageNum
        this.table.pageSize = response.data.pageSize
        if (response.data.total) {
          this.table.total = response.data.total
        }
        setTimeout(() => {
          this.table.listLoading = false
        }, 1000)
      })
    },
    form_add() {
      this.create.temp = { typeName: '', sort: 0, enable: '1' }
      this.dialogStatus = 'create'
      this.create.dialogFormVisible = true
    },
    add_dialog_save() {
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          this.requestParam = {}
          this.requestParam.name = 'insertMaintenanceType'
          this.requestParam.parammaps = this.create.temp
          PostDataByName(this.requestParam).then(response => {
            if (response.msg !== 'fail') {
              this.getList()
              this.create.dialogFormVisible = false
              this.$notify({ title: '成功', message: '新增成功', type: 'success', duration: 2000 })
            } else {
              failproccess(response, this.$notify)
            }
          })
        }
      })
    },
    add_dialog_save_again() {
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          this.requestParam = {}
          this.requestParam.name = 'insertMaintenanceType'
          this.requestParam.parammaps = this.create.temp
          PostDataByName(this.requestParam).then(response => {
            if (response.msg !== 'fail') {
              this.create.temp = { typeName: '', sort: '', enable: '1' }
              this.$notify({ title: '成功', message: '新增成功', type: 'success', duration: 2000 })
            } else {
              failproccess(response, this.$notify)
            }
          })
        }
      })
    },
    form_edit(row) {
      this.create.temp = Object.assign(row, {})
      this.dialogStatus = 'update'
      this.create.dialogFormVisible = true
    },
    edit_dialog_save() {
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          this.requestParam = {}
          this.requestParam.name = 'updateMaintenanceType'
          this.requestParam.parammaps = this.create.temp
          PostDataByName(this.requestParam).then(response => {
            if (response.msg !== 'fail') {
              this.create.dialogFormVisible = false
              this.$notify({ title: '成功', message: '新增成功', type: 'success', duration: 2000 })
            } else {
              failproccess(response, this.$notify)
            }
          })
        }
      })
    }
  }
}
</script>
