<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col :span="8">
          <el-form-item class="treeselectForm treeselectForm2" label="组织架构" prop="deptId">
            <tree @treeselectFn="treeselectFn" :treeval="queryParams.deptIds" :ismultiple="true" style="width: 193px;"></tree>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户" prop="userId">
            <user :userId="queryParams.userId" :orgIds="queryParams.deptIds" @userIdFn="userIdFn"></user>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="任务名称" prop="summaryId">
            <el-select size="mini" v-model="queryParams.summaryId" clearable placeholder="请选择任务名称">
              <el-option
                v-for="item in summaryTaskList"
                :key="item.summaryId"
                :label="item.summaryTaskName"
                :value="item.summaryId">
              </el-option>
            </el-select>
          </el-form-item>

        </el-col>
        <el-col :span="8">
          <el-form-item label="任务时间" prop="datetimerange">
            <el-date-picker
              v-model="datetimerange"
              align="left"
              end-placeholder="结束日期"
              range-separator="至"
              start-placeholder="开始日期"
              style="width: 240px"
              type="daterange"
              size="mini"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <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-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sfa:userSummary:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userSummaryList" >
      <el-table-column label="任务名称" align="center" prop="summaryTaskName" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="公司名称" align="center" prop="companyName" width="150"/>
      <el-table-column label="大区名称" align="center" prop="regionName" width="150"/>
      <el-table-column label="部门名称" align="center" prop="deptName" width="150"/>
      <el-table-column label="用户名称" align="center" prop="nickName" />
      <el-table-column label="工号" align="center" prop="workCode" />
      <el-table-column label="岗位" align="center" prop="postName" width="150" />
      <el-table-column label="提交时间" align="center" prop="createTime" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['sfa:userSummary:query']"
          >查看</el-button>
          <el-button
            v-hasPermi="['sfa:userSummary:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['sfa:userSummary:remove']"
            class="text-danger"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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


    <!-- 任务详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-descriptions title="">
        <el-descriptions-item label="任务名称">
          {{form.summaryTaskName}}
        </el-descriptions-item>
        <el-descriptions-item label="公司名称">
          {{form.companyName}}
        </el-descriptions-item>
        <el-descriptions-item label="大区名称">
          {{form.regionName}}
        </el-descriptions-item>
        <el-descriptions-item label="部门">
          {{form.deptName}}
        </el-descriptions-item>
        <el-descriptions-item label="用户名称">
          {{form.nickName}}
        </el-descriptions-item>
        <el-descriptions-item label="工号">
          {{form.workCode}}
        </el-descriptions-item>
        <el-descriptions-item label="岗位">
          {{form.postName}}
        </el-descriptions-item>
        <el-descriptions-item label="填报时间">
          {{form.createTime}}
        </el-descriptions-item>
        <el-descriptions-item label="修改人" v-if="form.updateBy">
          {{form.updateBy}}
        </el-descriptions-item>
        <el-descriptions-item label="修改时间" v-if="form.updateTime">
          {{form.updateTime}}
        </el-descriptions-item>
      </el-descriptions>

      <el-divider></el-divider>

      <el-form ref="answerForm" :model="form" :disabled="true"  size="mini" >
        <el-row v-for="(customItem,index) in form.customItemList" >
          <el-col :span="24" v-if="customItem.answerType == 'dx'" >
            <el-form-item :label="customItem.customName" >
              <el-radio v-for="(customOption, index) in customItem.customOptionList" v-model ="customItem.answerValue" :label="customOption.customOptionId">{{customOption.customOption}}</el-radio>

