<!--
 * @description: 模型分析弹窗
 * @fileName: ModelAnalysis
 * @author: 17076
 * @date: 2024/6/13-下午3:22
 * @version: V1.0.0
-->

<template>
  <cs-dialog
    :dialog="dialogOption"
  >
    <div slot="content">
      <p style="margin-top: 0">分析<span style="font-weight: bold">{{ `【${caseData['caseName']}】` }}</span>，获取智能取证分析结果</p>
      <el-form :model="caseData" label-width="60px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="行为人">
              <div class="flex-row">
                <el-input v-model="caseData['lawActorName']" :disabled="!doreEdit" />
                <el-button v-show="false" :plain="!doreEdit" type="primary" style="margin-left: 10px" @click="handelDore">{{ doreEdit ? '保存' : '修改' }}</el-button>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当事人">
              <div class="flex-row">
                <el-input v-model="caseData['lawParty']" :disabled="!victimEdit" />
                <el-button v-show="false" :plain="!victimEdit" type="primary" style="margin-left: 10px" @click="handleVictim">{{ victimEdit ? '保存' : '修改' }}</el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <h4>原子指标人工判别项</h4>
      <vxe-grid ref="xTable" v-bind="gridOptions">
        <template #result_edit="{row}">
          <el-select v-model="row['atomicResult']" placeholder="请选择">
            <el-option
              v-for="item in resultOptions"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </template>
        <template #remark_edit="{row}">
          <el-input v-model="row['remark']" placeholder="请输入" />
        </template>
      </vxe-grid>
      <div style="text-align: center;margin-top: 10px">
        <el-button type="primary" plain style="width: 80px" @click="() => { dialogOption.show = false }">取消</el-button>
        <el-button type="primary" style="width: 80px" @click="handelSave">保存</el-button>
        <el-button type="primary" style="width: 80px" @click="handleExecute">确定执行</el-button>
      </div>
    </div>
  </cs-dialog>
</template>

<script>
import mixin from '@/views/mixin'
import { listCaseAtomicIndex, saveCaseAtomicResult } from '@/api/caseDetails'
export default {
  name: 'ModelAnalysis',
  mixins: [mixin],
  data() {
    return {
      dialogOption: {
        show: false,
        // width: '30%',
        top: '50px',
        hiddenFooter: true,
        title: {
          title: '确定执行模型分析'
        }
      },
      // 行为人是否可编辑
      doreEdit: false,
      // 受害人是否可编辑
      victimEdit: false,
      caseData: {},
      caseId:"",
      // 人工指标判别项
      gridOptions: {
        ...mixin.data().gridOptions,
        editConfig: {
          trigger: 'click',
          mode: 'cell'
        },
        editRules: {
          result: [{ required: true, message: '判断结果不能为空!' }]
        },
        columns: [
          { title: '序号', type: 'seq', width: '50px' },
          { title: '指标名称', field: 'indexName' },
          { title: '原子指标', field: 'automicIndexName' },
          { title: '判断结果', field: 'atomicResult', formatter: ({ cellValue }) => {
            return cellValue ? this.resultOptions.find(item => item.value === cellValue).label : ''
          }, editRender: {}, slots: { edit: 'result_edit' }},
          { title: '备注', field: 'remark', formatter: ({ cellValue }) => {
            return cellValue || ''
          }, editRender: {}, slots: { edit: 'remark_edit' }}
        ],
        data: []
      },
      resultOptions: JSON.parse(sessionStorage.getItem('judge_result'))
    }
  },
  methods: {
    // 显示弹窗
    show(data) {
      debugger
      this.caseId = data.id
      this.dialogOption.show = true
      this.caseData = data
      this.fetchData()
    },
    // 修改/保存行为人
    handelDore() {
      this.doreEdit = !this.doreEdit
    },
    // 修改/保存当事人
    handleVictim() {
      this.victimEdit = !this.victimEdit
    },
    // 保存
    async handelSave() {
      const error = await this.$refs.xTable.validate(true)
      console.log(error, 666)
      this.save()
    },
    async save() {
      const res = await saveCaseAtomicResult({
        caseAtomicIndexList: this.gridOptions.data,
        caseId: this.caseId
      })
      if (res.code === 200) {
        this.$baseMessage.success('保存成功')
        this.dialogOption.show = false
      }
    },
    // 确认执行
    async handleExecute() {
      await this.save()
      this.$emit('confirm', this.caseData)
      this.dialogOption.show = false
    },
    // 获取数据
    fetchData() {
      const params = {
        caseId: this.caseId
      }
      listCaseAtomicIndex(params).then(res => {
        this.gridOptions.data = res.data
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
