<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :optionsEvensGroup="optionsEvensGroup"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :operation="operation()"
  >
    <div class="cartographer_big">
      <el-dialog title="签约信息" width="100%" :modal="false" :visible.sync="formBool" :before-close="handleClose">
        <zj-page-container v-if="formBool">
          <zj-page-fill class="neibuview">
            <zj-form-container
              ref="formRef"
              :form-data="formData"
              :form-rules="formRules"
              :form-attributes="{ size: 'mini' }"
            >
              <zj-form-module title="基础信息" label-width="110px" :form-data="formData" :form-items="items" />
              <zj-form-module title="附件信息" label-width="0px" :form-data="formData" :form-items="items2">
                <div
                  slot="header"
                  v-if="!adminWebsitIds.includes(formData.parentId)"
                  style="width: 100%; text-align: right"
                >
                  <el-button type="primary" size="mini" @click="qianyueanniu">签约</el-button>
                </div>
              </zj-form-module>
            </zj-form-container>
          </zj-page-fill>
        </zj-page-container>
      </el-dialog>
    </div>
    <el-dialog title="审核" width="700px" :modal="false" :visible.sync="shenheBool" :before-close="shenheClose">
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-form-item label="网点名称">
          <span>{{ `(${formData.websitId})${formData.websitName}` }}</span>
        </el-form-item>
        <el-form-item label="合同名称">
          <span>{{ form.contractName }}</span>
        </el-form-item>
        <el-form-item label="签约成功时间">
          <span>{{ form.validDate }}</span>
        </el-form-item>
        <el-form-item label="签约过期时间">
          <span>{{ form.expireDate }}</span>
        </el-form-item>
        <el-form-item label="审核结果">
          <el-radio-group v-model="form.status">
            <el-radio label="OK">审核通过</el-radio>
            <el-radio label="REJECT">审核驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注">
          <el-input type="textarea" v-model="form.remark"></el-input>
        </el-form-item>
        <el-form-item>
          <div style="text-align: right">
            <el-button @click="shenheClose">取消</el-button>
            <el-button type="primary" @click="shenhequeding">确定</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import {
  websitContractSignList,
  websitContractSignListExport,
  websitContractSignCancel,
  websitContractSignSign,
  websitContractSignDetail,
  fddCommonViewContract,
  fddCommonViewTemplate,
  fddCommonDownloadContract,
  websitContractSignConfirm
} from '@/api/networkContractSigning'
import operation_mixin from '@/components/template/operation_mixin.js'
export default {
  components: { TemplatePage },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      formBool: false,
      formType: 0,
      formData: {},
      formRules: {},
      shenheBool: false,
      form: {}
    }
  },
  computed: {
    userData() {
      return JSON.parse(localStorage.getItem('greemall_user'))
    },
    adminWebsitIds() {
      return this?.userData?.adminWebsitIds || []
    },
    optionsEvensGroup() {
      return []
    },
    items() {
      return [
        {
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '上级网点编号',
            prop: 'parentId'
          }
        },
        {
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '上级网点名称',
            prop: 'parentName'
          }
        },
        {
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '网点编号',
            prop: 'websitId'
          }
        },
        {
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '网点名称',
            prop: 'websitName'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '法大大法人id',
            prop: 'fadadaCustomerId'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '法人实名流水号',
            prop: 'fadadaWebsitNo'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '个人实名状态',
            prop: 'authStatus'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '法大大网点id',
            prop: 'fadadaWebsitNo'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '网点实名流水号',
            prop: 'fadadaWebsitNo'
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: true,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '创建时间',
            prop: 'createTime'
          }
        }
      ]
    },
    items2() {
      return [
        {
          name: 'slot-component',
          md: 24,
          formItemAttributes: {
            label: '',
            prop: 'items'
          },
          render: (h, { props }) => {
            var { formData } = props
            return (
              <div>
                <zj-table
                  columns={[
                    {
                      columnAttributes: {
                        label: '序号',
                        prop: ''
                      },
                      render: (h, { row, column, index }) => {
                        return <div style="padding-left:10px">{index + 1}</div>
                      }
                    },
                    {
                      columnAttributes: {
                        label: '附件名称',
                        prop: 'contractName'
                      }
                    },
                    {
                      columnAttributes: {
                        label: '附件状态',
                        prop: 'contractStatus'
                      },
                      render: (h, { row, column, index }) => {
                        return (
                          <div style="padding-left:10px">
                            {{ WAIT_SIGN: '待签约', WAIT: '待审核', OK: '审核通过' }[row.contractStatus]}
                          </div>
                        )
                      }
                    },
                    {
                      columnAttributes: {
                        label: '签约成功时间',
                        prop: 'validDate'
                      }
                    },
                    {
                      columnAttributes: {
                        label: '签约过期时间',
                        prop: 'expireDate'
                      }
                    },
                    {
                      columnAttributes: {
                        label: '审核人',
                        prop: 'confirmBy'
                      }
                    },
                    {
                      columnAttributes: {
                        label: '操作'
                      },
                      render: (h, { row, column, index }) => {
                        return (
                          <div style="padding-left:10px">
                            {!row.fadadaContractId && row.fadadaTemplateId ? (
                              <el-button
                                type="text"
                                onClick={() => {
                                  fddCommonViewTemplate({ templateId: row.fadadaTemplateId }).then(res => {
                                    window.open(res.data, '_blank')
                                  })
                                }}
                              >
                                查看模板
                              </el-button>
                            ) : null}
                            {row.fadadaContractId ? (
                              <el-button
                                type="text"
                                onClick={() => {
                                  fddCommonViewContract({
                                    contractId: row.fadadaContractId
                                  }).then(res => {
                                    window.open(res.data, '_blank')
                                  })
                                }}
                              >
                                查看合同
                              </el-button>
                            ) : null}
                            {row.contractStatus == 'OK' && this.adminWebsitIds.includes(this.formData?.parentId) ? (
                              <el-button
                                type="text"
                                onClick={() => {
                                  fddCommonDownloadContract({
                                    contractId: row.fadadaContractId
                                  }).then(res => {
                                    window.open(res.data, '_blank')
                                  })
                                }}
                              >
                                下载合同
                              </el-button>
                            ) : null}
                            {row.contractStatus == 'WAIT' && this.adminWebsitIds.includes(this.formData?.parentId) ? (
                              <el-button
                                type="text"
                                onClick={() => {
                                  this.form = { ...row }
                                  this.$nextTick(() => {
                                    this.shenheBool = true
                                  })
                                }}
                              >
                                审核
                              </el-button>
                            ) : null}
                          </div>
                        )
                      }
                    }
                  ]}
                  tableData={this.formData.items}
                ></zj-table>
              </div>
            )
          }
        }
      ]
    }
  },
  created() {
    if (this.$route?.query?.id) {
      this.openWindow(() => {
        websitContractSignDetail({
          id: this.$route?.query?.id
        }).then(res => {
          this.formData = {
            ...res.data
          }
          this.$nextTick(() => {
            this.formType = 1
            this.formBool = true
          })
        })
      })
    }
  },
  methods: {
    // 列表请求函数
    getList: websitContractSignList,
    // 导出
    exportList: websitContractSignListExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 获取勾选框数据
    selectionChange(data) {
      this.recordSelected = data
    },
    openWindow(cb) {
      this.$nextTick(() => {
        cb?.()
      })
    },
    handleClose() {
      this.$refs?.pageRef?.refreshList()
      this.$data.formData = this.$options.data().formData
      this.formType = 0
      this.formBool = false
    },
    operation() {
      return this.operationBtn({
        viewContract: {
          click: ({ row, index, column }) => {
            this.openWindow(() => {
              websitContractSignDetail({
                id: row.id
              }).then(res => {
                this.formData = {
                  ...res.data
                }
                this.$nextTick(() => {
                  this.formType = 1
                  this.formBool = true
                })
              })
            })
          }
        },
        loseEfficacy: {
          prompt: '是否确定设为失效?',
          conditions: ({ row, index, column }) => {
            return true
          },
          click: ({ row, index, column }) => {
            websitContractSignCancel({
              id: row.id
            }).then(res => {
              this.$message({ type: 'success', message: '设置成功!' })
              this.$refs?.pageRef?.refreshList()
            })
          }
        }
      })
    },
    shenheClose() {
      this.shenheBool = false
    },
    shenhequeding() {
      websitContractSignConfirm({
        id: this.form.id,
        remark: this.form.remark,
        status: this.form.status
      }).then(res => {
        this.$message({ type: 'success', message: '审核成功!' })
        this.shenheClose()
        this.openWindow(() => {
          websitContractSignDetail({
            id: row.id
          }).then(res => {
            this.formData = {
              ...res.data
            }
          })
        })
      })
    },
    qianyueanniu() {
      websitContractSignSign({
        id: this.formData.id,
        returnUrl: `${window.location.href.split('#')?.[0]}#/networkSigning?id=${this.formData.id}`
      }).then(res => {
        if (res.data.jump) {
          window.open(res.data.url, '_blank')
        }
      })
    }
  }
}
</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>
