<template>
  <div class="app-container">
    <buttons funid="sys_dept" @editCreate="editCreate" @editDelete="editDelete" @editSave="editSave" @upload="upload" />
    <el-card>
      <el-table
        ref="deptTable"
        v-loading="loading"
        :data="data"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        @cell-dblclick="cellDblclick"
      >
        <template v-for="(d,i) in tableHeader">
          <el-table-column v-if="d.type && d.type === 'selection'" :key="i" :type="d.type" :fixed="d.fixed" />
          <el-table-column
            v-else
            :key="i"
            :prop="d.prop"
            :label="d.label"
          >
            <template slot-scope="scope">
              <div v-if="d.label==='是否注销'">
                {{
                  scope.row.sys_dept__is_novalid == 0 ? '否' : '是'
                }}
              </div>
              <div v-else-if="d.label === '操作'">
                <el-button icon="el-icon-view" type="text" title="编辑" @click="edit(scope.row)" />
                <el-button v-if="scope.row.status !== 'NULLIFY'" icon="el-icon-delete" style="color:#F56C6C" type="text" title="删除" @click="Delete(scope.row)" />
              </div>
              <div v-else>{{ scope.row[d.prop] }}</div>
            </template>
          </el-table-column>
        </template>
      </el-table>
      <el-pagination
        :current-page="pager.pageNo"
        :page-sizes="[10, 30, 50, 100, 500]"
        :page-size="pager.pageSize"
        :total="pager.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </el-card>
    <el-dialog v-if="dialogFormVisible" title="新增部门" :visible.sync="dialogFormVisible" @close="closeDialog">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="所属部门" :label-width="formLabelWidth">
          <el-select v-model="dept_id" placeholder="请选择">
            <el-option
              v-for="item in data"
              :key="item.sys_dept__dept_id"
              :label="item.sys_dept__dept_name"
              :value="item.sys_dept__dept_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门编码" :label-width="formLabelWidth" prop="dept_code">
          <el-input v-model="form.dept_code" />
        </el-form-item>
        <el-form-item label="部门名称" :label-width="formLabelWidth" prop="dept_name">
          <el-input v-model="form.dept_name" />
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="memo">
          <el-input v-model="form.memo" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="create">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog v-if="dialogEditVisible" title="部门" :visible.sync="dialogEditVisible" @close="closeDialog">
      <AdutiDept :id="parent_id" ref="auditForm" :audit-form="auditForm" :data="data" @change="auditFormChange" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from './api'
