<template>
    <div>
        <SearchBox ref="searchBarRef" :formName="search" :formData="query" @showPopupUser="showPop" showResetButton
            @reset="reset" />
        <ButtonBar @search="handleSearch" @add="handleAdd" @remove="handleRemoveBatch" @edit="handleEditBatch"
            @upload="handleUpload" @download="handleDownload" @import="handleImport" @export="handleExport"></ButtonBar>
        <el-card :body-style="cardBodyStyle">
            <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange" style="height: 500px;">
                <el-table-column type="selection" fixed></el-table-column>
                <el-table-column type="index" label="序号" align="center" width="60">
                    <template #default="scope">
                        <span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="indiNo" label="指标编号" width="200" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="indiNm" label="指标名称" width="150" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="rewaPuni" label="奖惩" width="90" header-align="center" align="right"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column label="考核标准详情" header-align="center" align="center" width="120">
                    <template #default="scope">
                        <span :style="{ color: '#6699CC', cursor: 'pointer' }" @click="handleClick(scope.row)">
                            考核标准 <el-icon>
                                <View />
                            </el-icon>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" header-align="center" align="center" width="200px">
                    <template #default="scope">
                        <el-button plain type="warning" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button plain type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-card :body-style="cardBodyStyle">
            <el-pagination :current-page="currentPage" :page-size="pageSize" :page-sizes="pageSizes" :total="totalRows"
                background="background" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
                @current-change="handleCurrentChange">
            </el-pagination>
        </el-card>

        <el-dialog :close-on-click-modal="false" draggable title="编辑" v-model="dialogVisible" width="800px" top="5vh"
            @open="openDialog">
            <el-form :model="form" label-width="80px" label-position="right" inline ref="ruleFormRef" :rules="rules">
                <el-form-item label="指标名称" style="white-space: nowrap;" prop="indiNm">
                    <el-input v-model="form.indiNm" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="奖惩" style="white-space: nowrap;" prop="rewaPuni">
                    <el-input v-model="form.rewaPuni" clearable style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item v-for="(item, index) in form.list" :key="index" :prop="'list.' + index + '.asseStdNm'"
                    label="考核标准" style="white-space: nowrap;">
                    <el-row>
                        <el-form-item :prop="'list.' + index + '.asseStdNm'" :rules="rules.asseStdNm" style="margin-right: 20px;">
                            <!-- <el-input v-model="item.asseStdNm" clearable style="width: 140px"
                                placeholder="考核标准样品名称"></el-input> -->
                            <el-select v-model="item.asseStdNm" clearable style="width: 160px" placeholder="考核标准样品名称">
                                <el-option label="原矿" value="原矿" />
                                <el-option label="铁精矿" value="铁精矿" />
                                <el-option label="铜精矿" value="铜精矿" />
                                <el-option label="硫精矿" value="硫精矿" />
                                <el-option label="尾矿" value="尾矿" />
                            </el-select>
                        </el-form-item>
                        <el-form-item :prop="'list.' + index + '.asseStdSx'" :rules="rules.asseStdSx" style="margin-right: 5px;">
                            <!-- <el-input v-model="item.asseStdSx" clearable style="width: 140px"
                                placeholder="考核标准样品属性"></el-input> -->
                                <el-select v-model="item.asseStdSx" clearable style="width: 160px" placeholder="考核标准样品属性">
                                <el-option label="TFe" value="TFe" />
                                <el-option label="Cu" value="Cu" />
                                <el-option label="S" value="S" />
                                <el-option label="Co" value="Co" />
                            </el-select>
                        </el-form-item>
                        <el-form-item :prop="'list.' + index + '.asseStdFlag'" :rules="rules.asseStdFlag" style="margin-right: 5px;">
                            <el-select v-model="item.asseStdFlag" clearable style="width: 60px" placeholder="">
                                <el-option label=">" value="0" />
                                <el-option label="<" value="1" />
                                <el-option label="≥" value="2" />
                                <el-option label="≤" value="3" />
                            </el-select>
                        </el-form-item>
                        <el-form-item :prop="'list.' + index + '.asseStd'" :rules="rules.asseStd" style="margin-right: 20px;">
                            <el-input v-model="item.asseStd" clearable style="width: 140px"
                                placeholder="考核标准值"></el-input>
                        </el-form-item>
                        <el-button type="danger" @click="deleteAsseStd(index)" plain>删除</el-button>
                    </el-row>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button color="#626aef" @click="addAsseStd()">新增考核标准</el-button>
                    <el-button type="success" @click="handleSave(ruleFormRef)">保存</el-button>
                    <el-button type="danger" @click="handleCancelSave">取消</el-button>
                </div>
            </template>
        </el-dialog>
        <el-dialog :close-on-click-modal="false" draggable title="详情" v-model="detailDialogVisible" width="800px"
            top="5vh" close="detailClose">
            <el-descriptions>
                <el-descriptions-item label="指标名称：">{{ form.indiNm }}</el-descriptions-item>
                <el-descriptions-item label="奖惩：">{{ form.rewaPuni }}</el-descriptions-item>
            </el-descriptions>
            <el-descriptions column="4" style="margin-top: 20px">
                <el-descriptions-item :label="item.asseStdNm+':'" v-for="(item, index) in form.list" :key="index">
                    {{ item.asseStdSx }}
                    <span v-if="item.asseStdFlag=='1'"> &lt;</span>
                    <span v-else-if="item.asseStdFlag=='0'"> &gt;</span>
                    <span v-else-if="item.asseStdFlag=='2'"> ≥</span>
                    <span v-else-if="item.asseStdFlag=='3'"> ≤</span>
                    <span v-else>{{ item.asseStdFlag }}</span>
                    {{ item.asseStd }}
                </el-descriptions-item>
            </el-descriptions>
        </el-dialog>

    </div>
