<template>
  <el-dialog class="dialogWerapper" width="80%" v-model="visibleDialog" title="选择公司" @close="handleClose">
    <div>
      <MyTable 
        :tableData="pageData.tableData" 
        :tableColumn="pageData.tableColumn" 
        :tableHeight="pageData.tableHeight"
        :key="pageData.keyCount" 
        :tableConfiger="pageData.tableConfiger" 
        :controlsWidth="pageData.controlsWidth"
        @chooseData="chooseData"
      >
      </MyTable>
    </div>
    <template #footer>
      <div class="flex just-center">
        <el-button type="primary" :loading="loading" @click="handleSubmit">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import MyTable from "@/components/aboutTable/MyTable.vue";
import { ref, reactive,getCurrentInstance } from 'vue';
import { qcckPost } from "@/api/qcckApi.js";
const emits = defineEmits(['refresh']);
const { proxy } = getCurrentInstance();
const visibleDialog = ref(false);
const pageData = reactive({
  tableData: [],
  keyCount: 0,
  tableConfiger: {
    rowHieght: 61,
    showSelectType: "radio",
    loading: false,
    haveControls:false
  },
  tableHeight:500,
  pageConfiger: {
    pageSize: 20,
    pageCurrent: 1
  },
  controlsWidth: 180,
  tableColumn: [
    { label: "单位名称", prop: "dwmc" },
    { label: "信用代码", prop: "xydm" },
    { label: "场所名称", prop: "csmc" },
    { label: "场所电话", prop: "csLxdh" }
  ]
});

const formData = ref({})
const loading = ref(false)
const handleClose = () => {
  formData.value = {};
  visibleDialog.value = false;
}

const chooseData = (row) => {
  formData.value.pxgsdm =  row[0].id;
  formData.value.pxgsid =  row[0].id;
  formData.value.pxgs = row[0].dwmc;
}

const handleSubmit = async () => {
  if(!formData.value.pxgsid) return proxy.$message.error('请选择公司');
  loading.value = true;
  let data = { ...formData.value }
  data.sfzh = data.zjhm;
  await qcckPost(data, `/mosty-base/baxx/basq/edit`)
  await qcckPost(data, `/mosty-base/baxx/pxry/add`)
  loading.value = false
  proxy.$message.success('培训公司保存成功')
  emits('refresh')
  handleClose()
}

function open(row) {
  pageData.keyCount++;
  formData.value = row;
  visibleDialog.value = true;
  getList();
}

function getList () {
  pageData.tableConfiger.loading = true;
  qcckPost({ }, "/mosty-base/baxx/dwgl/list").then((res) => {
    let arr = res || []
    pageData.tableData = arr.filter(item => item.type == '02')
    pageData.tableConfiger.loading = false;
  }).catch(() => {
    pageData.tableConfiger.loading = false;
  });
}

defineExpose({
  open
})
</script>
