<template>
  <div class="app-container row-container">
    <!--侧边部门数据-->
    <!--工具栏-->
    <div class="head-container">
      <div class="head-search" style="align-items: center;">
        <el-select v-model="query.type" clearable size="small" placeholder="栏目类型" class="filter-item" style="width: 160px;" @change="crud.toQuery">
          <el-option v-for="item in typeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-input v-model="query.title" size="small" clearable placeholder="输入栏目名称搜索" prefix-icon="el-icon-search" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.status" clearable size="small" placeholder="状态" class="filter-item" style="width: 100px" @change="crud.toQuery">
          <i slot="prefix" class="iconfont icon-zhuangtai" />
          <el-option v-for="item in enabledTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation />
        <el-checkbox v-model="query.isContent" label="仅显示内容数量为 0 的栏目" name="fiterColumnZero" @change="crud.toQuery" />
      </div>
      <crudOperation :permission="permission">
        <template v-slot:middle>
          <el-button slot="reference" size="mini" :loading="crud.delAllLoading" :disabled="crud.selections.length === 0" @click="toDelete(crud.selections)">
            <i class="iconfont icon-shanchu" />
            删除
          </el-button>
        </template>
        <template v-slot:right>
          <el-button :loading="crud.downloadLoading" size="mini" :disabled="crud.selections.length === 0" @click="doExport(crud.selections)">
            <i class="iconfont icon-daochu" />
            导出
          </el-button>
        </template>
      </crudOperation>
    </div>
    <div class="container-wrap">
      <span class="right-top-line" />
      <span class="left-bottom-line" />
      <el-dialog :close-on-click-modal="false" :modal-append-to-body="false" append-to-body :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title">
        <span class="dialog-right-top" />
        <span class="dialog-left-bottom" />
        <div class="setting-dialog">
          <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="110px">
            <el-row>
              <el-form-item label="栏目名称" prop="title">
                <el-input v-model="form.title" style="width: 586px;" />
              </el-form-item>
            </el-row>
            <el-form-item label="栏目编码" prop="code">
              <el-input v-model="form.code" />
            </el-form-item>
            <el-form-item label="栏目类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择">
                <el-option
                  v-for="(item,index) in columnTypeOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="排序" prop="seqencing">
              <el-input-number
                v-model.number="form.seqencing"
                :min="0"
                :max="999"
                controls-position="right"
              />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">启用</el-radio>
                <el-radio :label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-row>
              <el-form-item label="备注" prop="remarks">
                <el-input v-model="form.remarks" style="width: 586px;" rows="3" type="textarea" />
              </el-form-item>
            </el-row>
          </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>
        </div>
      </el-dialog>
      <el-table ref="table" v-loading="crud.loading" highlight-current-row style="width: 100%;" height="calc(100vh - 330px)" :data="crud.data" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" align="center" width="55" />
        <el-table-column prop="title" label="栏目名称" />
        <el-table-column prop="code" label="栏目编码" />
        <el-table-column prop="type" label="栏目类型">
          <template slot-scope="scope">
            <div v-if="scope.row.type === 1">图文详情</div>
            <div v-if="scope.row.type === 2">文字列表-图文详情</div>
            <div v-if="scope.row.type === 3">图文列表-图文详情</div>
            <div v-if="scope.row.type === 4">图文列表-外部跳转</div>
          </template>
        </el-table-column>
        <el-table-column prop="deptsCount" label="内容数量" />
        <el-table-column prop="seqencing" label="排序" align="center" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-color="#409EFF" inactive-color="#F56C6C" :active-value="1" :inactive-value="0" @change="changeStatus(scope.row, scope.row.status)" />
          </template>
        </el-table-column>
        <el-table-column prop="create" label="创建者" />
        <el-table-column :show-overflow-tooltip="true" prop="createTime" label="创建日期">
          <template slot-scope="scope">
            <div>{{ scope.row.createTime | parseTime }}</div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination v-if="crud.data.length !== 0" />
    </div>
  </div>
</template>

