<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <!-- <label class="el-form-item-label">单位名称(全称)</label>
        <el-input v-model="query.fullName" clearable placeholder="单位名称(全称)" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">单位简称</label>
        <el-input v-model="query.shortName" clearable placeholder="单位简称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">单位地址</label>
        <el-input v-model="query.address" clearable placeholder="单位地址" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">联系方式</label>
        <el-input v-model="query.contacts" clearable placeholder="联系方式" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">单位介绍</label>
        <el-input v-model="query.description" clearable placeholder="单位介绍" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">备注</label>
        <el-input v-model="query.remark" clearable placeholder="备注" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <el-input v-model.trim="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="单位名称(全称)">
            <el-input v-model.trim="form.fullName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位简称">
            <el-input v-model.trim="form.shortName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位地址">
            <el-input v-model.trim="form.address" style="width: 370px;"  />
          </el-form-item>
          <el-form-item label="联系方式" prop="contacts">
            <el-input v-model.trim="form.contacts" style="width: 370px;"  />
          </el-form-item>
          <el-form-item label="单位logo">
            <uploadDialog :upload-params.sync="uploadParams" @callback="callbackFunc" />
          </el-form-item>
          <el-form-item label="单位介绍">
            <el-input v-model.trim="form.description" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="单位官网">
            <el-input v-model.trim="form.website" style="width: 370px;"  />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model.trim="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="注册时间">
            <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="单位名称(全称)" />
        <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="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 uploadDialog from '@/components/Upload/uploadImage'
import { isvalidPhone } from '@/utils/validate'

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 }
// 自定义验证
const validPhone = (rule, value, callback) => {
  if (value && !isvalidPhone(value)) {
    callback(new Error('请输入正确的联系方式'))
  } else {
    callback()
  }
}
export default {
  name: 'DmCompany',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker, uploadDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '单位', url: 'api/dmCompany', idField: 'companyId', sort: 'companyId,desc', crudMethod: { ...crudDmCompany }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'dmCompany:add'],
        edit: ['admin', 'dmCompany:edit'],
        del: ['admin', 'dmCompany:del']
      },
      rules: {
        companyId: [
          { required: true, message: '主键不能为空', trigger: 'blur' }
        ],
        contacts: [
          { required: false, trigger: 'blur', validator: validPhone }
        ]
      },
      uploadParams: {
        fileIds: [],
        fileList: [],
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        limit: 1
      },
      queryTypeOptions: [
        { key: 'fullName', display_name: '单位名称(全称)' },
        { key: 'shortName', display_name: '单位简称' },
        { key: 'address', display_name: '单位地址' },
        { key: 'contacts', display_name: '联系方式' },
        { key: 'description', display_name: '单位介绍' },
        { key: 'remark', display_name: '备注' }
      ]
    }
  },
  watch: {
    // 编辑框隐藏时,更新图片展示状态
    'crud.status.cu': {
      handler: function(val, oldVal) {
        if (val <= 0) {
          this.$set(this.uploadParams, 'fileList', [])
          this.$set(this.uploadParams, 'fileIds', [])
        } else if (val === 1) {
          this.$set(this.uploadParams, 'fileIds', this.form.logo != null ? this.form.logo.split(',') : [])
        }
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    callbackFunc(file_id) {
      this.form.logo = file_id
    }
  }
}
</script>

<style scoped>

</style>
