<template>
  <div class="rrr">
    <!--工具栏-->
    <div class="head-container">
      <div class="head-archives-top">
        <div class="head-search">
          <!-- 搜索 -->
          <el-input v-model="query.blurry" clearable size="small" placeholder="请输入关键词" prefix-icon="el-icon-search" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
          <el-select v-model="query.enabled" clearable size="small" placeholder="状态" class="filter-item" style="width: 100px" @change="crud.toQuery">
            <i slot="prefix" class="iconfont icon-zhuangtai-fanbai" />
            <el-option v-for="item in stateOptions" :key="item.key" :label="item.label" :value="item.key" />
          </el-select>
          <rrOperation />
        </div>
        <el-checkbox v-model="fixedStatusBar">隐藏固定状态栏</el-checkbox>
      </div>
      <crudOperation :permission="permission">
        <template v-slot:rightButtonGroup>
          <div class="archives-handler-btn">
            <el-button class="packing-btn iconfont icon-weibiaoti-2" type="primary">装盒</el-button>
            <el-button class="part-btn iconfont icon-weibiaoti-2" type="primary">分卷装盒</el-button>
            <el-button class="warehousing-btn iconfont icon-weibiaoti-2" type="primary">入库</el-button>
            <el-button class="lending-btn iconfont icon-weibiaoti-2" type="primary">借阅</el-button>
            <el-button class="binding-btn iconfont icon-weibiaoti-2" type="primary">绑定标签</el-button>
          </div>
        </template>
      </crudOperation>
    </div>
    <!--表单组件-->
    <el-dialog class="preview-dialog" :modal-append-to-body="false" :close-on-click-modal="false" :before-close="crud.cancelCU" :visible="crud.status.cu > 0" :title="crud.status.title">
      <span class="dialog-right-top" />
      <span class="dialog-left-bottom" />
      <div class="setting-dialog">
        <PreviewForm :is-disabled="false" :this-fields="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>
      </div>
    </el-dialog>
    <!-- 弹框形式的内容展示 -->
    <el-dialog class="edit-form-dialog" :visible="popoverVisible" :before-close="handleClose" :close-on-click-modal="false" title="字典列表">
      <span class="dialog-right-top" />
      <span class="dialog-left-bottom" />
      <div class="setting-dialog">
        <el-table ref="table" :data="tableData" highlight-current-row style="width: 100%;">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="dicName" label="字典名称" />
          <el-table-column prop="dicCode" label="字典代码" />
          <el-table-column prop="dicExplain" label="内容说明" />
        </el-table>
      </div>
    </el-dialog>
    <el-dialog title="删除字典内容" :visible.sync="deleteVisible" :before-close="handleClose">
      <span class="dialog-right-top" />
      <span class="dialog-left-bottom" />
      <div class="setting-dialog">
        <p><span style="color:#fff;">确定删除当前档案吗？</span></p>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click.native="handleConfirm">确定</el-button>
        </div>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading || getTableDisplayFieldsLoading" class="archives-table" :data="crud.data" highlight-current-row style="width: 100%; " height="calc(100vh - 370px)" @selection-change="crud.selectionChangeHandler" @row-click="clickRowHandler">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column v-for="field in tableDisplayFields" :key="field.id" :label="field.fieldCnName" :align="field.displayformatType" :width="field.displayLength" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row[field.fieldName] }}
        </template>
      </el-table-column>
      <el-table-column label="装盒" width="100" align="center" :fixed="fixedStatusBar?false:'right'">
        <!-- state-active 已装/已入/已借/已绑 -->
        <template slot-scope="">
          <!-- 未装 / 已装 -->
          <span class="row-state row-packing state-active">已装</span>
        </template>
      </el-table-column>
      <el-table-column label="入库" width="100" align="center" :fixed="fixedStatusBar?false:'right'">
        <template slot-scope="">
          <!-- 未入 / 已入 -->
          <span class="row-state row-warehousing">未入</span>
        </template>
      </el-table-column>
      <el-table-column label="借阅" width="100" align="center" :fixed="fixedStatusBar?false:'right'">
        <template slot-scope="">
          <!-- 待借 / 不可借-用“—” 表示 / 已借 -->
          <span class="row-state row-lending">—</span>
        </template>
      </el-table-column>
      <el-table-column label="标签" width="100" align="center" :fixed="fixedStatusBar?false:'right'">
        <template slot-scope="">
          <!-- 未绑 / 已绑 -->
          <span class="row-state row-binding">未绑</span>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import { add, edit, del, getTableDisplayFields, getFormDisplayFields } from '@/api/archivesManage/archivesList'
