<script setup>
/**
 * name：
 * user：sa0ChunLuyu
 * date：2023年3月24日 11:52:52
 */
import {
  ProfitsharingCreateAction,
  ProfitsharingUpdateAction,
  ProfitsharingDeleteAction,
  ProfitsharingListAction,
  HospitalProfitsharingStatusAction,
  ProfitsharingTestAction,
  $response
} from '~/api'
import {h} from "vue";
import {NTag} from "naive-ui";

const $props = defineProps({
  hospital: {
    type: Number,
    default: 0
  }
})
const profitsharing_status = ref(false)
const profitsharing_type = ref(0)
const HospitalProfitsharingStatus = async () => {
  const response = await HospitalProfitsharingStatusAction({
    hospital: $props.hospital
  })
  $response(response, () => {
    profitsharing_status.value = response.data.info.open === 1 || response.data.info.open === 3
    profitsharing_type.value = response.data.info.open
  })
}
const profitsharing_list = ref([])
const ProfitsharingList = async () => {
  const response = await ProfitsharingListAction({
    hospital: $props.hospital
  })
  $response(response, () => {
    profitsharing_list.value = response.data.list
  })
}
const profitsharing_active = ref([])
const profitsharing_columns = [{
  type: 'selection'
}, {
  title: '名称',
  key: 'name'
}, {
  title: '类型',
  key: 'status',
  render(row) {
    return h(
      NTag,
      {
        type: row.type === 1 ? 'success' : 'error'
      },
      {
        default: () => {
          return row.type === 1 ? '商户' : '个人'
        }
      }
    )
  }
}, {
  title: '账号',
  key: 'account'
}, {
  title: '计算公式',
  key: 'formula'
}, {
  title: '描述',
  key: 'desc'
}, {
  title: '状态',
  key: 'status',
  render(row) {
    return h(
      NTag,
      {
        type: row.status === 1 ? 'success' : 'error'
      },
      {
        default: () => {
          return row.status === 1 ? '可用' : '停用'
        }
      }
    )
  }
}]

const create_show = ref(false)
const create_data_default = {
  type: 1,
  name: '',
  account: '',
  formula: '',
  desc: '',
  status: 1,
}
const create_data = ref(JSON.parse(JSON.stringify(create_data_default)))
const test_data = ref({
  in_value: 0,
  out_value: 0
})
const ProfitsharingTest = async () => {
  let data = {
    in_value: test_data.value.in_value
  }
  if (create_show.value) {
    data.php = create_data.value.formula
  } else {
    data.php = update_data.value.formula
  }
  const response = await ProfitsharingTestAction(data)
  $response(response, () => {
    test_data.value.out_value = response.data.out_value
  })
}
const ProfitsharingCreate = async () => {
  let data = {
    ...create_data.value,
    hospital: $props.hospital
  }
  const response = await ProfitsharingCreateAction(data)
  $response(response, () => {
    window.$message().success(response.message)
    create_show.value = false
    create_data.value = JSON.parse(JSON.stringify(create_data_default))
    ProfitsharingList()
  })
}
const createShowClick = () => {
  create_data.value = JSON.parse(JSON.stringify(create_data_default))
  create_show.value = true
}
const update_show = ref(false)
const update_data_default = {
  id: 0,
  type: 1,
  name: '',
  account: '',
  formula: '',
  desc: '',
  status: 1,
}
const update_data = ref(JSON.parse(JSON.stringify(update_data_default)))
const updateShowClick = () => {
  if (profitsharing_active.value.length !== 1) return window.$message().error('请选择一个分账设置')
  update_data.value = JSON.parse(JSON.stringify(update_data_default))
  for (let i = 0; i < profitsharing_list.value.length; i++) {
    if (profitsharing_active.value[0] === profitsharing_list.value[i].id) {
      update_data.value = JSON.parse(JSON.stringify(profitsharing_list.value[i]))
      update_show.value = true
      return
    }
  }
}
const ProfitsharingUpdate = async () => {
  let data = {
    ...update_data.value,
    hospital: $props.hospital
  }
  const response = await ProfitsharingUpdateAction(data)
  $response(response, () => {
    window.$message().success(response.message)
    update_show.value = false
    update_data.value = JSON.parse(JSON.stringify(update_data_default))
    ProfitsharingList()
  })
}
const delete_show = ref(false)
const deleteShowClick = () => {
  if (profitsharing_active.value.length <= 0) return window.$message().error('请选择一个分账设置')
  delete_show.value = true
}
const ProfitsharingDelete = async () => {
  const response = await ProfitsharingDeleteAction(profitsharing_active.value)
  $response(response, () => {
    window.$message().success(response.message)
    delete_show.value = false
    profitsharing_active.value = []
    ProfitsharingList()
  })
}

