<template>
    <el-dialog :title="titleType + '问题标准'" @close="closeDialog" v-model="dialogFormVisible" append-to-body>
        <el-form :model="form" :rules="rules" ref="form" label-width="90px">
            <el-form-item label="专业" prop="pblmChType">
                <el-select style="width:180px;" v-model="form.pblmChType" placeholder="请选择专业" @change="professionChange">
                    <el-option
                    v-for="(item,index) in professionList"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="问题大类" prop="class1">
                <el-select style="width:180px;" v-model="form.class1" placeholder="请选择问题大类" @change="majorChange">
                    <el-option
                    v-for="(item,index) in marjorClassList"
                    :key="index"
                    :label="item.className"
                    :value="item.classCode">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="问题小类" prop="class2">
                <el-select style="width:180px;" v-model="form.class2" placeholder="请选择问题小类">
                    <el-option
                    v-for="(item,index) in subclassList"
                    :key="index"
                    :label="item.className"
                    :value="item.classCode">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="问题序号" prop="sn">
                <el-input v-model="form.sn" autocomplete="off"></el-input>
            </el-form-item>
            <!-- <el-form-item label="入库时间">
                <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="选择日期" v-model="form.intm" style="width: 100%;"></el-date-picker>
            </el-form-item> -->
            <el-form-item label="问题描述（名称）" prop="pblmsDesc">
                <el-input v-model="form.pblmsDesc" type="text" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="问题等级" prop="cate">
                <div>
                    <el-checkbox v-model="form.cate0" true-label="1" false-label="0">一般</el-checkbox>
                    <span class="add_desc" @click="appearDes0cInput = !appearDes0cInput">添加描述</span>
                    <div class="demo-input-suffix" v-if="appearDes0cInput">
                        描述信息：<el-input v-model="form.cate0Desc" autocomplete="off"></el-input>
                    </div>
                </div>
                <div>
                    <el-checkbox v-model="form.cate1" true-label="1" false-label="0">较重</el-checkbox>
                    <span class="add_desc" @click="appearDes1cInput = !appearDes1cInput">添加描述</span>
                    <div class="demo-input-suffix" v-if="appearDes1cInput">
                        描述信息：<el-input v-model="form.cate1Desc" autocomplete="off"></el-input>
                    </div>
                </div>
                <div>
                    <el-checkbox v-model="form.cate2" true-label="1" false-label="0">严重</el-checkbox>
                    <span class="add_desc" @click="appearDes2cInput = !appearDes2cInput">添加描述</span>
                    <div class="demo-input-suffix" v-if="appearDes2cInput">
                        描述信息：<el-input v-model="form.cate2Desc" autocomplete="off"></el-input>
                    </div>
                </div>
            </el-form-item>
            <el-form-item label="责任主体" prop="subjectIds">
                <el-select style="width:180px;" multiple v-model="form.subjectIds" popper-class="nature_select" filterable placeholder="请选择">
                    <el-option
                    v-for="item in natureList"
                    :key="item.id"
                    :label="item.subName"
                    :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="法规名称" prop="relativeLaw">
                <!-- <el-select v-model="form.relativeLaw" @change="lawChange" popper-class="nature_select" filterable placeholder="请选择">
                    <el-option
                    v-for="(item,index) in lawsList"
                    :key="index"
                    :label="item.lawKeyword"
                    :value="item.lawKeyword">
                    </el-option>
                </el-select> -->
                <el-input v-model="form.relativeLaw" type="text" autocomplete="off"></el-input>
                <!-- <el-cascader v-model="form.relativeLaw" :props="laws" popper-class="max_width" :show-all-levels="false"></el-cascader> -->
            </el-form-item>
            <el-form-item label="法规内容" prop="lawContent">
                <el-input v-model="form.lawContent" type="textarea" :rows="5" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="form.note" type="textarea" :rows="5" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="closeDialog">取 消</el-button>
            <el-button type="primary" @click="submitNewProblem">保 存</el-button>
        </div>
    </el-dialog>
