<template>
  <div class="app-container">
    <el-button
      type="primary"
      size="mini"
      style="margin-bottom:20px"
      @click="handleAdd()"
    >新增部门</el-button>
    <el-table :data="list" border style="width: 100%">

      <el-table-column label="部门名称" prop="name" />
      <el-table-column label="部门名称(藏语)" prop="nameZ" />

      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <span>{{
            scope.row.createDate | parseTime("{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="最后更新时间">
        <template slot-scope="scope">
          <span>{{
            scope.row.lastUpdateDate | parseTime("{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" align="center" width="280px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            round
            style="font-size:14px;padding:10px 20px"
            @click="jumpStaff(scope.row)"
          >人员管理</el-button>
          <el-button
            size="mini"
            type="primary"
            round
            style="font-size:14px;padding:10px 20px"
            @click="handleAdd(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            round
            style="font-size:14px;padding:10px 20px"
            @click="handleDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-if="total > 0"
      :total="total"
      :page.sync="listQuery.lastPageIndex"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />

    <el-dialog
      class="dialog"
      :title="diaTitle"
      append-to-body
      center
      width="434px"
      :visible.sync="diaVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :close="closeDia"
    >
      <div class="preview-container">
        <el-form
          ref="form"
          label-position="top"
          :rules="rules"
          :model="form"
        >
          <el-form-item label="部门名称" prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item label="部门名称(藏语)" prop="nameZ">
            <el-input
              v-model="form.nameZ"
              placeholder="请输入"
            />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="default" @click="closeDia">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { list, add, del } from '@/api/dept'
export default {
  components: { Pagination },
  data() {
    return {
      total: 0,
      listQuery: {
        lastPageIndex: 1,
        // params: {},
        size: 10
      },
      list: undefined,

      diaTitle: '',
      diaVisible: false,
      form: {
        name: undefined,
        nameZ: undefined
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'change' }]
      }
    }
  },
  watch: {
    'listQuery.lastPageIndex': {
      handler(val) {
        this.getList()
      },
      deep: true // true 深度监听
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      list(this.listQuery).then(res => {
        // console.log(res);
        this.list = res.d.records
        this.total = res.d.total
        this.pageSize = res.d.pageSize
        // if (!res.d.lastPageIndex) {
        //   this.listQuery.lastPageIndex;
        // }
      })
    },
    handleAdd(item) {
      if (item) {
        this.diaTitle = '编辑部门'
        this.form = JSON.parse(JSON.stringify(item))
      } else {
        this.diaTitle = '新增部门'
        this.form = {
          name: undefined,
          nameZ: undefined
        }
      }
      this.$nextTick(() => {
        const formEl = this.$refs['form']
        if (formEl) formEl.clearValidate()
        this.diaVisible = true
      })
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          add(this.form).then(() => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.closeDia()
            this.getList()
          })
        }
      })
    },
    handleDel(item) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del({ id: item.id }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      })
    },
    jumpStaff(item) {
      const { name, id } = item
      this.$router.push({
        path: '/dept/staff',
        query: {
          name,
          id
        }
      })
    },
    closeDia() {
      this.diaVisible = false
    }
  }
}
</script>

<style></style>