onMounted(() => {
  HospitalProfitsharingStatus()
  ProfitsharingList()
})
</script>
<template>
  <div>
    <n-modal v-model:show="delete_show" preset="card" :style="{width: '400px'}" title="删除确认" :auto-focus="false"
             :bordered="false">
      <div>
        <n-space justify="center">
          <n-button @click="ProfitsharingDelete()" type="info">确定</n-button>
          <n-button @click="delete_show = false">取消</n-button>
        </n-space>
      </div>
    </n-modal>
    <n-modal v-model:show="update_show" preset="card" :style="{width: '700px'}" title="修改信息" :auto-focus="false"
             :bordered="false">
      <div>
        <n-space align="center">
          <n-tag>
            <div class="form_tag_wrapper">名称</div>
          </n-tag>
          <n-input class="form_input_wrapper" v-model:value="update_data.name"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">类型</div>
          </n-tag>
          <n-radio-group v-model:value="update_data.type" name="update_type_radio">
            <n-space>
              <n-radio :value="1">商户</n-radio>
              <n-radio :value="2">个人</n-radio>
            </n-space>
          </n-radio-group>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">账号</div>
          </n-tag>
          <n-input class="form_input_wrapper" v-model:value="update_data.account"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">分账公式</div>
          </n-tag>
          <n-input :autosize="{
        minRows: 10,
        maxRows: 10
      }" type="textarea" class="form_textarea_wrapper"
                   v-model:value="update_data.formula"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">分账测试</div>
          </n-tag>
          <n-input class="form_input_wrapper" v-model:value="test_data.in_value" placeholder="入参"></n-input>
          <n-button @click="ProfitsharingTest()" type="info">计算</n-button>
          <n-input class="form_input_wrapper" v-model:value="test_data.out_value" placeholder="测试结果"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">分账描述</div>
          </n-tag>
          <n-input class="form_input_wrapper" v-model:value="update_data.desc"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">状态</div>
          </n-tag>
          <n-radio-group v-model:value="update_data.status" name="update_status_radio">
            <n-space>
              <n-radio :value="1">可用</n-radio>
              <n-radio :value="2">停用</n-radio>
            </n-space>
          </n-radio-group>
        </n-space>
        <n-button @click="ProfitsharingUpdate()" type="info" mt-2>确定</n-button>
      </div>
    </n-modal>
    <n-modal v-model:show="create_show" preset="card" :style="{width: '700px'}" title="新建" :auto-focus="false"
             :bordered="false">
      <div>
        <n-space align="center">
          <n-tag>
            <div class="form_tag_wrapper">名称</div>
          </n-tag>
          <n-input class="form_input_wrapper" v-model:value="create_data.name"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">类型</div>
          </n-tag>
          <n-radio-group v-model:value="create_data.type" name="update_type_radio">
            <n-space>
              <n-radio :value="1">商户</n-radio>
              <n-radio :value="2">个人</n-radio>
            </n-space>
          </n-radio-group>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">账号</div>
          </n-tag>
          <n-input class="form_input_wrapper" v-model:value="create_data.account"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">分账公式</div>
          </n-tag>
          <n-input :autosize="{
        minRows: 10,
        maxRows: 10
      }" type="textarea" class="form_textarea_wrapper"
                   v-model:value="create_data.formula"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">分账测试</div>
          </n-tag>
          <n-input class="form_input_wrapper" v-model:value="test_data.in_value" placeholder="入参"></n-input>
          <n-button @click="ProfitsharingTest()" type="info">计算</n-button>
          <n-input class="form_input_wrapper" v-model:value="test_data.out_value" placeholder="测试结果"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">分账备注</div>
          </n-tag>
          <n-input class="form_input_wrapper" v-model:value="create_data.desc"></n-input>
        </n-space>
        <n-space mt-2 align="center">
          <n-tag>
            <div class="form_tag_wrapper">状态</div>
          </n-tag>
          <n-radio-group v-model:value="create_data.status" name="update_status_radio">
            <n-space>
              <n-radio :value="1">可用</n-radio>
              <n-radio :value="2">停用</n-radio>
            </n-space>
          </n-radio-group>
        </n-space>
        <n-button @click="ProfitsharingCreate()" type="info" mt-2>确定</n-button>
      </div>
    </n-modal>

    <n-card mt-2 title="分账管理">
      <div>
        <n-alert v-if="profitsharing_type !== 3" title="如果使用微信分账，使用前请务必前往微信支付后台开通分账功能，且配置管理分账接收方。接收方商家 - 分账接收设置 - 分账回退设置
          需保持开启状态。" type="warning"></n-alert>
        <n-alert v-else title="当前模式为站内分账，下列数据只做展示，后续金额需要手动进行分账。" type="warning"></n-alert>
        <n-alert mt-2 v-if="!profitsharing_status" title="请先前往「额外配置」标签开启分账功能" type="error"></n-alert>
        <div v-else mt-2>
          <n-space>
            <n-button @click="createShowClick()" type="success">新建</n-button>
            <n-button @click="deleteShowClick()" type="error">删除</n-button>
            <n-button @click="updateShowClick()" type="info">修改信息</n-button>
          </n-space>
        </div>
        <n-data-table mt-2 v-model:checked-row-keys="profitsharing_active" :columns="profitsharing_columns"
                      :row-key="row=>row.id" :data="profitsharing_list"/>
      </div>
    </n-card>
  </div>
</template>
<style scoped>
.form_textarea_wrapper {
  width: 300px;
}
</style>