</template>
<script>
import request from '@/utils/gw_ajax_request.js'
import {formatDateTime2} from '@/utils/gw_date.js'
import {getNatureListApi,submitNewProblemApi} from '@/api/InspectionReport.js'
export default {
    props:['titleType','rowData','listType'],
    data(){
        let _this = this
        const validateCate = (rule, value, callback) => {
            if ((!_this.form.cate0 || _this.form.cate0=="0") && (!_this.form.cate1 || _this.form.cate1=="0") && (!_this.form.cate2 || _this.form.cate2=="0")) {
                callback(new Error('请选择问题等级'))
            } else {
                callback()
            }
        }
        const validateDuty = (rule, value, callback) => {
            if (!value || (value && value.length==0)) {
                callback(new Error('请选择责任主体'))
            } else {
                callback()
            }
        }
        return{
            form: {
                pblmChType: '',
                class1: '',
                class2: '',
                class1Name: '',
                class2Name: '',
                sn: '',
                pblmsDesc: '',
                cate0: '',
                cate1: '',
                cate2: '',
                cate0Desc: '',
                cate1Desc: '',
                cate2Desc: '',
                subjectIds: '',
                relativeLaw: '',
                lawContent: '',
                note: ''
            },
            dialogFormVisible: true,
            professionList:[
                {label: '前期与设计',value: '1'},
                {label: '建设管理',value: '2'},
                {label: '计划下达与执行',value: '3'},
                {label: '资金使用与管理',value: '4'},
                {label: '质量行为',value: '5'},
                {label: '质量实体',value: '6'},
                {label: '工程安全',value: '7'}
            ],
            marjorClassList:[
            ],
            subclassList:[
            ],
            faguiList:[],
            problemType: [
                {label: '一般',value: '0'},
                {label: '较重',value: '1'},
                {label: '严重',value: '2'}
            ],
            appearDes0cInput: false,
            appearDes1cInput: false,
            appearDes2cInput: false,
            natureList: [],
            pcode: '',
            books: [],
            laws: {
                lazy: true,
                lazyLoad (node, resolve) {
                    const { level } = node;
                    if(level == 0){
                        // this.getBooks()
                        _this.getBooks(resolve)
                    }else if(level == 1){
                        _this.getLawIndex(resolve)
                    }else if(level == 2){
                        _this.getContentIndex(resolve,node.data.value)
                    }else if(level == 3){
                        _this.getContentSort(resolve,node.data.value,node.data.indexId)
                    }
                    // 通过调用resolve将子节点数据返回，通知组件数据加载完成
                }
            },
            lawsList: [],
            rules: {
                pblmChType: [
                    { required: true, message: '请选择专业', trigger: 'change' }
                ],
                class1: [
                    { required: true, message: '请选择问题大类', trigger: 'change' }
                ],
                class2: [
                    { required: true, message: '请选择问题小类', trigger: 'change' }
                ],
                sn: [
                    { required: true, message: '请输入问题序号', trigger: 'change' }
                ],
                pblmsDesc: [
                    { required: true, message: '请输入问题描述（名称）', trigger: 'change' }
                ],
                cate: [
                    { required: true, validator: validateCate, trigger: 'change' }
                ],
                subjectIds: [
                    { required: true, validator: validateDuty, trigger: 'change' }
                ],
                relativeLaw: [
                    { required: true, message: '请输入法规名称', trigger: 'change' }
                ],
                lawContent: [
                    { required: true, message: '请输入法规内容', trigger: 'change' }
                ],
            }
        }
    },
    computed:{
        currentOrgId(){
            return localStorage.getItem('currentOrgId') ? localStorage.getItem('currentOrgId') : ''
        }
    },
    mounted(){
        // this.getlawsList()
        if(this.titleType == '编辑'){
            // this.form = this.rowData
            for(var i in this.form){
                this.form[i] = this.rowData[i]
            }
            this.form['id'] = this.rowData['id']
            this.form.intm = formatDateTime2(this.form.intm)
            this.form.subjectIds = this.form.subjectIds ? this.rowData.subjectIds.split(',') : ''
            this.professionChange()
        }else{
            if(this.listType){
                this.form.pblmChType = this.listType
                this.professionList = this.professionList.filter(ele => {
                    if(this.listType == '5'){
                        return ele.value == '5' || ele.value == '6'
                    }else if(this.listType == '6'){
                        this.form.pblmChType = '7'
                        return ele.value == '7'
                    }else{
                        return ele.value == this.listType
                    }
                })
            }else{
                this.form.pblmChType = ''
            }
            this.professionChange()
        }
        this.$nextTick(()=>{
          this.$refs["form"].clearValidate()
        })
    },
    methods:{
        professionChange(){
            this.getMajorClassList()
            this.getNatureList()
        },
        getMajorClassList(){
            let majorParams = {
                spfsnlChType: this.form.pblmChType,
                type: '1',
                pageSize: 1000,
            }
            request.post(`/tac/pblm/class/dic/page`,majorParams).then(res=>{
                if(res.success){
                    this.marjorClassList = res.data.list
                    if(this.titleType == '编辑'){
                        this.majorChange(this.form.class1)
                    }
                }else{
                    this.$message.error(res.message)
                }
            })
        },
        majorChange(val){
            this.marjorClassList.forEach(ele=>{
                if(val == ele.classCode){
                    this.pcode = ele.id
                }
            })
            this.getSubClassList()
        },
        getSubClassList(){
            let subParams = {
                spfsnlChType: this.form.pblmChType,
                type: '2',
                pageSize: 1000,
                pcode: this.pcode
            }
            request.post(`/tac/pblm/class/dic/page`,subParams).then(res=>{
                if(res.success){
                    this.subclassList = res.data.list
                }else{
                    this.$message.error(res.message)
                }
            })
        },
        //获取单位性质列表
        getNatureList(){
            let _this = this
            let data = {}
            if(this.currentOrgId == '034'){
                data = {
                    note: ''
                }
            }else{
                data = {
                    note: this.form.pblmChType
                }
            }
            getNatureListApi(data).then(res=>{
                if(res.success){
                    let natureListFilter = []
                    this.natureList = res.data
                    // natureListFilter = res.data
                    // natureListFilter.forEach(ele=>{
                    //     if(ele.note.indexOf(_this.form.pblmChType) > -1){
                    //         _this.natureList.push(ele)
                    //     }
                    // })
                }else{
                    this.$message.error(res.message)
                }
            }).catch(err=>{
                this.$message.error(err)
            })
        },
        getlawsList(){
            request.post(`/tac/slb/law/index/list`,{}).then(res=>{
                if(res.success){
                    this.lawsList = res.data
                }else{
                    this.$message.error(res.message)
                }
            }).catch(err=>{
                this.$message.error(err)
            })
        },
        lawChange(val){
            this.lawsList.forEach(ele=>{
                if(val == ele.lawKeyword){
                    this.form.lawContent = ele.bookContent.substr(0,300)
                }
            })
        },
        submitNewProblem(){
            this.$refs['form'].validate((valid) => {
                if(valid){
                    this.marjorClassList.forEach(ele=>{
                        if(this.form.class1 == ele.classCode){
                            this.form.class1Name = ele.className
                        }
                    })
                    this.subclassList.forEach(ele=>{
                        if(this.form.class2 == ele.classCode){
                            this.form.class2Name = ele.className
                        }
                    })
                    // this.form.relativeLaw = this.form.relativeLaw[3]
                    this.form.subjectIds = this.form.subjectIds.join(',')
                    this.form['pblmTypeDesc'] = '其他'
                    submitNewProblemApi(this.form).then(res=>{
                        if(res.success){
                            if(this.titleType == '编辑'){
                                this.$message.success('修改成功')
                            }else{
                                this.$message.success('添加成功')
                            }
                            this.$emit('dialogClose')
                        }else{
                            this.$message.error(res.message)
                        }
                    }).catch(err=>{
                        this.$message.error(err)
                    })
                } else {
                    return false;
                }
            })
        },
        getFaguiList(){
            request.get(`/tac/slb/law/index/list/1`).then(res=>{
                if(res.success){
                    this.faguiList = res.data
                }else{
                    this.$message.error(res.message)
                }
            })
        },
        closeDialog(){
            this.$emit('dialogClose')
        },
        getBooks(resolve){
            request.post(`/tac/slb/law/book/list`,{tacSlbLawBookParam: {}}).then(res=>{
                if(res.success){
                    let books = res.data
                    let booksArray = []
                    books.forEach(ele=>{
                        booksArray.push({label:ele.bookName,value:ele.id})
                    })
                    resolve(booksArray)
                }else{
                    this.$message.error(res.message)
                }
            })
        },
        getLawIndex(resolve){
            request.get(`/tac/slb/law/index/list/1`).then(res=>{
                if(res.success){
                    let books = res.data
                    let booksArray = []
                    books.forEach(ele=>{
                        booksArray.push({label:ele.lawTitle,value:ele.docId})
                    })
                    resolve(booksArray)
                }else{
                    this.$message.error(res.message)
                }
            })
        },
        getContentIndex(resolve,indexId){
            request.get(`/tac/slb/law/content/list/${indexId}`).then(res=>{
                if(res.success){
                    let books = res.data
                    let booksArray = []
                    books.forEach(ele=>{
                        booksArray.push({label:ele.chapters,value:ele.sort,indexId:ele.indexId})
                    })
                    resolve(booksArray)
                }else{
                    this.$message.error(res.message)
                }
            })
        },
        getContentSort(resolve,sort,indexId){
            request.get(`/tac/slb/law/content/list/${indexId}/${sort}`).then(res=>{
                if(res.success){
                    let books = res.data
                    let booksArray = []
                    books.forEach(ele=>{
                        booksArray.push({label:ele.content,value:ele.id,leaf: true})
                    })
                    resolve(booksArray)
                }else{
                    this.$message.error(res.message)
                }
            })
        }
    },
    watch:{
    }
}
</script>
<style>
    .max_width{
        max-width: 1200px;
    }
</style>
