"use strict";
import {base} from './base.js'
import { util } from '../../util/util.js'
import {config} from "../../config.js";
import {fullMap} from "../../fullMap.js";
import {khcAlert} from "../alert/khcAlert.js";
import {httpUtil} from "../../util/httpUtil.js";
/**
 * 意见
 */
class vOpinion extends base{
    constructor(data) {
        super(data);
        this.type='opinion';
        this.opinionData=[];
    }
    render(parentObjID){
        let that=this;
        this.initFunc();
        let data=that.data
        let objID=that.objID;
        let eleID=that.eleID;
        let labelName=data.label_name;
        let placeholder=data.placeholder;
        let defaultValue=data.default_value;
        let isReadOnly=data.is_read_only;
        let rows=data.rows;
        let canNull=data.can_null;
        let cutCharUUID=util.getUUID()+'div';
        let htmlTitleTemplate=`
        <div class="card mb-4"  id="${cutCharUUID}div" style="display: none">
        <h6 class="card-header">意见</h6>
        <div class="card-body" id="${cutCharUUID}">
        </div></div>
        `;
        document.getElementById(parentObjID).insertAdjacentHTML("beforeEnd", htmlTitleTemplate);
        that.data.parentObjID = cutCharUUID;
        let htmlTemplate=`
        <div class="form-group row col-sm" id="${objID}_opinion"></div>
        <div class="form-group row col-sm-${that.rows}" id="${objID}" style="display: none">
        <label class="col-form-label col-sm-2 text-sm-right" style="line-height:25px;vertical-align: middle;">
        <font id="${that.objID}CanNull" style="font-size: 22px;vertical-align: middle;${canNull!='Y'?'display:none;':''}" color="red">*</font>
        ${labelName}</label>
        <div class="col-sm-10">
        <textarea type="text" id="${eleID}" rows="${rows}" value="${!util.isNull(defaultValue)?defaultValue:''}" name="${data.name}" 
        class="form-control" ${isReadOnly == 'Y'?'readonly=readonly':''} placeholder="${placeholder}"></textarea>
        <small class="form-text text-muted"><font style="display:none;"  validate="validate"  color="red">12412345123</font></small>
        </div></div>
        <div class="demo-inline-spacing mt-3 pull-right" id="${that.objID}_opinion_button"></div>
        `;
        document.getElementById(that.data.parentObjID).insertAdjacentHTML("beforeEnd",htmlTemplate);
    }
    renderOpinionHistory(){
        let that=this;
        let html=that.renderOpinionHistoryHtml();
        document.getElementById(that.objID+'_opinion').innerHTML='';
        document.getElementById(that.objID+'_opinion').insertAdjacentHTML("beforeEnd",html);
        let buttonHtml=that.renderOpinionHistoryButtonHtml();
        document.getElementById(that.objID+'_opinion_button').innerHTML='';
        document.getElementById(that.objID+'_opinion_button').insertAdjacentHTML("beforeEnd",buttonHtml);
        //如果新增过，就隐藏输入框，没有则展示输入框
        if(that.isEdit){//isEdit逻辑判断是否展示
            if(that.opinion_edit!='N'){//opinion_edit 传入的配置是否展示
                document.getElementById(that.objID+'_opinion_edit').setAttribute('style', '');//展示编辑按钮
            }
            document.getElementById(that.objID).setAttribute('style', 'display:none');//隐藏输入框
            document.getElementById(that.objID+'_opinion_yes_agree').setAttribute('style', 'display:none');//隐藏同意按钮
            document.getElementById(that.objID+'_opinion_no_agree').setAttribute('style', 'display:none');//隐藏不编辑按钮
        }else if(that.opinion_add=='Y'){
            that.opinionEditFunc()
        }
        util.addEventListener(that.objID+'_opinion_edit','click',that.opinionEditFunc);
        util.addEventListener(that.objID+'_opinion_yes_agree','click',that.opinionYesAgreeFunc);
        util.addEventListener(that.objID+'_opinion_no_agree','click',that.opinionNoAgreeFunc);
    }
    renderOpinionHistoryHtml(){
        let opinionHtmlTemplate=``;
        let opinionData=this.opinionData;
        this.isEdit=false;//是否已经同意过，如果没有则展示意见输入框
        for (let i = 0; i < opinionData.length; i++) {
            if(opinionData[i].create_user==util.getLocalStorageItem('userName')&&this.task_id==opinionData[i].task_id){
                this.isEdit=true;
                this.editData=opinionData[i];
            }
            opinionHtmlTemplate+=`
            <tbody><tr>
            <th scope="row">${i+1}</th>
            <td>${opinionData[i].create_user}</td>
            <td>${opinionData[i].create_time}</td>
            <td>${opinionData[i].is_agree}</td>
            <td>${opinionData[i].content}</td>
            </tr></tbody>`;
        }
        let htmlTemplate=`
        <label class="col-form-label col-sm-2 text-sm-right" style="line-height:25px;vertical-align: middle;">意见</label>
        <div class="col-sm-10">
        <table class="table card-table">
        <thead class="thead-light">
        <tr>
        <th>#</th>
        <th>创建人</th>
        <th>创建时间</th>
        <th>是否同意</th>
        <th>意见内容</th>
        </tr></thead>${opinionHtmlTemplate}</table></div>`;
        return htmlTemplate;
    }
    renderOpinionHistoryButtonHtml(){
        let that=this;
        let htmlTemplate=`
        <button type="button" class="btn btn btn-primary mr-2" id="${that.objID}_opinion_edit" style="display: none">修改</button>
        <button type="button" class="btn btn btn-primary mr-2" id="${that.objID}_opinion_yes_agree" style="display: none">同意</button>
        <button type="button" class="btn btn btn-secondary mr-2" id="${that.objID}_opinion_no_agree" style="display: none">不同意</button>`;
        return htmlTemplate;
    }
    initFunc(){
        let that=this;
        this.opinionEditFunc= ()=>{
            document.getElementById(that.objID+'_opinion_edit').setAttribute('style', 'display:none');//隐藏编辑按钮
            document.getElementById(that.objID).setAttribute('style', '');//展示输入框
            document.getElementById(that.objID+'_opinion_yes_agree').setAttribute('style', '');//展示同意按钮
            document.getElementById(that.objID+'_opinion_no_agree').setAttribute('style', '');//展示不编辑按钮
            if(that.isEdit){
                document.getElementById(that.eleID).value=that.editData.content;//
            }
        }
        this.opinionYesAgreeFunc= ()=>{
            if(!that.isEdit){
                that.editData={}//如果是新增定义一个空的json
                that.editData.page_parent_uuid=that.page_parent_uuid;
            }
            that.editData.is_agree='是'
            that.editData.task_id=that.task_id;
            that.editData.content=document.getElementById(that.eleID).value
            that.saveOpinionData().then(()=>{
                document.getElementById(that.objID+'_opinion_edit').setAttribute('style', '');//展示编辑按钮
                document.getElementById(that.objID).setAttribute('style', 'display:none');//隐藏输入框
                document.getElementById(that.objID+'_opinion_yes_agree').setAttribute('style', 'display:none');//隐藏同意按钮
                document.getElementById(that.objID+'_opinion_no_agree').setAttribute('style', 'display:none');//隐藏不编辑按钮
                that.refreshOpinion();
            })
        }
        this.opinionNoAgreeFunc= ()=>{
            if(!that.isEdit){
                that.editData={}//如果是新增定义一个空的json
                that.editData.page_parent_uuid=that.page_parent_uuid;
            }
            that.editData.is_agree='否'
            that.editData.task_id=that.task_id;
            that.editData.content=document.getElementById(that.eleID).value
            that.saveOpinionData().then(()=>{
                document.getElementById(that.objID+'_opinion_edit').setAttribute('style', '');//展示编辑按钮
                document.getElementById(that.objID).setAttribute('style', 'display:none');//隐藏输入框
                document.getElementById(that.objID+'_opinion_yes_agree').setAttribute('style', 'display:none');//隐藏同意按钮
                document.getElementById(that.objID+'_opinion_no_agree').setAttribute('style', 'display:none');//隐藏不编辑按钮
                that.refreshOpinion();
            })
        }
    }
    saveOpinionData(){
        let params=this.code;
        params['saveData']=[];
        params['saveData'].push(this.editData)
        let obj={url:config.getConfigJson().saveUrl,params:params}
        return httpUtil.post(obj, (response)=>{
            khcAlert.showSuccessMessage('操作成功')
        })
    }

    /**
     * 查询最新数据，将最新数据渲染
     */
    refreshOpinion(){
        if(this.opinion_show!='Y'){
            document.getElementById(this.data.parentObjID+'div').style.display='none';
            return
        }
        let params={};
        params.dsCode='ds_opinion'
        params.queryCode='query_opinion'
        params.formCode='form_opinion'
        params.version='1.0.0'
        params.queryParam= {}
        params.queryParam.page_parent_uuid = this.page_parent_uuid;
        params.queryParam.page = 0;
        params.queryParam.pageSize = 100;
        params.queryParam.sortField = 'create_time';
        params.queryParam.sortType = 'desc';
        this.code=params;
        let that = this;
        let obj={url:config.getConfigJson().queryUrl,params:params}
        httpUtil.post(obj, (response)=>{
            //查询结果
            that.opinionData = response.data.data.content;
            that.renderOpinionHistory();
        })
    }
    hide(){
        document.getElementById(this.data.parentObjID+'div').setAttribute('style', 'display:none');//隐藏
    }
    show(){
        document.getElementById(this.data.parentObjID+'div').setAttribute('style', '');//展示
    }
}
export { vOpinion }