<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <div class="header sa-flex">状态</div>
        <el-form-item label="状态">
          <div class="sa-flex sa-m-r-32">
            <el-switch
              v-model="form.model.status"
              class="sa-m-r-8"
              :active-value="1"
              :inactive-value="0"
            />
            <span class="sa-m-l-8" :class="form.model.status == 1 ? 'openswitch' : ''">{{
              form.model.status == 0 ? '关闭' : '开启'
            }}</span>
          </div>
        </el-form-item>
        <div class="header sa-flex"
          >支付配置<span class="pay-tip"
            >启用货到付款后，请自行安排合作快递完成收款和结算</span
          ></div
        >
        <el-form-item label="支付方式">
          <el-checkbox-group v-model="form.model.payment.methods">
            <el-checkbox label="wechat">微信</el-checkbox>
            <el-checkbox label="alipay">支付宝</el-checkbox>
            <el-checkbox label="money">余额</el-checkbox>
            <el-checkbox label="offline">货到付款</el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item
          label="支付宝"
          prop="payment.alipay"
          v-if="form.model.payment.methods.includes('alipay')"
        >
          <el-select v-model="form.model.payment.alipay" placeholder="请选择">
            <el-option
              v-for="item in alipay.select"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
          <div class="add-richtext sa-m-l-12 cursor-pointer" @click="createPay"> 新建支付方式 </div>
        </el-form-item>
        <div class="header sa-flex sa-flex-wrap">
          <div>
            {{
              props.modal.params.subtitle == 'H5' ? '微信H5' : props.modal.params.subtitle
            }}平台设置
          </div>
          <div v-if="props.modal.params.subtitle == 'H5'" class="header-title">
            如使用微信支付，请在此输入已开通微信H5支付的Appid
          </div>
        </div>
        <el-form-item label="Appid" prop="app_id">
          <el-input v-model="form.model.app_id" placeholder="请输入Appid"></el-input>
          <el-button
            class="is-link sa-m-l-12"
            type="primary"
            @click="configuration"
            v-if="props.modal.params.subtitle == 'H5'"
            >查看配置引导</el-button
          >
          <el-button
            class="is-link sa-m-l-12"
            type="primary"
            @click="AppConfiguration"
            v-if="props.modal.params.subtitle == 'App'"
            >查看配置引导</el-button
          >
        </el-form-item>
        <el-form-item label="AppSecret" prop="secret" v-if="props.modal.params.platform != 'H5'">
          <el-input v-model="form.model.secret" placeholder="请输入AppSecret"></el-input>
        </el-form-item>
        <div v-if="props.modal.params.platform != 'H5'">
          <div class="header sa-flex">微信登录设置</div>
          <el-form-item label="自动登录" v-if="props.modal.params.platform != 'App'">
            <div class="sa-flex sa-m-r-32">
              <el-switch
                v-model="form.model.auto_login"
                class="sa-m-r-8"
                :active-value="1"
                :inactive-value="0"
              />
              <div v-if="form.model.auto_login == 1" class="openswitch shrink-0"> 开启 </div>
              <div v-if="form.model.auto_login == 0" class="shrink-0">关闭</div>
              <div class="warning-text sa-m-l-12"> 进入应用后立即自动发起授权登录 </div>
            </div>
          </el-form-item>
          <el-form-item label="绑定手机号">
            <div class="sa-flex sa-m-r-32">
              <el-switch
                v-model="form.model.bind_mobile"
                class="sa-m-r-8"
                :active-value="1"
                :inactive-value="0"
              />
              <div v-if="form.model.bind_mobile == 1" class="openswitch shrink-0"> 开启 </div>
              <div v-if="form.model.bind_mobile == 0" class="shrink-0">关闭</div>
              <div class="warning-text sa-m-l-12"> 授权登录后立即提醒绑定手机号 </div>
            </div>
          </el-form-item>
        </div>
        <div v-if="props.modal.params.platform == 'App'">
          <div class="header sa-flex">APP下载</div>
          <el-form-item label="Android地址">
            <el-input
              v-model="form.model.download.android"
              placeholder="请输入Android下载地址"
            ></el-input>
          </el-form-item>
          <el-form-item label="IOS地址">
            <el-input v-model="form.model.download.ios" placeholder="请输入IOS下载地址"></el-input>
          </el-form-item>
          <el-form-item label="本地地址">
            <el-input v-model="form.model.download.local" placeholder="请输入本地地址"></el-input>
          </el-form-item>
        </div>

        <div class="header sa-flex">分享设置</div>
        <template v-if="form.model.share">
          <el-form-item label="分享方式">
            <el-checkbox-group v-model="form.model.share.methods">
              <el-checkbox label="forward" disabled>直接转发</el-checkbox>
              <el-checkbox label="poster">分享海报</el-checkbox>
              <el-checkbox label="link">复制链接</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <template v-if="form.model.share.methods.includes('forward')">
            <el-form-item label="标题">
              <el-input
                v-model="form.model.share.forwardInfo.title"
                placeholder="请输入分享标题"
              ></el-input>
            </el-form-item>
            <el-form-item label="副标题">
              <el-input
                v-model="form.model.share.forwardInfo.subtitle"
                placeholder="请输入分享副标题"
              ></el-input>
            </el-form-item>
            <el-form-item label="分享图片">
              <sa-uploader
                v-model="form.model.share.forwardInfo.image"
                fileType="image"
              ></sa-uploader> </el-form-item
          ></template>

          <template v-if="form.model.share.methods.includes('poster')">
            <el-form-item label="用户海报">
              <sa-uploader
                v-model="form.model.share.posterInfo.user_bg"
                fileType="image"
              ></sa-uploader>
            </el-form-item>
            <el-form-item label="商品海报">
              <sa-uploader
                v-model="form.model.share.posterInfo.goods_bg"
                fileType="image"
              ></sa-uploader>
            </el-form-item>
            <el-form-item label="拼团海报">
              <sa-uploader
                v-model="form.model.share.posterInfo.groupon_bg"
                fileType="image"
              ></sa-uploader>
            </el-form-item>
          </template>
          <el-form-item label="分享Url">
            <div class="sa-flex-1">
              <div class="sa-flex">
                <el-input
                  v-model="form.model.share.linkAddress"
                  placeholder="请输入分享Url"
                ></el-input>
              </div>
              <div class="tip">分享链接的默认域名</div>
            </div>
          </el-form-item>
        </template>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button type="primary" @click="confirm">保存</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from '../../config.service';
  import { api as payConfigApi } from '../payConfig/payConfig.service';
  import { useModal } from '@/sheep/hooks';
  import { composeFilter } from '@/sheep/utils';
  import { isEmpty, cloneDeep } from 'lodash';
  import PayConfigEdit from '../payConfig/edit.vue';

  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  const value1 = ref(false);
  let formRef = ref(null);
  const form = reactive({
    model: {
      app_id: '',
      secret: '',
      status: '',
      payment: {
        alipay: '',
        wechat: '',
        methods: [],
      },
      share: {
        methods: [],
        forwardInfo: {
          title: '',
          subtitle: '',
          image: '',
        },
        linkAddress: '',
        posterInfo: {
          user_bg: '',
          goods_bg: '',
          groupon_bg: '',
        },
      },
      download: {
        android: '',
        ios: '',
        local: '',
      },
    },
    rules: {},
  });
  // 获取详情
  async function getDetail() {
    const { data } = await api.platform(props.modal.params.platform);
    form.model = {
      ...data,
    };
    if (isEmpty(form.model.share)) {
      form.model.share = {
        methods: [],
        forwardInfo: {
          title: '',
          subtitle: '',
          image: '',
        },
        linkAddress: '',
        posterInfo: {
          user_bg: '',
          goods_bg: '',
          groupon_bg: '',
        },
      };
    }
    if (isEmpty(form.model.download)) {
      form.model.download = {
        android: '',
        ios: '',
        local: '',
      };
    }
    if (props.modal.params.platform != 'H5' && !form.model.share.methods.includes('forward')) {
      form.model.share.methods.push('forward');
    }
    if (form.model.payment.wechat === 0) {
      form.model.payment.wechat = '';
    }
    if (form.model.payment.alipay === 0) {
      form.model.payment.alipay = '';
    }
  }

  function createPay() {
    useModal(
      PayConfigEdit,
      { title: '新建', type: 'add' },
      {
        confirm: () => {
          getAlipaySelect();
        },
      },
    );
  }

  const alipay = reactive({
    select: [],
  });
  async function getAlipaySelect() {
    let search = composeFilter({
      type: 'alipay',
    });
    ({ data: alipay.select } = await payConfigApi.select(search));
  }

  // 表单关闭时提交
  async function confirm() {
    // 表单验证
    const validator = unref(formRef);
    if (!validator) return;
    await validator.validate();
    let submitForm = cloneDeep(form.model);
    const { code } = await api.platform(props.modal.params.platform, submitForm);
    if (code == '200') {
      emit('modalCallBack', { event: 'confirm' });
    }
  }

  onMounted(() => {
    getDetail();
    getAlipaySelect();
  });
</script>
<style lang="scss" scoped>
  .header {
    width: 100%;
    color: var(--sa-subtitle);
    padding: 10px 0 10px 16px;
    background: var(--sa-background-hex-hover);
    margin: 0 0 16px 0;
    font-weight: 500;
    font-size: 14px;
    .header-title {
      margin-left: 20px;
    }
  }
  .icon-warning {
    width: 14px;
    height: 14px;
    display: flex;
    margin-left: 8px;
  }
  .openswitch {
    color: var(--el-color-primary);
  }
  .switch-width {
    width: 40px;
  }
  .warning-text {
    font-size: 14px;
    line-height: 16px;
    color: var(--sa-font);
    flex-shrink: 0;
  }
  .add-richtext {
    color: var(--el-color-primary);
    font-size: 14px;
    line-height: 16px;
  }
  .shrink-0 {
    flex-shrink: 0;
  }
  @media screen and (max-width: 768px) {
    .header-title {
      margin-left: 0 !important;
    }
  }
  .pay-tip {
    font-weight: 400;
    font-size: 14px;
    line-height: 20px;
    color: var(--sa-subtitle);
    margin-left: 16px;
  }
</style>
