<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.blurry" clearable size="small" placeholder="输入单位名称(全称)/简称/地址/联系方式/介绍/备注进行搜索" style="width: 550px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <date-range-picker v-model="query.registerDate" class="date-item" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="单位名称(全称)" prop="fullName">
            <el-input v-model="form.fullName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位简称" prop="shortName">
            <el-input v-model="form.shortName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位地址" prop="address">
            <el-input v-model="form.address" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="联系方式" prop="contacts">
            <el-input v-model="form.contacts" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位logo" prop="logo">
            <el-input v-model="form.logo" style="width: 370px;" />
          </el-form-item>
          <!-- <el-form-item label="单位logo">
            <el-upload
              ref="upload"
              :limit="1"
              :before-upload="beforeUpload"
              :auto-upload="false"
              :headers="headers"
              :on-success="handleSuccess"
              :on-error="handleError"
              :action="imagesUploadApi"
            >
              <div class="eladmin-upload"><i class="el-icon-upload" /> 添加图片</div>
            </el-upload>
          </el-form-item> -->
          <el-form-item label="单位介绍" prop="description">
            <el-input v-model="form.description" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位官网" prop="website">
            <el-input v-model="form.website" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="注册时间" prop="registerDate">
            <el-date-picker v-model="form.registerDate" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="fullName" label="单位名称(全称)" required />
        <el-table-column prop="shortName" label="单位简称" />
        <el-table-column prop="address" label="单位地址" />
        <el-table-column prop="contacts" label="联系方式" />
        <!-- <el-table-column prop="logo" label="单位logo" /> -->
        <!-- <el-table-column prop="logo" label="单位logo">
          <template slot-scope="{row}">
            <el-image
              :src=" baseApi + '/file/' + row.type + '/' + row.realName"
              :preview-src-list="[baseApi + '/file/' + row.type + '/' + row.realName]"
              fit="contain"
              lazy
              class="el-avatar"
            >
              <div slot="error">
                <i class="el-icon-document" />
              </div>
            </el-image>
          </template>
        </el-table-column> -->
        <el-table-column prop="description" label="单位介绍" />
        <el-table-column prop="website" label="单位官网" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="registerDate" label="注册时间" />
        <el-table-column v-if="checkPer(['admin','dmCompany:edit','dmCompany:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmCompany from '@/api/dm/company/dmCompany'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'

const defaultForm = { companyId: null, fullName: null, shortName: null, address: null, contacts: null, logo: null, description: null, website: null, remark: null, registerDate: null, createBy: null, updateBy: null, createTime: null, updateTime: null }

export default {
  name: 'DmCompany',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '单位', url: 'api/dmCompany', idField: 'companyId', sort: 'companyId,desc', crudMethod: { ...crudDmCompany }})
  },
  data() {
    const validWebsite = (rule, value, callback) => {
      if (value) {
        // 校验网址
        var reg = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/
        var p = new RegExp(reg)
        if (!p.test(value)) {
          callback(new Error('请输入正确的网址'))
        } else {
          callback()
        }
      }
    }
    return {
      permission: {
        add: ['admin', 'dmCompany:add'],
        edit: ['admin', 'dmCompany:edit'],
        del: ['admin', 'dmCompany:del']
      },
      loading: false,
      headers: { 'Authorization': getToken() },
      rules: {
        fullName: [
          { required: true, message: '请输入单位名称(全称)', trigger: 'blur' }
        ],
        shortName: [
          { required: true, message: '请输入单位简称', trigger: 'blur' }
        ],
        website: [
          { required: false, trigger: 'blur', validator: validWebsite }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'imagesUploadApi'
    ])
  },
  methods: {
    beforeUpload(file) {
      let isLt2M = true
      isLt2M = file.size / 1024 / 1024 < 100
      if (!isLt2M) {
        this.loading = false
        this.$message.error('上传文件大小不能超过 100MB!')
      }
      this.form.name = file.name
      return isLt2M
    },
    handleSuccess(response, file, fileList) {
      this.crud.notify('上传成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      this.$refs.upload.clearFiles()
      this.crud.status.add = CRUD.STATUS.NORMAL
      this.crud.resetForm()
      this.crud.toQuery()
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 2500
      })
      this.loading = false
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
