<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" v-show="showSearch" label-width="82px">
      <el-form-item label="组织名称" prop="orgName">
        <el-input v-model="queryParams.orgName" placeholder="请输入组织名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="组织类别" prop="orgType">
        <el-select v-model="queryParams.orgType" placeholder="请选择组织类别" clearable>
          <el-option v-for="item in orgTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="组织状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择组织状态" clearable>
          <el-option v-for="item in enableStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['risk:org:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['risk:org:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['risk:org:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="orgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="组织名称" align="center" prop="orgName" :show-overflow-tooltip="true" />
      <el-table-column label="组织类别" align="center" prop="orgType" width="120">
        <template slot-scope="scope">
          {{ optionLabel(orgTypeOptions, scope.row.orgType) }}
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="leader" width="120" />
      <el-table-column label="联系电话" align="center" prop="phone" width="140" />
      <el-table-column label="电子邮箱" align="center" prop="email" :show-overflow-tooltip="true" />
      <el-table-column label="机构状态" align="center" prop="status" width="110">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : 'info'" size="mini">
            {{ optionLabel(enableStatusOptions, scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['risk:org:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['risk:org:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="720px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="96px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="组织名称" prop="orgName">
              <el-input v-model="form.orgName" placeholder="请输入组织名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组织类别" prop="orgType">
              <el-select v-model="form.orgType" placeholder="请选择组织类别" style="width: 100%">
                <el-option v-for="item in orgTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="leader">
              <el-input v-model="form.leader" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电子邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入电子邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="sortNum">
              <el-input-number v-model="form.sortNum" :min="1" :max="999" controls-position="right" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="联系地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入联系地址" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="机构状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in enableStatusOptions" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addOrg, delOrg, getOrg, listOrg, updateOrg } from '@/api/risk/org'
import { ENABLE_STATUS_OPTIONS, ORG_TYPE_OPTIONS, findOptionLabel } from '@/views/risk/config'

export default {
  name: 'RiskOrg',
  data() {
    return {
      loading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      orgList: [],
      title: '',
      open: false,
      orgTypeOptions: ORG_TYPE_OPTIONS,
      enableStatusOptions: ENABLE_STATUS_OPTIONS,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgName: undefined,
        orgType: undefined,
        status: undefined
      },
      form: {},
      rules: {
        orgName: [{ required: true, message: '组织名称不能为空', trigger: 'blur' }],
        orgType: [{ required: true, message: '组织类别不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listOrg(this.queryParams).then(response => {
        this.orgList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    reset() {
      this.form = {
        orgId: undefined,
        orgName: undefined,
        orgType: '1',
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: '0',
        address: undefined,
        sortNum: 1,
        remark: undefined
      }
      this.resetForm('form')
    },
    cancel() {
      this.open = false
      this.reset()
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orgId)
      this.single = selection.length !== 1
      this.multiple = selection.length === 0
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增组织机构'
    },
    handleUpdate(row) {
      const orgId = row ? row.orgId : this.ids[0]
      this.reset()
      getOrg(orgId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改组织机构'
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        const request = this.form.orgId ? updateOrg(this.form) : addOrg(this.form)
        request.then(() => {
          this.$message.success(this.form.orgId ? '修改成功' : '新增成功')
          this.open = false
          this.getList()
        })
      })
    },
    handleDelete(row) {
      const orgIds = row ? row.orgId : this.ids.join(',')
      this.$confirm('确认删除选中的组织机构数据吗？', '提示', { type: 'warning' }).then(() => {
        return delOrg(orgIds)
      }).then(() => {
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {})
    },
    optionLabel(options, value) {
      return findOptionLabel(options, value)
    }
  }
}
</script>
