<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评分主题" prop="voteTitle">
        <el-input
          v-model="queryParams.voteTitle"
          placeholder="请输入评分主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="sTime">
        <el-date-picker clearable v-model="queryParams.sTime" type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="eTime">
        <el-date-picker clearable v-model="queryParams.eTime" type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kaohe:vote:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kaohe:vote:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="voteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="主键" align="center" prop="id" width="55" fixed/>
      <el-table-column label="考核任务名称" align="center" prop="pingceName" min-width="200" :show-overflow-tooltip="true" fixed/>
      <el-table-column label="考核项名称" align="center" prop="khitemName" min-width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="考核项类型" align="center" prop="khitemTypeid" min-width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.khitemTypeid===1">选项评分</span>
          <span v-if="scope.row.khitemTypeid===2">占比评分</span>
          <span v-if="scope.row.khitemTypeid===5">单独评分</span>
        </template>
      </el-table-column>
      <el-table-column label="评分主题" align="center" prop="voteTitle" min-width="200" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link type="primary" @click="goVoteEmp(scope.row)">{{ scope.row.voteTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="评分选项" align="center" prop="options" min-width="200" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div v-if="scope.$index=== xzhIndex && scope.row.khitemTypeid===1">
            <el-select :disabled="!(new Date(scope.row.sTime) > (new Date()))"
                       v-model="scope.row.voteItems" multiple placeholder="请选择评分选项">
              <el-option
                v-for="item in vote_options"
                :key="item.id"
                :label="item.vitemName"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div v-else>
            <div v-if=" scope.row.voteItems">
              <div>{{ scope.row.voteItemsName }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="最大值" align="center" prop="maxNum" min-width="220" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div v-if="scope.$index===xzhIndex && scope.row.khitemTypeid!==1">
            <el-input-number :disabled="!(new Date(scope.row.sTime) > (new Date()))"
                             v-model="scope.row.maxNum" :min="0" style="width: 150px;"></el-input-number>
          </div>
          <div v-else>
            <span>{{ scope.row.maxNum }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评分占比" align="center" prop="vote"  min-width="220" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div v-if="scope.$index===xzhIndex && scope.row.khitemTypeid!==5">
            <el-input-number :disabled="!(new Date(scope.row.sTime) > (new Date()))"
                             v-model="scope.row.percentage" :precision="2" :step="0.01" :max="1" style="width: 150px;"></el-input-number>
          </div>
          <div v-else>
            <span>{{ scope.row.percentage }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="vote"  min-width="100" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kh_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="vote"  min-width="220" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div v-if="scope.$index===xzhIndex">
            <el-date-picker clearable style="width: 100%;" v-model="scope.row.sTime"
                            :disabled="!(new Date(scope.row.sTime) > (new Date()))"
                            type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择开始时间">
            </el-date-picker>
          </div>
          <div v-else>
            <span>{{ scope.row.sTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="vote"  min-width="220" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div v-if="scope.$index===xzhIndex">
            <el-date-picker :disabled="!(new Date(scope.row.eTime) > (new Date()))"
                            clearable style="width: 100%;"   v-model="scope.row.eTime"
                            type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择结束时间">
            </el-date-picker>
          </div>
          <div v-else>
            <span>{{ scope.row.eTime }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评分详情描述" align="center" prop="vote" min-width="220" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div v-if="scope.$index===xzhIndex">
            <el-input :disabled="!(new Date(scope.row.eTime) > (new Date()))"
                      v-model="scope.row.vDescription" type="textarea" placeholder="请输入评分详情描述"/>
          </div>
          <div v-else>
            <span>{{ scope.row.vDescription }}</span>
          </div>

        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="vote" min-width="220" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div v-if="scope.$index===xzhIndex">
            <el-input :disabled="!(new Date(scope.row.eTime) > (new Date()))"
                      v-model="scope.row.remark" type="textarea" placeholder="请输入备注"/>
          </div>
          <div v-else>
            <span>{{ scope.row.remark }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
        <template slot-scope="scope">
          <div>
            <el-button v-if="!(scope.$index=== xzhIndex)"
                       size="mini"
                       type="text"
                       icon="el-icon-s-tools"
                       v-hasPermi="['kaohe:vote:edit']"
                       @click="handleUpdate(scope.$index)"
            >修改</el-button>
            <el-button v-if="scope.$index=== xzhIndex"
                       size="mini"
                       type="text"
                       v-hasPermi="['kaohe:vote:edit']"
                       @click="voteSubmit(scope.row)"
            >确认</el-button>
            <el-button v-if="scope.$index=== xzhIndex"
                       size="mini"
                       type="text"
                       v-hasPermi="['kaohe:vote:edit']"
                       @click="cancelVote"
            >取消</el-button>
            <el-button
              v-if="(new Date(scope.row.sTime) < (new Date())) && (new Date(scope.row.eTime) > (new Date()))"
              size="mini"
              type="text"
              icon="el-icon-thumb"
              @click="goVotepage(scope.row)"
            >去评分</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listVote, getVote, delVote, addVote, updateVote } from "@/api/kaohe/vote"
import { listVote_items } from "@/api/kaohe/vote_items"

export default {
  name: "Vote",
  dicts: ['kh_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票评测表格数据
      voteList: [],
      // 查询参数
      queryParams: {
        pageNum: 2,
        pageSize: 10,
        voteTitle: null,
        vDescription: null,
        sTime: null,
        eTime: null,
        maxNum: null,
      },
      // 选中行Index
      xzhIndex: null,
      // 评分选项类型数据
      vote_options: []
    }
  },
  created() {
    this.getVoteItemsList();
  },
  methods: {
    /** 查询投票评测列表 */
    getList() {
      this.loading = true
      listVote(this.queryParams).then(response => {
        let pcSetUpList = [];
        response.rows.forEach(item=>{
          if(item.voteItems){
            item.voteItems = item.voteItems.split(',');
            let VoteItemsName = []
            item.voteItems.forEach(e => {
              this.vote_options.forEach(e1 => {
                if(e === e1.id){
                  VoteItemsName.push(e1.vitemName)
                }
              })
            })
            item['voteItemsName'] = VoteItemsName.join(',');
          }
          pcSetUpList.push(item);
        })
        this.voteList = pcSetUpList;
        this.total = response.total;
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(index) {
      this.xzhIndex = index;
      this.getList();
    },
    cancelVote(){
      this.xzhIndex = null;
      this.getList();
    },
    /** 提交按钮 */
    voteSubmit(row) {
      let flag = true;
      let Vote = {...row}
      if(!Vote.voteTitle){
        flag = false;
        this.$alert(`评分主题不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.state){
        flag = false;
        this.$alert(`状态不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.sTime){
        flag = false;
        this.$alert(`开始时间不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.eTime){
        flag = false;
        this.$alert(`结束时间不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.id && !(new Date(Vote.sTime) > (new Date()))){
        flag = false;
        this.$alert(`开始时间需大于当前时间！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!(new Date(Vote.eTime) > (new Date()))){
        flag = false;
        this.$alert(`结束时间需大于当前时间！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!(new Date(Vote.eTime) > (new Date(Vote.sTime)))){
        flag = false;
        this.$alert(`结束时间需大于开始时间！`, `提示`, {
          type: 'Danger'
        })
      }
      if(row.khitemTypeid===1 && (!Vote.voteItems)){
        flag = false;
        this.$alert(`评分选项不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(row.khitemTypeid!==1 && (!Vote.maxNum)){
        flag = false;
        this.$alert(`最大值不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(row.khitemTypeid!==5 && (!Vote.percentage)){
        flag = false;
        this.$alert(`评分占比不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.voteTitle){
        flag = false;
        this.$alert(`评分主题不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if(!Vote.vDescription){
        flag = false;
        this.$alert(`评分详情描述不能为空！`, `提示`, {
          type: 'Danger'
        })
      }
      if (Vote.id != null && flag ) {
        if(Vote.voteItems){
          Vote.voteItems = Vote.voteItems.join(',')
        }
        updateVote(Vote).then(response => {
          this.$modal.msgSuccess("修改成功")
          this.cancelVote()
        })
      } else if(Vote.id === null && flag){
        if(Vote.voteItems){
          Vote.voteItems = Vote.voteItems.join(',')
        }
        addVote(Vote).then(response => {
          this.$modal.msgSuccess("新增成功")
          this.cancelVote()
        })
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除评分任务编号为"' + ids + '"的数据项？').then(function() {
        return delVote(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      /*this.download('kaohe/vote/export', {
        ...this.queryParams
      }, `vote_${new Date().getTime()}.xlsx`)*/
    },
    goVotepage(row){
      this.$router.push({ path: '/votepage', query: { id: row.id }});
    },
    goVoteEmp(row){
      this.$router.push({ path: '/toupiao/vote_emp', query: { id: row.id }});
    },
    getVoteItemsList() {
      listVote_items({
        pageSize: 1000000,
        pageNum: 1
      }).then(response => {
        this.vote_options = response.rows;
        this.vote_options.forEach(e1 => {
          e1.id = String(e1.id)
        });
        this.getList();
      })
    },
  }
}
</script>
