<template>
  <div>
    <el-row class="handle-box" style="margin-bottom: 10px">
      <el-col :span="24">
        <el-input v-model="search.name" class="ch-input ch-input-size" placeholder="分公司名称" size="small" @keyup.enter.native="handleSearch()" />
        <el-button class="ch-button-warning" size="small" @click="handleReset()"><i class="el-icon-search" />&nbsp;重置</el-button>
        <el-button class="ch-button" size="small" @click="handleSearch()"><i class="el-icon-search" />&nbsp;搜索</el-button>
        <el-button class="ch-button-export" size="small" @click="confirmOutput()"><i class="el-icon-download" />&nbsp;导出</el-button>
        <el-button class="ch-button" size="small" style="float: right;margin-top: 4px" @click="handleAdd()"><i class="el-icon-menu" />&nbsp;新增</el-button>
      </el-col>
    </el-row>
    <el-row class="handle-box">
      <el-col :span="24">
        <el-table v-loading="loading" :data="AllData" row-class-name="g_table_row" stripe>
          <el-table-column type="index" width="60" />
          <el-table-column label="组织机构" prop="name" />
          <el-table-column label="编码" prop="code" />
          <el-table-column label="顺序号" prop="sequenceNo" />
          <el-table-column label="说明" prop="comments" />
          <el-table-column header-align="center" label="操作" width="180">
            <template scope="scope">
              <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDel(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-page">
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :total="allpage"
            background
            layout="total, prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="dialogTitle" class="statistic_base" :visible.sync="dialogVisible" top="50px" width="75%" append-to-body @open="dlgOpen">
      <el-form ref="form" :model="form" :rules="rules" style="width: 100%;padding: 5px">
        <el-row>
          <el-col style="padding-bottom: 10px">
            <el-card shadow="always" style="padding: 15px 5px 5px 15px">
              <el-row>
                <el-col :span="3" class="col-txt"><span>组织机构</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-input v-model="form.name" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="3" class="col-txt"><span>编码</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-input v-model="form.code" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="3" class="col-txt"><span>顺序号</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-input v-model="form.sequenceNo" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="3" class="col-txt"><span>说明</span></el-col>
                <el-col :span="20" class="col-input">
                  <el-form-item>
                    <el-input v-model="form.comments" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-card>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Base from '@/views/base/base'
import BaseData from '@/views/base/baseData'

export default {
    name: 'Group',
    mixins: [Base, BaseData],
    data() {
        return {
            dc_key: [],
            // 列表相关
            search: {
                name: ''
            },
            AllData: [],
            loading: false,
            // 弹框相关
            dialogVisible: false,
            dialogTitle: '新增',
            isAdd: true,
            form: this.initForm(),
            rules: {}
        }
    },
    mounted() {
        this.getData()
    },
    methods: {
        getData: function() {
            const _this = this
            _this.loading = true
            _this.AllData = []

            this.search.pageNum = this.currentPage
            this.search.pageSize = this.pageSize
            this.baseRequest('list', this.search).then((res) => {
                if (res.data.rows) {
                    res.data.rows.forEach(function(item) {
                        const json = _this.getItemJson(item)
                        _this.AllData.push(json)
                    })
                    _this.allpage = res.data.total
                }
                _this.loading = false
            }).catch(() => {
            })
            this.initOutData()
        },
        handleSearch: function() {
            this.getData()
        },
        handleReset: function() {
            for (const i in this.search) {
                if (i !== 'pageNum' && i !== 'pageSize') {
                    this.search[i] = ''
                }
            }
            this.handleSearch()
        },
        initOutData: function() {
            const _this = this
            this.OutData = []
            const title = [' 组织机构', ' 编码', ' 顺序号', ' 说明']
            this.OutData.push(title)

            this.baseRequest('listAll', this.search).then((res) => {
                if (res.data) {
                    res.data.forEach(function(item) {
                        const jsonMap = _this.getItemJson(item)
                        const jsonArray = []
                        jsonArray.push(jsonMap.name)
                        jsonArray.push(jsonMap.code)
                        jsonArray.push(jsonMap.sequenceNo)
                        jsonArray.push(jsonMap.comments)

                        _this.OutData.push(jsonArray)
                    })
                }
            }).catch(() => {
            })
        },
        getItemJson: function(item) {
            return item
        },
        initForm: function() {
            return {
                id: '',
                name: '',
                comments: '',
                code: '',
                sequenceNo: ''
            }
        },
        confirmOutput() {
            const OutSize = [{ wch: 15 }, { wch: 15 }, { wch: 15 }]
            const fileName = 'group导出' + new Date().Format('yyyyMMddhhmm')
            this.$outputXlsxFile(this.OutData, OutSize, fileName)
        },
        dlgOpen: function() {
            const _this = this

            if (_this.form.id !== '') {
                const postData = {
                    id: _this.form.id
                }
                this.baseRequest('getById', postData).then((res) => {
                    if (res.data) {
                        _this.form = Object.assign({}, _this.form, res.data)
                    }
                }).catch(() => {
                })
            }
        },
        /* 编辑*/
        handleEdit: function(val) {
            this.isAdd = false
            this.form.id = val.id
            this.dialogVisible = true
            this.dialogTitle = '编辑'
        },
        /* 新增*/
        handleAdd: function() {
            this.form = this.initForm()
            this.isAdd = true
            this.dialogVisible = true
            this.dialogTitle = '新增'
        },
        confirmSubmit: function() {
            const _this = this

            this.$refs.form.validate(valid => {
                if (valid) {
                    let soaUrl = 'edit'
                    const extraData = {}
                    const postData = Object.assign({}, _this.form, extraData)

                    if (this.isAdd) {
                        soaUrl = 'add'
                    }
                    this.opRecord(postData, soaUrl)
                } else {
                    // console.log('error submit!!')
                    return false
                }
            })
        },
        baseRequest(opUrl, postData) {
            return this.$channel.globeRequest('SysGroupController', opUrl, postData, 'project')
        }
    }
}
</script>

<style scoped>
    .ch-input .el-input__inner {
        border-color: #32323A;
    }
    .ch-input-size {
        width: 150px;
    }
    .ch-button {
        border-color: #32323A;
        background-color: #32323A;
        color: #fff;
    }
    .ch-button-warning {
        margin-left: 10px;
        border-color: #E6A23C;
        background-color: #E6A23C;
        color: #fff;
    }
    .ch-button-export {
        margin-left: 10px;
        border-color: #98CC1F;
        background-color: #98CC1F;
        color: #fff;
    }
    /deep/.el-dialog__header {
        padding: 10px 20px;
    }
    /deep/.el-dialog__body {
        padding: 10px 20px;
    }
</style>