import buttons from '@/components/Buttons'
import AdutiDept from './components/auditDept'
export default {
  name: 'Guide',
  components: {
    buttons,
    AdutiDept
  },
  data() {
    return {
      loading: false,
      data: [],
      ids: [],
      pager: {
        pageNo: 0,
        pageSize: 10,
        total: 0
      },
      tableHeader: [
        {
          prop: 'selection',
          type: 'selection',
          fixed: 'left'
        }, {
          prop: 'sys_dept__dept_code',
          label: '组织编码'
        }, {
          prop: 'sys_dept__dept_name',
          label: '组织名称'
        }, {
          prop: 'sys_dept__memo',
          label: '备注'
        }, {
          prop: 'sys_dept__is_novalid',
          label: '是否注销'
        },
        {
          prop: 'opration',
          label: '操作',
          width: '70px',
          fixed: 'right',
          minWidth: '70px',
          show: true
        }],
      value: '',
      dept_id: '',
      id: '',
      parent_id: '',
      form: {
        dept_name: '',
        dept_code: '',
        memo: ''
      },
      rules: {
        dept_code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' }
        ],
        dept_name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }

        ]
      },
      dialogFormVisible: false,
      dialogEditVisible: false,
      formLabelWidth: '120px',
      auditForm: {},
      saveFrom: {}
    }
  },
  created() {
    this.getList()
  },
  mounted() {
  },
  methods: {
    getList() {
      this.loading = true
      let pageNo = this.pager.pageNo * this.pager.pageSize - this.pager.pageSize
      if (pageNo < 0) {
        pageNo = 0
      }
      api.getDept(
        this.pager.pageSize,
        pageNo
      ).then(data => {
        if (data.success) {
          this.data = data.data.root
          this.pager.total = data.data.total
          setTimeout(() => {
            this.loading = false
          }, 200)
        } else {
          this.$message.error(data.message)
        }
      })
    },
    editCreate() {
      if (this.ids === null) {
        this.$message.warning('请选择一个组织再添加下属组织')
      } else if (this.ids.length !== 1) {
        this.$message.warning('请选择一个组织再添加下属组织')
      } else {
        this.dialogFormVisible = true
        this.dept_id = this.ids[0]
      }
    },
    create() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const data = `funid= sys_dept&parentId= ${this.dept_id}&levelCol= sys_dept.dept_level&keyid= &pagetype= editgrid&eventcode= save_eg&sys_dept__dept_code= ${this.form.dept_code}&sys_dept__dept_name= ${this.form.dept_name}&sys_dept__memo= ${this.form.memo}&sys_dept__is_novalid= 0&sys_dept__dept_id= &sys_dept__dept_level= &user_id= administrator&dataType= json`
          api.Crerte(data).then(data => {
            if (data.success) {
              this.getList()
              this.dialogFormVisible = false
              this.$refs['form'].resetFields()
              this.form.dept_name = ''
              this.form.dept_code = ''
              this.$message.success('新增成功！')
            } else {
              this.$message.error(data.message)
            }
          })
        }
      })
    },
    Delete(row) {
      this.ids = []
      this.ids.push(row.sys_dept__dept_id)
      this.editDelete()
    },
    editDelete() {
      if (this.ids && this.ids.length > 0) {
        this.$confirm('确认删除部门？').then(() => {
          api.Delete(this.ids).then(data => {
            if (data.success) {
              this.getList()
              this.$message.success('删除成功！')
            } else {
              this.$message.error(data.message)
            }
          })
        }).catch(() => {})
      } else {
        this.$message.warning('请选择数据进行删除')
      }
    },
    editSave() {
      console.log('editSave')
    },
    upload() {
      console.log('upload')
    },
    edit(row) {
      this.id = row.sys_dept__dept_id
      this.parent_id = this.id.substring(0, this.id.length - 4)
      console.log(this.parent_id, this.id)
      this.auditForm = row
      this.dialogEditVisible = true
    },
    auditFormChange(form) {
      this.saveFrom = form
    },
    save() {
      console.log(this.saveFrom, 'this.saveFrom')
      console.log(this.auditForm, 'this.auditForm')
      if (Object.keys(this.saveFrom).length === 0) {
        this.saveFrom = this.auditForm
      }
      this.$refs.auditForm.$refs.auditForm.validate(valid => {
        if (valid) {
          const _form = `funid=sys_dept&parentId=&levelCol=sys_dept.dept_level&keyid=${this.id}&pagetype=editgrid&eventcode=save_eg&sys_dept__dept_code=${this.saveFrom.sys_dept__dept_code}&sys_dept__dept_name=${this.saveFrom.sys_dept__dept_name}&sys_dept__memo=${this.saveFrom.sys_dept__memo}&sys_dept__is_novalid=${this.saveFrom.sys_dept__is_novalid}&sys_dept__dept_id=${this.id}&sys_dept__dept_level=3&user_id=administrator&dataType=json`
          api.auditSave(_form).then(data => {
            if (data.success) {
              this.getList()
              this.$message.success('保存成功！')
              this.dialogEditVisible = false
            } else {
              this.$message.error(data.message)
            }
          })
        }
      })
    },
    cellDblclick(row) {
      console.log(row, 'row')
      this.id = row.sys_dept__dept_id
      this.auditForm = row
      this.saveFrom = []
      this.dialogEditVisible = true
    },
    sizeChange(size) {
      this.pager.pageSize = size
      this.getList()
    },
    pageChange(page) {
      this.pager.pageNo = page
      this.getList()
    },
    handleSelectionChange(val) {
      this.ids = val.map(d => d.sys_dept__dept_id)
    },
    closeDialog() {
      this.dialogFormVisible = false
      this.$refs['form'].resetFields()
      this.form.dept_name = ''
      this.form.dept_code = ''
    }
  }
}
</script>
<style lang="scss" scoped>
  .el-card {
    margin-top: 10px;
  }
</style>
