<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline"
            v-if="checkPermission(['enterprise:add'])" @click="handleAdd">添加</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" placeholder="请输入企业名称" @advancedSearch="advancedSearchEvent" advanced>
            <template slot="advanced-content">
              <el-form ref="advancedForm" :model="advancedForm" label-width="90px">
                <el-form-item label="编号">
                  <el-input v-model="advancedForm.sn"  placeholder="请输入企业编号"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                  <el-input v-model="advancedForm.mobile"  placeholder="请输入手机号"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input v-model="advancedForm.email"  placeholder="请输入邮箱"></el-input>
                </el-form-item>
                <el-form-item label="纳税人识别号">
                  <el-input v-model="advancedForm.tax_num"  placeholder="请输入纳税人识别号"></el-input>
                </el-form-item>
              </el-form>
            </template>
          </en-table-search>
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column prop="name" label="企业名称" />
        <el-table-column prop="sn" label="企业编号" />
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="tax_num" label="纳税人识别号" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" v-if="checkPermission(['enterprise:update'])"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger"
              v-if="checkPermission(['enterprise:del'])"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
    <el-dialog :title="editForm.id ? '编辑企业' : '添加企业'" :visible.sync="dialogVisible" width="900px"
      @open="handleDialogOpen" :modal-append-to-body="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="80px" inline>
        <el-form-item label="企业名称" prop="name" >
          <el-input v-model="editForm.name" :maxlength="30" clearable placeholder="请输入企业名称"></el-input>
        </el-form-item>
        <el-form-item label="企业编号" prop="sn">
          <el-input v-model="editForm.sn" :maxlength="20" clearable placeholder="请输入企业编号"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="editForm.mobile"  clearable
            placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" :maxlength="30" clearable placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="邮编" prop="zip_code">
          <el-input v-model="editForm.zip_code" :maxlength="30" clearable placeholder="请输入邮编"></el-input>
        </el-form-item>
        <el-form-item label="识别号" prop="tax_num">
          <el-input v-model="editForm.tax_num" :maxlength="30" clearable placeholder="请输入纳税人识别号"></el-input>
        </el-form-item>
        <el-form-item label="开户银行" prop="bank_name">
          <el-input v-model="editForm.bank_name" :maxlength="30" clearable placeholder="请输入开户银行"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="editForm.address" :maxlength="30" clearable placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="linkman">
          <el-input v-model="editForm.linkman" :maxlength="30" clearable placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="link_phone">
          <el-input v-model="editForm.link_phone" :maxlength="30" clearable placeholder="请输入联系电话"></el-input>
        </el-form-item>
        <el-form-item label="传真" prop="fax">
          <el-input v-model="editForm.fax" :maxlength="30" clearable placeholder="请输入传真"></el-input>
        </el-form-item>
        <el-form-item label="银行账号" prop="bank_account">
          <el-input v-model="editForm.bank_account" :maxlength="30" clearable placeholder="请输入银行账号"></el-input>
        </el-form-item>
        <el-form-item label="银行户名" prop="bank_account_name">
          <el-input v-model="editForm.bank_account_name" :maxlength="30" clearable placeholder="请输入银行户名"></el-input>
        </el-form-item>
        <div>
          <el-form-item label="备注" prop="remark" class="form-item-remark">
            <el-input v-model="editForm.remark" type="textarea" :autosize="{ minRows: 3, maxRows: 4 }"
                      placeholder="请输入备注" class="form-item-remark-input" style="width: 250%"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as API_Enterprise from '@/api/enterprise'
import { RegExp } from '~/ui-utils'
export default {
  name: 'enterprise',
  data() {
    return {
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 列表数据
      tableData: '',
      // 编辑表单
      editForm: {},
      // 搜索表单
      advancedForm: {},
      // 编辑表单 规则
      editFormRules: {
        name: [
          this.MixinRequired('企业名称必填！'),
          { min: 2, max: 30, message: '长度在 2 到 30 个字符' }
        ],
        mobile: [
          {
            validator: (rule, value, callback) => {
              if (value && !RegExp.mobile.test(value)) {
                callback(new Error('手机号格式错误！'))
              } else {
                callback()
              }
            }
          }
        ],
        email: [
          {
            validator: (rule, value, callback) => {
              if (value && !RegExp.email.test(value)) {
                callback(new Error('邮箱格式错误！'))
              } else {
                callback()
              }
            }
          }
        ]
      },
      // 编辑表单 dialog
      dialogVisible: false
    }
  },
  watch: {
  },
  mounted() {
    this.GET_List()
  },
  methods: {
    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      this.params.page_no = 1
      this.GET_List()
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_List()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_List()
    },

    /** 添加 */
    handleAdd() {
      this.editForm = {}
      this.dialogVisible = true
    },

    /** 编辑 */
    handleEdit(index, row) {
      this.editForm = this.MixinClone(row)
      this.dialogVisible = true
    },
    /** 删除 */
    handleDelete(index, row) {
      this.$confirm('确定要删除吗？', '提示', { type: 'warning' }).then(() => {
        API_Enterprise.del(row.id).then(() => {
          this.$message.success('删除成功！')
          this.GET_List()
        })
      }).catch(() => { })
    },
    /** dialog打开后重置form表单校验结果 */
    handleDialogOpen() {
      setTimeout(() => { this.$refs['editForm'].clearValidate() })
    },

    /** 提交表单 */
    submitEditForm() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          const { id } = this.editForm
          const params = this.MixinClone(this.editForm)
          if (id) {
            API_Enterprise.edit(id, params).then(response => {
              this.dialogVisible = false
              this.$message.success('修改成功！')
              this.GET_List()
            })
          } else {
            API_Enterprise.add(params).then(() => {
              this.dialogVisible = false
              this.$message.success('添加成功！')
              this.GET_List()
            })
          }
        } else {
          this.$message.error('表单填写有误，请核对！')
          return false
        }
      })
    },

    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        name: data
      }
      this.params.page_no = 1
      this.GET_List()
    },

    /** 获取分页列表 */
    GET_List() {
      this.loading = true
      API_Enterprise.getList(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
