<template>
  <template-page
    ref="pageRef"
    :get-list="getList"
    :table-attributes="tableAttributes"
    :table-events="tableEvents"
    :operationColumnWidth="200"
    :options-evens-group="optionsEvensGroup"
    :moreParameters="moreParameters"
    :column-parsing="columnParsing"
    :operation="operation()"
    :exportList="exportList"
  >
    <div class="cartographer_big">
      <el-dialog
        title="设置支付商户信息"
        width="100%"
        :modal="false"
        :visible.sync="formDialog"
        :before-close="handleClose"
      >
        <zj-page-container>
          <zj-page-fill class="neibuview">
            <zj-form-container
              ref="formRef"
              :styleSwitch="false"
              :form-data="formData"
              :form-attributes="{ size: 'mini' }"
            >
              <zj-form-module title="" label-width="120px" :form-data="formData" :form-items="formItem" />
            </zj-form-container>
          </zj-page-fill>
          <div style="box-sizing: border-box; padding: 10px 10px 0; text-align: right">
            <el-button type="primary" @click="save">保存</el-button>
            <el-button @click="chongzhi">重置</el-button>
          </div>
        </zj-page-container>
      </el-dialog>
    </div>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import ImageUpload from '@/components/file-upload'
import { required } from '@/components/template/rules_verify.js'
import {
  adminCompanyPayConfigListExport,
  adminCompanyPayConfigList,
  adminCompanyPayConfigSave,
  adminCompanyPayConfigDetail
} from '@/api/paymentMerchantManagement'
import MemberDetail from '@/components/Mall/Member/member-detail'
import operation_mixin from '@/components/template/operation_mixin.js'
export default {
  components: { TemplatePage, MemberDetail, ImageUpload },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      // 勾选选中行
      recordSelected: [],
      /** 表单变量 */
      formDialog: false,
      formDialogType: 0,
      formData: {
        appId: '',
        companyName: JSON.parse(localStorage.getItem('greemall_user'))?.companyName || '',
        companyWechatId: JSON.parse(localStorage.getItem('greemall_user'))?.companyWechatId || '',
        mchId: '',
        mchKey: '',
        payMerchantName: '',
        remark: '',
        status: true,
        subAppId: '',
        subMchId: '',
        appletAppid: '',
        appletSecret: '',
        attr: []
      }
    }
  },
  computed: {
    moreParameters() {
      return []
    },
    // 事件组合
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', {
              click: () => {
                this.formDialog = true
              }
            })
          ]
        ]
      ]
    },
    formItem() {
      return [
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '电子商户名称',
            prop: 'payMerchantName',
            rules: [...required]
          }
        },
        {
          name: 'el-checkbox',
          options: [
            { label: '电子支付', value: '电子支付' },
            { label: '保险支付', value: '保险支付' }
          ],
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '商户属性',
            prop: 'attr',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: 'appid',
            prop: 'appId',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '子商户应用appid',
            prop: 'subAppId',
            rules: []
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: 'mchid',
            prop: 'mchId',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '子商户mchid',
            prop: 'subMchId',
            rules: []
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: 'key',
            prop: 'mchKey',
            rules: [...required]
          }
        },
        {
          name: 'el-radio',
          options: [
            { label: '启用', value: true },
            { label: '禁用', value: false }
          ],
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '状态',
            prop: 'status',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '小程序appid',
            prop: 'appletAppid',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: !!this.formDialogType,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '小程序密钥',
            prop: 'appletSecret',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 24,
          attributes: {
            disabled: !!this.formDialogType,
            type: 'textarea',
            rows: 3,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '支付说明',
            prop: 'remark',
            rules: []
          }
        }
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList: adminCompanyPayConfigList,
    // 列表导出函数
    exportList: adminCompanyPayConfigListExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },

    // 表格操作列
    operation() {
      return this.operationBtn({
        details: {
          click: ({ row, index, column }) => {
            adminCompanyPayConfigDetail({
              id: row.id
            }).then(res => {
              this.formDialogType = 1
              this.formData = { ...res.data, attr: res.data?.attr ? res.data?.attr?.split(',') : [] }
              this.formDialog = true
              this.$refs.pageRef.refreshList()
            })
          }
        },
        edit: {
          click: ({ row, index, column }) => {
            adminCompanyPayConfigDetail({
              id: row.id
            }).then(res => {
              this.formData = { ...res.data, attr: res.data?.attr ? res.data?.attr?.split(',') : [] }
              this.formDialog = true
              this.$refs.pageRef.refreshList()
            })
          }
        }
      })
    },
    handleClose() {
      this.formDialogType = 0
      this.formData = {
        appId: '',
        companyName: JSON.parse(localStorage.getItem('greemall_user'))?.companyName || '',
        companyWechatId: JSON.parse(localStorage.getItem('greemall_user'))?.companyWechatId || '',
        mchId: '',
        mchKey: '',
        payMerchantName: '',
        remark: '',
        status: true,
        subAppId: '',
        subMchId: '',
        appletAppid: '',
        appletSecret: '',
        attr: []
      }
      this.formDialog = false
    },
    chongzhi() {
      this.formData = Object.assign(this.formData, {
        companyName: JSON.parse(localStorage.getItem('greemall_user'))?.companyName || '',
        companyWechatId: JSON.parse(localStorage.getItem('greemall_user'))?.companyWechatId || '',
        appId: '',
        mchId: '',
        mchKey: '',
        payMerchantName: '',
        remark: '',
        status: true,
        subAppId: '',
        subMchId: '',
        appletAppid: '',
        appletSecret: '',
        attr: []
      })
    },
    save() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          adminCompanyPayConfigSave({
            ...this.formData,
            attr: this.formData.attr.join(',')
          }).then(res => {
            this.$message({
              type: 'success',
              message: '保存成功'
            })
            this.$refs.pageRef.refreshList()
            this.handleClose()
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.neibuview {
  box-sizing: border-box;
  padding-left: 16px;

  ::v-deep & > .zj-page-fill-scroll {
    box-sizing: border-box;
    padding-right: 16px;

    & > div:nth-child(1) {
      margin-top: 20px;
    }
  }
}
</style>
