<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true"> </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span>服务商信息列表</span>
        </div>
      </template>
      <el-table v-loading="loading" :data="smsServiceConfigList">
        <el-table-column v-if="false" label="" align="center" prop="definitionId" />
        <el-table-column label="服务商名称" align="center" prop="providerName" />
        <el-table-column label="服务商状态" align="center" prop="serviceStatus">
          <template #default="scope">
            <dict-tag :options="msg_service_status" :value="scope.row.serviceStatus" />
          </template>
        </el-table-column>
        <el-table-column label="计费方式" align="center" prop="meterType">
          <template #default="scope">
            <dict-tag :options="msg_service_charging" :value="scope.row.meterType" />
          </template>
        </el-table-column>
        <el-table-column label="前后缀方式" align="center" prop="fixType">
          <template #default="scope">
            <dict-tag :options="msg_fix_type" :value="scope.row.fixType" />
          </template>
        </el-table-column>
        <el-table-column label="前缀" align="center" prop="prefix" />
        <el-table-column label="后缀" align="center" prop="suffix" />
        <el-table-column label="每条短信字数" align="center" prop="wordCount" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="信息变更" placement="top">
              <el-button
                v-hasPermi="['msg:smsServiceConfigAudit:viewdetail']"
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row, 'audit')"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="80%" append-to-body>
      <el-form ref="smsServiceConfigFormRef" :model="form" :rules="rules" :inline="true" label-width="100px">
        <el-row>
          <el-form-item label="服务商名称" prop="providerName">
            <el-input v-model="form.providerName" placeholder="请输入服务商名称" disabled />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="API 地址" prop="apiAddress">
            <el-input v-model="form.apiAddress" placeholder="请输入API 地址" style="width: 500px" disabled />
          </el-form-item>
        </el-row>
        <el-form-item label="AccessKeyID" prop="accessKeyId">
          <el-input v-model="form.accessKeyId" placeholder="请输入Access Key ID" disabled />
        </el-form-item>
        <el-form-item label="AccessKeySecret" prop="accessKeySecret" disabled>
          <el-input v-model="form.accessKeySecret" placeholder="请输入Access Key Secret" disabled />
        </el-form-item>
        <el-row>
          <el-form-item label="状态" prop="serviceStatus">
            <el-select v-model="form.serviceStatus" placeholder="请选择状态">
              <el-option v-for="item in msg_service_status" :key="item.id" :value="item.value" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="前后缀类型" prop="fixType">
            <el-select v-model="form.fixType" placeholder="请选择状态">
              <el-option v-for="item in msg_fix_type" :key="item.id" :value="item.value" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="form.fixType === '2' || form.fixType === '4'" label="前缀" prop="prefix">
            <el-input v-model="form.prefix" placeholder="请输入前缀" />
          </el-form-item>
          <el-form-item v-if="form.fixType === '3' || form.fixType === '4'" label="后缀" prop="suffix">
            <el-input v-model="form.suffix" placeholder="请输入后缀" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="每条短信字数" prop="wordCount">
            <el-input-number v-model="form.wordCount" :precision="0" :step="1" :max="500" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="计费方式" prop="fixType">
            <el-select v-model="form.meterType" placeholder="请选择状态">
              <el-option v-for="item in msg_service_charging" :key="item.id" :value="item.value" :label="item.label"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item v-if="form.meterType === '1'" label="每条单价（元）" prop="unitPrice">
            <el-input-number v-model="form.unitPrice" :precision="2" :step="0.01" :max="0.5" />
          </el-form-item>

          <el-form-item v-if="form.meterType === '2'" label="每包数量（个）" prop="packageQuantity">
            <el-input-number v-model="form.packageQuantity" :precision="0" :step="1" />
          </el-form-item>

          <el-form-item v-if="form.meterType === '2'" label="每包价格（元）" prop="packagePrice">
            <el-input-number v-model="form.packagePrice" :precision="2" :step="0.01" />
          </el-form-item>
        </el-row>
      </el-form>

      <el-table v-if="form.meterType === '3'" :data="servicePiceList">
        <el-table-column label="起始数量" align="center" prop="tierStart">
          <template #default="scope">
            <el-input v-model="scope.row.tierStart" placeholder="请输入起始数量" />
          </template>
        </el-table-column>
        <el-table-column label="终止数量" align="center" prop="tierEnd">
          <template #default="scope">
            <el-input v-model="scope.row.tierEnd" placeholder="请输入终止数量" />
          </template>
        </el-table-column>
        <el-table-column label="单个价格（元）" align="center" prop="tierPrice">
          <template #default="scope">
            <el-input v-model="scope.row.tierPrice" placeholder="请输入终止数量" />
          </template>
        </el-table-column>
      </el-table>


      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-hasPermi="['msg:smsServiceConfigAudit:auditSmsServiceConfig']"
            :loading="buttonLoading"
            type="primary"
            @click="submitForm(true)"
            >审核通过</el-button
          >
          <el-button
            v-hasPermi="['msg:smsServiceConfigAudit:auditSmsServiceConfig']"
            :loading="buttonLoading"
            type="danger"
            @click="submitForm(false)"
            >审核不通过</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SmsServiceConfigAudit" lang="ts">
import {
  auditSmsServiceConfig,
  getApplaySmsServiceConfig,
  listSmsServiceApplay,
  listSmsServiceConfig
} from '@/api/msg/smsServiceConfig';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { msg_service_status } = toRefs<any>(proxy?.useDict('msg_service_status'));
const { msg_service_charging } = toRefs<any>(proxy?.useDict('msg_service_charging'));
const { msg_fix_type } = toRefs<any>(proxy?.useDict('msg_fix_type'));
const smsServiceConfigList = ref([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const servicePiceList = ref([]);
const queryFormRef = ref<ElFormInstance>();
const smsServiceConfigFormRef = ref<ElFormInstance>();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SmsServiceConfigForm = {
  providerId: undefined,
  providerName: undefined,
  apiAddress: undefined,
  requestMethod: undefined,
  authenticationMethod: undefined,
  signatureMethod: undefined,
  accessKeyId: undefined,
  accessKeySecret: undefined,
  accountId: undefined,
  subAccountInfo: undefined,
  signatureId: undefined,
  signatureContent: undefined,
  signatureStatus: undefined,
  callbackAddress: undefined,
  callbackDataFormat: undefined,
  receivingNumberLimit: undefined,
  serviceStatus: undefined,
  meterType: undefined,
  fixType: undefined,
  prefix: undefined,
  suffix: undefined,
  wordCount: undefined,

  unitPrice: undefined,
  packagePrice: undefined,
  packageQuantity: undefined
};
const data = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 0,
    params: {}
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询短信服务商配置列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSmsServiceApplay(queryParams.value);
  smsServiceConfigList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  smsServiceConfigFormRef.value?.resetFields();
};

/** 修改按钮操作 */
const handleUpdate = async (row, sign) => {
  // reset();
  // const _definitionId = row?.definitionId;
  // const res = await getApplaySmsServiceConfig(_definitionId);
  // Object.assign(form.value, res.data.configDefinition);
  // servicePiceList.value = res.data.priceDefinitionList;
  // dialog.visible = true;
  // dialog.title = '短信服务商修改审核';
  //  console.log(row);
  proxy.$router.push({
    path: `/msg/service/smsServiceConfig`,
    query: {
      providerId: row.providerId,
      definitionId: row.definitionId,
      status: row.status,
      sign: sign
    }
  });
};

/** 提交按钮 */
const submitForm = (res) => {
  smsServiceConfigFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      await auditSmsServiceConfig(form.value.definitionId, res);
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss"></style>
