<template>
    <div class="app-container">
      <a-form :model="queryForm" ref="queryFormRef" layout="inline">
  
  <a-form-item field="doctor_name" label="医生姓名">
    <a-input :style="{ width: '182px' }" v-model="queryForm.doctor_name" placeholder="请输入姓名" @press-enter="handleQuery" />
  </a-form-item>
  <a-form-item field="mobile" label="电话号码">
    <a-input :style="{ width: '182px' }" v-model="queryForm.mobile" placeholder="请输入电话号码" @press-enter="handleQuery" />
  </a-form-item>

  <a-form-item>
    <a-space>
      <a-button type="primary" @click="handleQuery"><icon-search /> 搜索</a-button>
      <a-button @click="handleResetQuery"><icon-loop /> 重置</a-button>
    </a-space>
  </a-form-item>
</a-form>

<a-divider />
      <!-- action -->
      <div class="action">
        <a-space>
          <a-button v-has="'admin:sysVisitconfigList:add'" type="primary" @click="handleAdd"><icon-plus /> 新增 </a-button>
          <!-- <a-button v-has="'admin:sysFamilyList:remove'" type="primary" status="danger"><icon-delete /> 批量删除 </a-button>  -->
        </a-space>
      </div> 
  
      <!-- table -->
      <a-table :columns="columns" :data="tableData" 
      :scroll="{ x: 1500 }"
        :pagination="{ 'show-total': true, 'show-jumper': true, 'show-page-size': true, total: pager.total, current: currentPage }"
        row-key="package_id" @selection-change="(selection) => {deleteData = selection;console.log(selection)}"
        @page-change="handlePageChange" @page-size-change="handlepage_sizeChange">
        <template #doctor_id="{record,rowIndex}">
          <div>{{(rowIndex+1)+(pager.page-1)*pager.page_size}}</div>
        </template>
        <template #doctor_name="{record}">
          <div>{{record.user_doctor.user_name}}</div>
        </template>
        <template #department_custom_name="{record}">
          <div>{{record.user_doctor.department_custom_name}}</div>
        </template>
        <template #doctor_title="{record}">
          <div>{{formatDoctorTitle(record.user_doctor.doctor_title)}}</div>
        </template>
        <template #multi_point_status="{record}">
          <div>{{formatMultipoint(record.user_doctor.multi_point_status)}}</div>
        </template>
       
        <template #last_enable_method="{record}">
          <div class="reason" v-if="record.doctor_inquiry_config && record.doctor_inquiry_config.last_enable_method==1" >自己</div>
          <div v-else>后台</div>
        </template>
         <template #monthly_frequency="{ record }">
           <div v-if="record.monthly_frequency==0">不限次</div>
           <div v-else>{{record.monthly_frequency}}</div>
        </template>
        <template #service_rounds="{ record }">
          <div v-if="record.service_rounds==0">
            不限回合
          </div>
          <div v-else>{{ record.service_rounds}}</div>
        </template>
        <template #status="{ record }">
          <div v-if="record.doctor_inquiry_config && record.doctor_inquiry_config.is_enable==1">开启</div>
          <div v-else-if="record.doctor_inquiry_config && record.doctor_inquiry_config.is_enable==0">关闭</div>
        </template> 
        
       <!-- <template #service_price="{ record }">
          <div v-if="record.doctor_inquiry_config">{{record.doctor_inquiry_config.inquiry_price}}</div>
        </template> -->
        <template #action="{ record }">
          <a-space>
            <a-button v-has="'admin:sysVisitconfigList:edit'" type="text"
              @click="handleDetail(record)"><icon-edit />修改</a-button>
            <!-- <a-button v-has="'admin:sysFamilyList:edit'" type="text" @click="handleUpdate(record)"><icon-edit />
              修改</a-button> -->
            <!-- <a-button v-has="'admin:sysFamilyList:remove'" type="text"
              @click="() => { deleteVisible = true; deleteData = [record.doctor_id];  }"><icon-delete /> 删除</a-button> -->
          </a-space>
        </template>
      </a-table>
  
      <addVisitConfigModal ref="addChild" :modalVisible="modalVisible" :id="follow_package_id" :modalForm="modalForm" @freshList="handleQuery" @healthVisibleChange="()=>{modalVisible=false;follow_package_id='';}"></addVisitConfigModal>
      <a-modal v-model:visible="okVisible" :modal-style="{width:'320px'}" body-class="okmodal" 
        @cancel="()=>okVisible=false">
        <template #title>
          提示
        </template>
        <div>确定保存当前信息？</div>
      </a-modal>
    </div>
  </template>
  
  <script setup>
    import { reactive, ref, getCurrentInstance, onMounted, nextTick, watch, computed } from 'vue';
    import { getVisitConfigList,visitConfigDetail} from '@/api/inquiry/service';
    import {formatDoctorTitle,formatMultipoint} from "@/utils/format"
    // Akiraka 20230210 删除数据
    const deleteData = ref([])
    // Akiraka 20230210 删除对话框
    const deleteVisible = ref(false)
    // Akiraka 20230210 监听删除事件
    const okVisible = ref(false);
   const follow_package_id=ref("");
    watch(() => deleteVisible.value, (value) => {
      if (value == false) {
        getInquiryInfo(pager);
      }
    });
  
    const addChild = ref(null);
    const { proxy } = getCurrentInstance();
    const currentPage = ref(1);
    // Pager
    const pager = {
      total: 0,
      page: 1,
      page_size: 10,
    };
    // form
    const queryForm = reactive({
  
    });
    const modalForm = reactive({

    });
    // Modal
    const modalVisible = ref(false);
    const modalTitle = ref('默认标题');
   
    // Batch Del List
    let batchList = [];
  
    // Table Columns
    const columns = [
      { title: '编号', dataIndex: 'doctor_id', slotName: 'doctor_id', width: '90' },
      { title: '医生姓名', dataIndex: 'doctor_name',slotName:'doctor_name',width:180 },
      { title: '医生科室', dataIndex: 'department_custom_name',slotName:'department_custom_name',width:180 },
      { title: '职称', dataIndex: 'doctor_title',slotName:'doctor_title',width:180 },
      { title: '多点执业', dataIndex: 'multi_point_status',slotName:'multi_point_status',width:180 },
      // { title: '服务名称', dataIndex: 'inquiry_mode',slotName:'inquiry_mode',width:180 },
      { title: '每月次数', dataIndex: 'monthly_frequency',slotName:'monthly_frequency', width:130 },
      { title: '服务回合数', dataIndex: 'service_rounds', slotName:'service_rounds', width: 150 },
      //  { title: '服务价格(元)', dataIndex: 'service_price',slotName:'service_price',width:80 },
       { title: '启用状态', dataIndex: 'status',slotName:'status',width:80 },
       { title: '申请人', dataIndex: 'last_enable_method',slotName:'last_enable_method',width:80 },
      { title: '操作', slotName: 'action', fixed: "right", width: 180 },
    ];
  
    // Table Data
    const tableData = ref([]);
    //弹框状态
    const modalSatus = ref('add');
  
    // 新增Satus
    const handleAdd = () => {
      modalVisible.value = true;
      follow_package_id.value='';
      modalForm.follow_package_id='';
      modalForm.doctor_config_follow_package_item=[];
      modalForm.doctor_id=null;
      modalForm.monthly_frequency=null;
      modalForm.service_rounds=null;
      modalForm.is_enable=1;
      addChild.value.reset();

    };
  
    //详情
    const handleDetail = async (record) => {
      follow_package_id.value=record.follow_package_id;
      modalVisible.value = true;
      const {code,data}=await visitConfigDetail(record.follow_package_id);
      if(code==200){
        Object.assign(modalForm,data);
        if(data.doctor_inquiry_config){
          modalForm.is_enable=data.doctor_inquiry_config.is_enable
        }
        addChild.value.changeDoctor();
      }
    };
  
    /**
     * 分页改变
     * @param {Number} [page]
     */
    const handlePageChange = (page) => {
      pager.page = page;
  
      // 修改当前页码
      currentPage.value = page;
      getInquiryInfo({ ...pager, ...queryForm });
    };
  
    // 每页数据量
    const handlepage_sizeChange = (page_size) => {
      pager.page_size = page_size;
      getInquiryInfo({ ...pager, ...queryForm });
    };
  
    // 获取患者信息
    const getInquiryInfo = async (params = {}) => {
      const { data, code, message } = await getVisitConfigList(params);
      if (code == 200) {
        tableData.value = data.data;
        Object.assign(pager, { total: data.total, page: data.page, page_size: data.page_size });
      }
    };
  
    // 查询患者信息
    const handleQuery = async () => {
      pager.page = 1;
      const params = {
        page: pager.page,
        page_size: pager.page_size,
        ...queryForm,
      };
  
      getInquiryInfo(params);
    };
    onMounted(() => {
      getInquiryInfo(pager);
    });
    // 重置搜索
    const handleResetQuery = () => {
      proxy.$refs.queryFormRef.resetFields();
      getInquiryInfo(queryForm);
    }
    
  </script>
  
  <style lang="scss" scoped>
    .action {
      margin-bottom: 12px;
    }
  
    .okmodal div {
      text-align: center;
    }
  
    .hospital_name {
      width: 140px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  
    .headImg {
      margin-right: 20px;
      border-radius: 50%;
      width: 80px;
      height: 80px;
    }
  
    .arco-form-item-layout-horizontal:first-child,
    .arco-form-item-layout-horizontal:nth-child(2) {
      align-items: center;
    }
    .cellbox{
      margin-top: 35px;
    }
    .cellbox .cell{
      width:50%;
      border-bottom:1px dashed #efefef;
      margin-bottom: 20px;
    }
    .cellbox .cell:first-child{
      border: none;
    }
    .cell{
      .arco-form-item{
        margin-bottom: 10px;
      }
    }
    .box {
      display: flex;
      align-items: center;
    }
  
    .cert .arco-form-item-label-col {
      flex: 0 0 8px !important;
    }
  
    .red {
      display: inline-block;
      margin-right: 5px;
      font-size: 14px;
      color: red;
      margin-top: 5px;
    }
  
    .cardNum {
      width: 148px;
    }
    .codbox{
      display: flex;
      align-items: center;
    }
    .reason{
      width:250px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  </style>