<script>
import crudColumn from '@/api/inquiryMachine/column'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
// import { exportFile } from '@/utils/index'
// import qs from 'qs'
import { mapGetters } from 'vuex'

const defaultForm = { id: null, title: null, code: null, type: 1, seqencing: 999, status: 1, remarks: null }
export default {
  name: 'Column',
  components: { pagination, crudOperation, rrOperation },
  cruds() {
    return CRUD({ title: '栏目', url: 'api/queryMachine/initQueryTopic', crudMethod: { ...crudColumn }, optShow: {
      add: true,
      edit: true,
      del: false,
      reset: true,
      download: false,
      group: false
    }})
  },
  mixins: [presenter(), header(), form(function() {
    return Object.assign({ libcode: this.user.fonds.fondsNo }, defaultForm)
  }), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'column:add'],
        edit: ['admin', 'column:edit'],
        del: ['admin', 'column:del']
      },
      typeOptions: [
        { key: '1', display_name: '图文详情' },
        { key: '2', display_name: '文字列表-图文详情' },
        { key: '3', display_name: '图文列表-图文详情' },
        { key: '4', display_name: '图文列表-外部跳转' }
      ],
      enabledTypeOptions: [
        { key: '1', display_name: '启用' },
        { key: '0', display_name: '停用' }
      ],
      columnTypeOptions: [
        { value: 1, label: '图文详情' },
        { value: 2, label: '文字列表-图文详情' },
        { value: 3, label: '图文列表-图文详情' },
        { value: 4, label: '图文列表-外部跳转' }
      ],
      rules: {
        title: [
          { required: true, message: '请输入机构名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入栏目编码', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择栏目类型', trigger: 'change' }
        ],
        seqencing: [
          { required: true, message: '请输入排序', trigger: 'blur', type: 'number' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'user'
    ])
  },
  created() {
  },
  mounted() {
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.query.libcode = this.user.fonds.fondsNo
    },
    [CRUD.HOOK.afterRefresh]() {
    },
    // 新增前
    [CRUD.HOOK.beforeToAdd](crud, form) {
    },
    // 编辑前
    [CRUD.HOOK.beforeToEdit](crud, form) {
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      console.log(crud.form)
      return true
    },
    handleFiterColumnZero(val) {
      console.log(val)
    },
    toDelete(datas) {
      this.$confirm('此操作将删除当前所选' + this.crud.title + '<span>你是否还要继续？</span>', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        this.crud.delAllLoading = true
        const ids = []
        datas.forEach(val => {
          ids.push(val.id)
        })
        crudColumn.del(ids).then((res) => {
          if (res.code !== 500) {
            this.$message({ message: '删除成功', type: 'success', offset: 8 })
            this.crud.refresh()
          } else {
            this.$message({ message: '删除失败', type: 'error', offset: 8 })
          }
          this.crud.delAllLoading = false
        }).catch(err => {
          this.crud.delAllLoading = false
          console.log(err)
        })
      }).catch(() => {
      })
    },
    changeStatus(data, val) {
      this.$confirm('此操作将禁用 / 启用栏目 “' + data.title + '”' + '<span>你是否还要继续？</span>', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        crudColumn.FetchQueryTopicStatus(data).then(res => {
          this.$message({ message: '修改成功', type: 'success', offset: 8 })
        }).catch(() => {
          data.status = !data.status
        })
      }).catch(() => {
        this.$message({ message: '已取消修改', offset: 8 })
        data.status = data.status ? 0 : 1
      })
    },
    doExport(data) {
      crud.downloadLoading = true
      this.$confirm('此操作将导出所选数据' + '<span>你是否还要继续？</span>', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        const ids = []
        data.forEach(val => {
          ids.push(val.fondsId)
        })
        const params = {
          'fondsIds': ids
        }
        console.log('params', params)
        // exportFile(this.baseApi + '/api/fonds/download?' + qs.stringify(params, { indices: false }))
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  .el-dialog__body {
    .el-form-item{
      &:nth-child(odd){
        margin-right: 0 !important;
      }
    }
  }
}
</style>
