<template>
  <el-dialog
          :visible.sync="visible"
          :append-to-body="true"
          :modal-append-to-body="false"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          width="80%"
  >
    <el-card shadow="never" class="aui-card--fill">
      <div class="mod-news__totalconfigselectlist}">
          <el-form :inline="true" :model="dataForm" @keyup.enter.native="selectData()">
              <div>
                  <el-form-item label="统计名称" prop="totalName">
                      <el-input v-model="dataForm.totalName" placeholder="统计名称" clearable></el-input>
                  </el-form-item>
                  <el-form-item label="统计类型" prop="totalType">
                      <el-input v-model="dataForm.totalType" placeholder="统计类型" clearable></el-input>
                  </el-form-item>
              </div>
              <div>
                  <el-form-item label="统计方式" prop="totalStyle">
                      <el-select v-model="dataForm.totalStyle" placeholder="统计方式" clearable>
                          <el-option label="其他" :value="0"></el-option>
                          <el-option label="统计次数" :value="1"></el-option>
                          <el-option label="统计人数" :value="2"></el-option>
                      </el-select>
                  </el-form-item>
                  <el-form-item label="自动计算" prop="autoFlag">
                      <el-select v-model="dataForm.autoFlag" placeholder="自动计算" clearable>
                          <el-option label="否" :value="0"></el-option>
                          <el-option label="是" :value="1"></el-option>
                      </el-select>
                  </el-form-item>
                  <el-form-item>
                      <el-button @click="selectData()">{{ $t('query') }}</el-button>
                  </el-form-item>
                  <el-form-item>
                      <el-button v-if="$hasPermission('workRecord:relation:select')" type="primary" @click="SubmitHandle()">{{ "选择" }}</el-button>
                  </el-form-item>
              </div>
          </el-form>
        <el-table v-loading="dataListLoading" :data="dataList" max-height="500" @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
          <!--        <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">-->
            <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
            <el-table-column label="序号" header-align="center" align="center" width="50px">
              <template slot-scope="scope">
                  {{scope.$index+1}}
              </template>
          </el-table-column>
            <el-table-column prop="totalName" label="统计名称" header-align="center" align="center"></el-table-column>
            <el-table-column prop="totalType" label="统计类型" header-align="center" align="center"></el-table-column>
            <el-table-column prop="totalStyle" label="统计方式" :formatter="totalStyleFormatter" header-align="center" align="center"></el-table-column>
            <el-table-column prop="createdTime" label="创建时间" header-align="center" align="center"></el-table-column>
            <el-table-column prop="sort" label="排序" header-align="center" align="center"></el-table-column>
            <el-table-column prop="autoFlag" label="是否自动计算" :formatter="autoFlagFormatter" header-align="center" align="center"></el-table-column>
            <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
                <template slot-scope="scope">
                    <el-button v-if="$hasPermission('workRecord:relation:select')" type="text" size="small" @click="SubmitHandle(scope.row.id)">选择</el-button>

                </template>
            </el-table-column>
      </el-table>
        <el-pagination
                :current-page="page"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="limit"
                :total="total"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="pageSizeChangeHandle"
                @current-change="pageCurrentChangeHandle">
        </el-pagination>
      </div>
    </el-card>
  </el-dialog>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/workRecord/totalconfig/selectPage',
        getDataListIsPage: true
      },
      selectIsBatchKey: 'id',
      buttonFlag : false,
      visible: false,
      dataForm: {
        resourceId: '',
        totalConfigIdList: [],
        totalName: '',
        totalType: '',
        totalStyle: '',
        autoFlag: ''
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        if (this.dataForm.resourceId) {
          this.dataListSelections = []
          this.dataForm.totalConfigIdList = []
          this.getDataList()
        }
      })
    },
    totalStyleFormatter: function (row, column) {
      if (row.totalStyle === 0) {
        return '其他'
      } else if (row.totalStyle === 1) {
        return '统计次数'
      } else if (row.totalStyle === 2) {
        return '统计人数'
      }
    },
    selectData(){
        this.page = 1
        this.getDataList()
    },
    autoFlagFormatter: function (row, column) {
      if (row.autoFlag === '0') {
        return '否'
      } else if (row.autoFlag === '1') {
        return '是'
      }
    },
    SubmitHandle(configId) {
      if(!configId && this.dataListSelections.length <= 0) {
          return this.$message({
              message: this.$t('prompt.selectBatch'),
              type: 'warning',
              duration: 500
          })
      }
      this.buttonFlag = true
      if(configId){
          this.dataForm.totalConfigIdList = [configId]
      } else {
          this.dataForm.totalConfigIdList = this.dataListSelections.map(item => item[this.selectIsBatchKey])
      }
      this.$http['post']('/workRecord/resourcestotalrelation/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
              return this.$message.error(res.msg)
          }
          this.buttonFlag = false
          this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
              }
          })
      }).catch(() => {})
    }
  }
}
</script>