import CRUD, { presenter, header, form } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import PreviewForm from '@/views/components/category/PreviewForm'

export default {
  components: { crudOperation, rrOperation, pagination, PreviewForm },
  mixins: [
    presenter(),
    header(),
    form({})
  ],
  props: {
    selectedCategory: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  cruds() {
    return [
      CRUD({
        title: '档案',
        url: 'api/archives/initArchivesView',
        query: { isdel: false },
        crudMethod: { add, edit, del },
        optShow: {
          add: true,
          edit: true,
          del: true,
          reset: false,
          download: true,
          group: false
        },
        queryOnPresenterCreated: false,
        sort: ['department,asc']
      })
    ]
  },
  data() {
    return {
      // dicPid: null,
      // needRefreshTree: false,
      // rules: {
      //   dicName: [
      //     { required: true, message: '请输入字典名称', trigger: 'blur' }
      //   ],
      //   dicCode: [
      //     { required: true, message: '请输入字典代码', trigger: 'blur' }
      //   ]
      // },
      stateOptions: [
        { key: '1', label: '全部' },
        { key: '2', label: '未装' },
        { key: '3', label: '未入' },
        { key: '4', label: '在库' },
        { key: '5', label: '待借' },
        { key: '6', label: '已借' }
      ],
      permission: {
        add: ['admin', 'archivesList:add'],
        edit: ['admin', 'archivesList:edit'],
        del: ['admin', 'archivesList:del']
      },
      fixedStatusBar: false,
      deleteVisible: false,
      deleteData: {},
      tableDisplayFields: [],
      getTableDisplayFieldsLoading: false,
      formPreviewData: [],
      rules: {},
      popoverVisible: false,
      selectData: [],
      tableData: [],
      defaultForm: {}
    }
  },
  watch: {
    selectedCategory: function(newValue, oldValue) {
      // 案卷 卷内 文件
      if (newValue.isType === 3 || newValue.isType === 4 || newValue.isType === 5) {
        this.crud.query.categoryId = newValue.id
        this.crud.refresh()
      }
    },
    tableDisplayFields(val) {
      this.doLayout()
    }
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.getTableDisplayFieldsLoading = true
      // 请求数据
      getTableDisplayFields({ categoryId: this.selectedCategory.id }).then(data => {
        this.tableDisplayFields = data
        this.getTableDisplayFieldsLoading = false
        return true
      })
    },
    [CRUD.HOOK.beforeToCU]() {
      this.form.dictionaryConfigId = {}
      getFormDisplayFields({ categoryId: this.selectedCategory.id }).then(data => {
        this.formPreviewData = data.showFiled.filter((fields) => { return fields.isInput })
        this.formPreviewData.map(item => {
          this.rules[item.fieldName] = [
            {
              required: item.isRequired,
              message: (item.isInputClass === 'text' ? '请输入' : '请选择') + item.fieldCnName,
              trigger: item.isInputClass === 'text' ? 'blur' : 'change'
            }
          ]
          if (item.isDefaultValue) {
            this.crud.defaultForm[item.fieldName] = item.isDefaultValue
          } else {
            this.crud.defaultForm[item.fieldName] = null
          }
        })
        this.form.categoryId = this.selectedCategory.id
        return true
      })
    },
    /* 重新渲染table组件 防止table-fixed 错位 配合watch-table数据 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    // // 获取数据前设置默认参数
    // [CRUD.HOOK.beforeRefresh]() {
    //   this.crud.query.id = this.dicPid
    // },
    // [CRUD.HOOK.afterRefresh](crud) {
    //   if (this.needRefreshTree) {
    //     this.$emit('treeRefresh', crud.data)
    //   }
    // },
    toDelete(data) {
      this.deleteData = data
      this.deleteVisible = true
    },
    handleConfirm() {
      this.deleteVisible = false
      this.crud.delAllLoading = true
      this.crud.doDelete(this.deleteData)
    },
    handleClose(done) {
      this.deleteData = {}
      done()
    },
    clickRowHandler(row) {
      this.$refs.table.toggleRowSelection(row)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "~@/assets/styles/archives-manage.scss";
.preview-dialog {
  ::v-deep .el-dialog {
    width: 680px;
    .preview-content {
      max-height: calc(100vh - 330px);
      overflow: hidden;
      overflow-y: auto;
      .el-textarea__inner {
        border: 1px solid #339cff;
        background-color: transparent;
      }
    }
    .el-dialog__header {
      width: 420px;
    }
  }
}
</style>