</template>

<script setup>
import ButtonBar from '@/components/ButtonBar.vue'

import {
    ref,
    reactive,
    onMounted,
    getCurrentInstance
} from 'vue'

const {
    proxy
} = getCurrentInstance()

import tools from '@/tools'
const cardBodyStyle = ref(tools.style.card)
const inputStyle = ref(tools.style.input)

onMounted(() => {
    handleSearch()
})

const detailDialogVisible = ref(false)
const handleClick = (row) =>{
    detailDialogVisible.value = true
    form.value.indiNo = row.indiNo
    form.value.indiNm = row.indiNm
    form.value.rewaPuni = row.rewaPuni
    // form.value.list=[]
    getFormList(row.indiNo)
}
const detailClose = () => {
    form.value = {
        indiNo: '',
        rewaPuni: '',
        indiNm: '',
        list: [{asseStdNm: '',
            asseStdFlag: '',
            asseStd: '', 
            asseStdSx: ''}]
    }
    detailDialogVisible.value = false
}
const getFormList = (indiNo) => {
    var url = 'prd/IndiAsseStd/getListByIndiNo'
    var param = {
        indiNo: indiNo
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            console.log(response.data.data)
            form.value.list = response.data.data
            form.value.list.forEach(a => {
                a.asseStdFlag = a.asseStdFlag.toString()
            });
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
const addAsseStd = () => {
    form.value.list.push({
        asseStdNm: '',
        asseStdFlag: '',
        asseStd: '',
        asseStdSx: ''
    })
}
const deleteAsseStd = (a) => {
    console.log(a)
    form.value.list.splice(a, 1)
}


/**
 *   查询
 */

const query = ref({
    indiNm: ''
})
const tableData = ref([])
const handleSearch = () => {
    currentPage.value = 1
    getTableData()
}

const getTableData = () => {
    var url = 'prd/IndiAsseStd/query'
    var param = {
        page: currentPage.value,
        rows: pageSize.value,
        params: JSON.stringify(query.value)
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            tableData.value = response.data.data.records
            totalRows.value = response.data.data.total
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
/**
 *   查询
 */

/**
 * 分页
 */
const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([5, 10, 20, 30, 40])
const pageSize = ref(20)
const handleSizeChange = (val) => {
    pageSize.value = val
    getTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    getTableData()
}
/**
 * 分页
 */


/**
 * 新增
 */
const dialogVisible = ref(false)
const form = ref({
    indiNo: '',
    rewaPuni: '',
    indiNm: '',
    list: [{
        asseStdNm: '',
        asseStdFlag: '',
        asseStd: '',
        asseStdSx: ''
    }]
})
const ruleFormRef = ref()
const rules = reactive({
    indiNm: [{
        required: true,
        message: '请输入指标名称',
        trigger: 'change' | 'blur'
    },{ max: 20, message: '长度过长' , trigger: 'blur'}],
    rewaPuni: [{
        required: true,
        message: '奖惩不能为空',
        trigger: 'change' | 'blur'
    },{ pattern: /^-9999999999(\.\d{1,10})?$|^[-+]?\d{1,10}(\.\d{1,10})?$|^[0-9]{1,10}(\.\d{1,10})?$/, message: '请输入有效的数字', trigger: 'blur'}],
    asseStdNm: [
        { required: true, message: '请输入', trigger: 'blur' },
        { max: 20, message: '长度过长' , trigger: 'blur'}
      ],
      asseStdSx: [
        { required: true, message: '请输入', trigger: 'blur' },
        { max: 20, message: '长度过长' , trigger: 'blur'}
      ],
      asseStdFlag: [
        { required: true, message: '请选择', trigger: 'change' }
      ],
      asseStd: [
        { required: true, message: '请输入', trigger: 'blur' },
        { pattern: /^[0-9]{1,15}(\.[0-9]{1,10})?$/, message: '请输入有效的数字', trigger: 'blur'}
      ]
})

const handleAdd = () => {
    form.value = {
        indiNo: '',
        rewaPuni: '',
        indiNm: '',
        list: [{
            asseStdNm: '',
            asseStdFlag: '',
            asseStd: '',
            asseStdSx: ''
        }]
    }
    if (ruleFormRef.value) {
            ruleFormRef.value.resetFields()
        }
    dialogVisible.value = true
}
/**
 * 新增
 */


/**
 * 保存、修改、删除
 */

const openDialog = () => {
}

const handleSave = (ruleFormRef) => {
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'prd/IndiAsseStd/save'
            var param = {
                json: JSON.stringify(form.value),
            }
            proxy.$axios.post(url, param).then(response => {
                if (response.data.code == 0) {
                    form.value = response.data.data
                    ElMessage({
                        message: '保存成功',
                        type: 'success',
                    })
                    getTableData()
                    form.value = {
                        indiNo: '',
                        rewaPuni: '',
                        indiNm: '',
                        list: [{
                            asseStdNm: '',
                            asseStdFlag: '',
                            asseStd: '',
                            asseStdSx: ''
                        }]
                    }
                    dialogVisible.value = false
                } else {
                    // ElMessage.error('操作失败！' + response.data.msg)
                    ElMessage({
                        message: '操作失败' + response.data.msg,
                        type: 'error'
                    })
                }
            })

        } else {
            console.log('error submit!', fields)
        }
    })
}
const handleCancelSave = () => {
    dialogVisible.value = false
}
const handleEdit = (row) => {
    dialogVisible.value = true
    form.value = row
    getFormList(row.indiNo)
}
const handleDelete = (row) => {
    ElMessageBox.confirm(
        '确定删除本条记录?',
        '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
    }
    )
        .then(() => {
            var url = 'prd/IndiAsseStd/remove'
            var param = {
                id: row.indiNo
            }
            proxy.$axios.post(url, param).then(response => {
                if (response.data.code == 0) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getTableData()
                } else {
                    ElMessage({
                        type: 'error',
                        message: '删除失败' + response.data.msg
                    });
                }
            });
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}

/**
 * 保存、修改、删除
 */

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

const handleRemoveBatch = () => {
    console.log('handleRemoveBatch')

    if (multipleSelection.value.length < 1) {
        ElMessage({
            type: 'warning',
            message: '请选择要删除的数据!'
        })
        return
    }

    ElMessageBox.confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        var ids = []
        multipleSelection.value.forEach((item, index) => {
            ids.push(item.indiNo)
        })
        var url = 'prd/IndiAsseStd/removeBatch'
        var param = {
            ids: JSON.stringify(ids)
        }
        proxy.$axios.post(url, param).then(response => {
            if (response.data.code == '0') {
                ElMessage({
                    type: 'success',
                    message: '操作成功!',
                });
                getTableData()
                dialogVisible.value = false
            } else {
                ElMessage({
                    type: 'error',
                    message: '操作失败!' + response.data.msg
                });
            }
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}

const handleEditBatch = () => {
    console.log('handleEditBatch')
    if (multipleSelection.value.length != 1) {
        ElMessage({
            type: 'warning',
            message: '请选择一条数据编辑!'
        })
        return
    }
    handleEdit(multipleSelection.value[0])
}

const handleUpload = () => {
    console.log('handleUpload')
}

const handleDownload = () => {
    console.log('handleDownload')
}

const handleImport = () => {
    console.log('handleImport')
}

const handleExport = () => {
    console.log('handleExport')
}

</script>

<style></style>