<!--              <el-radio-group>-->
<!--                <el-radio v-for="(customOption, index) in customItem.customOptionList" v-model ="customOption.checked" :label="customOption.customOption">{{customOption.customOption}}</el-radio>-->
<!--              </el-radio-group>-->
              {{customItem.remark}}
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'duox'">
            <el-form-item :label="customItem.customName" >
              <el-checkbox v-for="(customOption, index) in customItem.customOptionList" v-model="customOption.checked">{{customOption.customOption}}</el-checkbox>
              {{customItem.remark}}
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'zp'">
            <el-form-item :label="customItem.customName" >
              <div class="demo-image__preview">
                <el-image
                  v-for="(fileInfo, index) in customItem.fileInfoList"
                  class="imgPreview"
                  :src="fileInfo.fileUrl"
                  :preview-src-list="fileInfoLists" @click="previewFn(customItem)">
                </el-image>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'wb'||customItem.answerType == 'gs'||customItem.answerType == 'dq'||customItem.answerType == 'xsb'">
            <el-form-item :label="customItem.customName" >
              <el-input v-model="customItem.answerValue" type="textarea"></el-input>
              {{customItem.remark}}
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'rq'|| customItem.answerType == 'sz'">
            <el-form-item :label="customItem.customName" >
              <el-input v-model="customItem.answerValue"></el-input>
              {{customItem.remark}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 任务详情对话框 -->
    <el-dialog :title="title" :visible.sync="openEdit" width="800px" append-to-body :before-close="handleClose" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-descriptions title="">
        <el-descriptions-item label="任务名称">
          {{form.summaryTaskName}}
        </el-descriptions-item>
        <el-descriptions-item label="公司名称">
          {{form.companyName}}
        </el-descriptions-item>
        <el-descriptions-item label="大区名称">
          {{form.regionName}}
        </el-descriptions-item>
        <el-descriptions-item label="部门">
          {{form.deptName}}
        </el-descriptions-item>
        <el-descriptions-item label="用户名称">
          {{form.nickName}}
        </el-descriptions-item>
        <el-descriptions-item label="工号">
          {{form.workCode}}
        </el-descriptions-item>
        <el-descriptions-item label="岗位">
          {{form.postName}}
        </el-descriptions-item>
        <el-descriptions-item label="填报时间">
          {{form.createTime}}
        </el-descriptions-item>
        <el-descriptions-item label="修改人" v-if="form.updateBy">
          {{form.updateBy}}
        </el-descriptions-item>
        <el-descriptions-item label="修改时间" v-if="form.updateTime">
          {{form.updateTime}}
        </el-descriptions-item>
      </el-descriptions>
      <el-divider>答题内容</el-divider>
      <el-form ref="form" :model="form"  size="mini" class="el-label-form">
        <el-row v-for="(customItem,index) in form.customItemList" >
          <el-col :span="24" v-if="customItem.answerType == 'dx'">
            <el-form-item :label="index+1+'、'+customItem.customName"  >
              <el-radio  disabled  v-for="(customOption, index) in customItem.customOptionList" v-model ="customItem.answerValue" :label="customOption.customOptionId">{{customOption.customOption}}</el-radio>
              <p style="text-align: right;font-size: 12px;color:#999;">备注：{{customItem.remark}}</p>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'duox'">
            <el-form-item :label="index+1+'、'+customItem.customName" >
              <el-checkbox  disabled v-for="(customOption, index) in customItem.customOptionList" v-model="customOption.checked" >{{customOption.customOption}}</el-checkbox>
              <p style="text-align: right;font-size: 12px;color:#999;">备注：{{customItem.remark}}</p>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'zp'">
            <el-form-item :label="index+1+'、'+customItem.customName" >
              <div class="demo-image__preview">
                <el-image
                  v-for="(fileInfo, index) in customItem.fileInfoList"
                  class="imgPreview"
                  :src="fileInfo.fileUrl"
                  :preview-src-list="fileInfoLists" @click="previewFn(customItem)">
                </el-image>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'gs'||customItem.answerType == 'dq'||customItem.answerType == 'xsb'">
            <el-form-item :label="index+1+'、'+customItem.customName"  :prop="'customItemList.'+index+'.answerValue'" >
              <el-input disabled v-model="customItem.answerValue"></el-input>
              <el-row>
                <el-col :span="12"> <p style="text-align: left;font-size: 12px;color:#999;">备注：{{customItem.remark}}</p></el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'wb' || customItem.answerType == 'sz'">
            <el-form-item :label="index+1+'、'+customItem.customName"  :prop="'customItemList.'+index+'.answerValue'" :rules="rules.answerTextValue">
              <el-input v-model="customItem.answerValue"></el-input>
              <el-row>
                <el-col :span="12"> <p style="text-align: left;font-size: 12px;color:#999;">备注：{{customItem.remark}}</p></el-col>
                <el-col :span="12">&nbsp;&nbsp;<p v-if="customItem.updateBy" style="text-align: right;font-size: 12px;color:#999;"> 修改人：{{customItem.updateBy}}<span style="width:40px;display: inline-block"></span>修改时间：{{customItem.updateTime}}</p></el-col>
              </el-row>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="customItem.answerType == 'rq'">
            <el-form-item :label="index+1+'、'+customItem.customName" :prop="'customItemList.'+index+'.answerValue'" :rules="rules.answerValue" >
              <el-date-picker
                v-model="customItem.answerValue"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
              </el-date-picker>
              <el-row>
                <el-col :span="12"> <p style="text-align: left;font-size: 12px;color:#999;">备注：{{customItem.remark}}</p></el-col>
                <el-col :span="12">&nbsp;&nbsp;<p v-if="customItem.updateBy" style="text-align: right;font-size: 12px;color:#999;"> 修改人：{{customItem.updateBy}}<span style="width:40px;display: inline-block"></span>修改时间：{{customItem.updateTime}}</p></el-col>
              </el-row>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
        <el-button @click="canceleiti" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserSummary, getUserSummary, editAnswer } from "@/api/sfa/userSummary";
