<template>
  <div>
    <CostAuditTable
      :table-data="surveyData.list"
      :columns="getSurveyColumns()"
      :show-index="true"
      :show-action-column="true"
    ></CostAuditTable>
    <!-- 成本调查表查看弹窗 -->
    <SurveyDialog
      :dialog-visible="contentEditDialogVisible"
      :dialog-title="contentEditDialogTitle"
      :form-data="contentEditForm"
      :disabled="contentEditDisabled"
      @cancel="handleContentEditCancel"
    />
  </div>
</template>
<script>
  import CostAuditTable from '@/components/costAudit/CostAuditTable.vue'
  import SurveyDialog from '@/views/costAudit/baseInfo/catalogManage/surveyDialog.vue'
  export default {
    components: {
      CostAuditTable,
      SurveyDialog,
    },
    props: {
      // 父组件传递的参数
      project: {
        type: Object,
        default: () => {},
      },
      isView: {
        type: Boolean,
        default: false,
      },
      surveyData: {
        type: Object,
        default: () => {},
      },
    },
    data() {
      return {
        // 内容编辑弹窗相关
        contentEditDialogVisible: false,
        contentEditDialogTitle: '内容维护',
        contentEditDisabled: true,
        contentEditForm: {
          surveyTemplateName: '',
          templateType: '1',
          // 单记录列表
          tableHeaders: [],
          // 固定表列表
          fixedTable: [],
          // 动态表列表
          dynamicTable: [],
        },
      }
    },
    mounted() {
      // this.getSurveyData()
    },

    methods: {
      // 获取带操作按钮的表格列配置
      getSurveyColumns() {
        const columns = JSON.parse(
          JSON.stringify(this.surveyData.surveyColumns || [])
        )
        const actionColumn = columns.find((col) => col.prop === 'action')
        if (actionColumn) {
          actionColumn.actions = [
            {
              name: 'view',
              label: '查看模板',
              type: 'text',
              onClick: this.handleViewTemplate,
            },
          ]
        }
        return columns
      },
      // 查看成本调查表内容弹窗
      handleViewTemplate(data) {
        // 设置表单数据
        this.contentEditForm = {
          surveyTemplateName: data.surveyTemplateName || '',
          templateType: data.templateType || '',
          data:
            {
              ...data,
              surveyId: data.surveyTemplateId,
            } || {},
        }
        // 设置弹窗标题
        this.contentEditDialogTitle = '查看'
        // 显示弹窗
        this.contentEditDialogVisible = true
      },
      // 关闭内容编辑弹窗
      handleContentEditCancel() {
        this.contentEditDialogVisible = false
        // 重置表单数据
        this.contentEditForm = {
          surveyTemplateName: '',
          templateType: '1',
          tableHeaders: [],
          fixedTable: [],
          dynamicTable: [],
        }
      },
    },
  }
</script>
<style lang="scss" scoped></style>
