<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， 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="borrowMsgType">
            <el-select
              v-model="form.borrowMsgType"
              clearable
              placeholder="提醒方式"
              style="width: 350px;"
              class="filter-item"
            >
              <el-option
                v-for="item in dict.send_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="逾期时间">
            <template>
              <div>
                <!--                <el-input v-for="(item,i) of times" v-model="times[i]" :key="i" style="width: 350px;" />-->
                <!--                <span ><el-button @click = "addInput" type="primary">添加</el-button></span>-->
                <el-select v-model="form.overdueTimeMsgArr" multiple placeholder="请选择" style="width: 350px;">
                  <el-option
                    v-for="item in overdueTime"
                    :key="item.configOverdueContent"
                    :label="item.configOverdueLabel"
                    :value="item.configOverdueContent"
                  />
                </el-select>
              </div>
            </template>
          </el-form-item>
          <el-form-item label="逾期金额" prop="overdueAmount">
            <el-input v-model="form.overdueAmount" style="width: 350px;" />
          </el-form-item>
          <el-form-item label="扣费上限" prop="overdueAmountMax">
            <el-input v-model="form.overdueAmountMax" style="width: 350px;" />
          </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>-->
          <el-button :loading="crud.status.cu === 2" type="primary" @click="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="borrowMsgType" label="借阅方式">
          <template slot-scope="{ row }">
            {{ row.borrowMsgType === '00' ? '邮箱': row.borrowMsgType === '01'? '手机' : row.borrowMsgType === '02'? '短信' : '未知' }}
          </template>
        </el-table-column>
        <el-table-column prop="overdueAmount" label="逾期金额" />
        <el-table-column prop="overdueAmountMax" label="扣费上限" />
        <el-table-column label="逾期时间提醒">
          <template #default="{row}">
            <el-select :value="row.overdueTimeMsg.split(',')" multiple collapse-tags style="width: 200px;">
              <el-option
                v-for="item in filterOverTime(row)"
                :key="item.configOverdueContent"
                :label="item.configOverdueLabel"
                :value="item.configOverdueContent"
                :disabled="true"
              />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','dmReadingRoomConfig:edit','dmReadingRoomConfig: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 crudDmReadingRoomConfig from '@/api/dmReadingRoomConfig'
import crudDmReadingRoomConfigOverdue from '@/api/dmReadingRoomConfigOverdue'
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'

const defaultForm = { borrowMsgType: null, borrowConfigId: null, overdueAmount: null, overdueAmountMax: null, overdueTimeMsg: null, overdueTimeMsgArr: [] }

const optShow = {
  add: true,
  edit: false,
  del: false,
  download: false,
  reset: true
}
export default {
  name: 'DmReadingRoomConfig',
  // eslint-disable-next-line vue/no-unused-components
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['send_type'],
  cruds() {
    return CRUD({ title: '阅览配置', url: 'api/dmReadingRoomConfig', idField: 'borrowConfigId', sort: 'borrowConfigId,desc', crudMethod: { ...crudDmReadingRoomConfig }, optShow })
  },
  data() {
    return {
      times: [],
      overdueTime: [],
      permission: {
        add: ['admin', 'dmReadingRoomConfig:add'],
        edit: ['admin', 'dmReadingRoomConfig:edit'],
        del: ['admin', 'dmReadingRoomConfig:del']
      },
      selectOptionValue: [{ name: '邮箱', value: '00', disabled: true }, { name: 'APP', value: '01', disabled: true }, { name: '短信', value: '02' }],
      rules: {
        borrowMsgType: [
          { required: true, message: '借阅提醒方式不能为空', trigger: 'blur' }
        ],
        overdueAmount: [
          { required: true, message: '逾期金额不能为空', trigger: 'blur' }
        ],
        overdueAmountMax: [
          { required: true, message: '扣费上限不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    crudDmReadingRoomConfigOverdue.get().then(res => {
      this.overdueTime = res.content
      // this.form.overdueTimeMsgArr = this.crud.data[0].overdueTimeMsg.split(',')
    })
  },
  methods: {
    filterOverTime(data) {
      const overdueTimeArr = data.overdueTimeMsg.split(',')
      return this.overdueTime.filter(item => {
        return overdueTimeArr.includes(item.configOverdueContent)
      })
    },
    submitCU() {
      // eslint-disable-next-line no-undef
      this.form.overdueTimeMsg = this.form.overdueTimeMsgArr.join(',')
      // 表单验证
      if (this.form.overdueAmount > this.form.overdueAmountMax) {
        return this.$message.info(' 逾期金额不能大于扣款上限 ')
      }
      if (this.crud.status.add === 1) {
        crudDmReadingRoomConfig.add(this.form).then(res => {
          this.$message.info(res.content)
          this.crud.status.cu = 0
          this.crud.status.add = 0
          this.crud.refresh()
        })
      } else if (this.crud.status.edit === 1) {
        crudDmReadingRoomConfig.edit(this.form).then(res => {
          this.$message.info(res.content)
          this.crud.status.cu = 0
          this.crud.status.edit = 0
          this.crud.refresh()
        })
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeSubmit]() {
      this.form.overdueTimeMsg = this.form.overdueTimeMsgArr.join(',')
    },
    [CRUD.HOOK.beforeToEdit]() {
      this.form.overdueTimeMsgArr = this.form.overdueTimeMsg.split(',')
    },
    [CRUD.HOOK.afterRefresh]() {
      this.form.overdueTimeMsgArr = this.crud.data[0].overdueTimeMsg.split(',')
    }
  }
}
</script>

<style scoped>

</style>