import user from "@/views/components/sfa/user";
import tree from "@/views/components/sfa/treeselectParent";
import {
  userSummary,
  listSummaryTask, editPublish, updateSummaryTask, addSummaryTask,
} from "@/api/sfa/summaryTask";
export default {
  name: "UserSummary",
  components: { user,tree },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      openEdit:false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主管任务查询表格数据
      userSummaryList: [],
      // 弹出层标题
      title: "",
      datetimerange:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userId: null,
        summaryId: null,
      },
      queryParamss:{
        pageNum: 1,
        pageSize: 300,
        summaryTaskName: null,
        fillObject: null,
        startTime: null,
        stopTime: null,
        status: "1",
      },
      summaryTaskList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        delFlag: [
          { required: true, message: "删除标记  0：有效  2：无效不能为空", trigger: "blur" }
        ],
        answerValue: [
          { required: true, message: "日期不能为空", trigger: "change" }
        ],
        answerTextValue: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
      },
      fileInfoLists:[]
    };
  },
  created() {
    this.getList();
    this.getLists();
  },
  methods: {
    getLists() {
      this.loading = true;
      listSummaryTask(this.queryParamss).then(response => {
        this.summaryTaskList = response.rows;
      });
    },
    userIdFn(val){
      this.queryParams.userId = val;
    },
    treeselectFn(val){
      this.queryParams.deptIds = val;
      // this.$refs['queryParams'].validate((valid) => {
      //     console.log(valid)
      // })
    },
    /** 查询主管任务查询列表 */
    getList() {
      this.loading = true;
      if(this.datetimerange!=null){
        this.queryParams.startTime=this.datetimerange[0]
        this.queryParams.endTime=this.datetimerange[1]
      }else{
        this.queryParams.startTime=""
        this.queryParams.endTime=""
      }

      listUserSummary(this.queryParams).then(response => {
        this.userSummaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    canceleiti(){
      var that=this
      this.$modal.confirm('是否关闭页面？').then(function () {
        that.openEdit=false
      })
    },
    handleClose(){
      var that=this
      this.$modal.confirm('是否关闭页面？').then(function () {
        that.openEdit=false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        userSummaryId: null,
        deptId: null,
        userId: null,
        summaryId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const summaryIds = row.userSummaryId || this.ids;
      this.$modal.confirm('是否确认删除主管任务为"' + row.summaryTaskName + '"的数据项？').then(function () {
        return userSummary(summaryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const summaryId = row.userSummaryId || this.ids
      getUserSummary(summaryId).then(response => {
        // response.data.fillObjects = response.data.fillObject.split(',');
        for(var i=0;i<response.data.customItemList.length;i++){
          if(response.data.customItemList[i].answerType=="zp"){
            response.data.customItemList[i].fileInfoLists=[]
            if(response.data.customItemList[i].fileInfoList!=null&&response.data.customItemList[i].fileInfoList!=undefined){
              var fileInfoList=response.data.customItemList[i].fileInfoList
              for(var k=0;k<fileInfoList.length;k++){
                response.data.customItemList[i].fileInfoLists.push(fileInfoList[k].fileUrl)
              }
            }
          }
          if(response.data.customItemList[i].answerType=="dx"){
            for(var k=0;k<response.data.customItemList[i].customOptionList.length;k++){
              if(response.data.customItemList[i].customOptionList[k].checked){
                response.data.customItemList[i].answerValue=response.data.customItemList[i].customOptionList[k].customOptionId
              }
            }
          }
        }
        response.data.datetimerange=[]
        response.data.datetimerange[0] = response.data.startTime;
        response.data.datetimerange[1] = response.data.stopTime;
        if(response.data.status=="1"){
          this.status=true
        }else{
          this.status=false
        }
        this.form = response.data;
        this.openEdit = true;
        this.title = "修改主管任务";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const userSummaryId = row.userSummaryId || this.ids
      getUserSummary(userSummaryId).then(response => {
        for(var i=0;i<response.data.customItemList.length;i++){
            if(response.data.customItemList[i].answerType=="zp"){
              response.data.customItemList[i].fileInfoLists=[]
              if(response.data.customItemList[i].fileInfoList!=null&&response.data.customItemList[i].fileInfoList!=undefined){
                var fileInfoList=response.data.customItemList[i].fileInfoList
                for(var k=0;k<fileInfoList.length;k++){
                  response.data.customItemList[i].fileInfoLists.push(fileInfoList[k].fileUrl)
                }
              }
            }
          if(response.data.customItemList[i].answerType=="dx"){
            for(var k=0;k<response.data.customItemList[i].customOptionList.length;k++){
              if(response.data.customItemList[i].customOptionList[k].checked){
                response.data.customItemList[i].answerValue=response.data.customItemList[i].customOptionList[k].customOptionId
              }
            }
          }
        }
        this.form = response.data;
        this.open = true;
        this.title = "查看任务详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.datetimerange == null || this.form.datetimerange == []) {
        this.form.startTime = '';
        this.form.stopTime = '';
      } else {
        this.form.startTime = this.form.datetimerange[0];
        this.form.stopTime = this.form.datetimerange[1];
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
              editAnswer(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.openEdit = false;
                this.getList();
              })
        }
      });
    },
    previewFn(val){
      this.fileInfoLists=val.fileInfoLists
    },
    /** 导出按钮操作 */
    handleExport() {
      if(this.queryParams.summaryId!=null&&this.queryParams.summaryId!=""){
      let exportParams = this.queryParams;
      this.download('sfa/userSummary/export', {
        ...exportParams
      }, `userSummary_${new Date().getTime()}.xlsx`)
      }else{
        this.$message({ message: '请选择任务', type: 'warning' })
      }
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
