<template>
  <zj-tab-page ref="tabPage" :defaultActives="[{ key: 'list', label: $route.meta.title + '-列表', essential: true }]">
    <template slot-scope="{ activeKey, data }">
      <div
        :style="{
          width: '100%',
          height: activeKey == 'list' ? '100%' : '0px',
          overflow: 'hidden'
        }"
      >
        <template-page
          ref="pageRef"
          :get-list="getList"
          :table-attributes="tableAttributes"
          :table-events="tableEvents"
          :options-evens-group="optionsEvensGroup"
          :moreParameters="moreParameters"
          :column-parsing="columnParsing"
          :exportList="exportList"
          :operation="operation()"
        >
        </template-page>
      </div>
      <div v-if="~['add', 'edit'].indexOf(activeKey)">
        <zj-form-container ref="formRef" :form-data="formData" :styleSwitch="false">
          <zj-form-module
            title=""
            label-width="110px"
            :showPackUp="false"
            :form-data="formData"
            :form-items="formItems"
          >
          </zj-form-module>
        </zj-form-container>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click="data.removeTab()">取 消</el-button>
          <el-button size="mini" @click="formConfirm(data.removeTab)" type="primary">确 定</el-button>
        </div>
      </div>
    </template>
  </zj-tab-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 operation_mixin from '@/components/template/operation_mixin.js'
import { required, mobileRequired, mobile, httpUrl, email } from '@/components/template/rules_verify.js'
import {
  dailyBankList,
  dailyBankListExport,
  dailyBankTemplateImport,
  dailyBankDelete,
  dailyBankAdd,
  dailyBankUpdate,
  dailyBankDetail
} from '@/api/bankAccount'
import { commonTemplateDownload } from '@/api/common.js'
export default {
  components: { TemplatePage, ImageUpload },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      // 勾选选中行
      recordSelected: [],
      // 表单
      formData: {
        bankAccount: '',
        bankAccountName: '',
        bankAddr: '',
        companyName: JSON.parse(localStorage.getItem('greemall_user')).companyName,
        companyWechatId: '',
        depositBank: '',
        idcard: '',
        mobile: '',
        remark: '',
        websitId: '',
        websitName: '',
        workerName: '',
        workerNumber: ''
      }
    }
  },
  computed: {
    // 更多参数
    moreParameters() {
      return []
    },
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', {
              click: () => {
                this.close()
                this.$refs.tabPage.addTab({
                  // 对应显示的模块
                  activeKey: 'add',
                  // 唯一标识
                  key: 'add',
                  // 页签名称
                  label: '新增',
                  // 打开时事件
                  triggerEvent: () => {},
                  // 关闭时事件
                  closeEvent: () => {}
                })
              }
            })
          ]
        ],
        [
          [
            this.optionsEvensAuth('import', ({ moduleName }) => {
              return {
                name: moduleName,
                render: () => {
                  return this.importButton(dailyBankTemplateImport, moduleName)
                }
              }
            })
          ]
        ],
        [
          [
            this.optionsEvensAuth('template', {
              click: () => {
                commonTemplateDownload({ name: '银行账户模板.xlsx' }, `银行账户模板`)
                  .then(res => {
                    this.$message({
                      message: '下载成功',
                      type: 'success'
                    })
                  })
                  .catch(err => {
                    this.$message.error('下载失败')
                  })
              }
            })
          ]
        ]
      ]
    },
    formItems() {
      return [
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: false
          },
          formItemAttributes: {
            label: '师傅名称',
            prop: 'workerName',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: false
          },
          formItemAttributes: {
            label: '身份证',
            prop: 'idcard',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: false
          },
          formItemAttributes: {
            label: '联系电话',
            prop: 'mobile',
            rules: [...mobileRequired]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: false
          },
          formItemAttributes: {
            label: '银行卡',
            prop: 'bankAccount',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: false
          },
          formItemAttributes: {
            label: '户名',
            prop: 'bankAccountName',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 6,
          attributes: {
            disabled: false
          },
          formItemAttributes: {
            label: '开户行名称',
            prop: 'depositBank',
            rules: [...required]
          }
        },
        {
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: false
          },
          formItemAttributes: {
            label: '开户行地址',
            prop: 'bankAddr',
            rules: []
          }
        },
        {
          name: 'el-input',
          md: 24,
          attributes: {
            disabled: false,
            type: 'textarea',
            rows: 2
          },
          formItemAttributes: {
            label: '备注',
            prop: 'remark',
            rules: []
          }
        }
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList(p, cb) {
      try {
        cb && cb(p)
        return dailyBankList(p)
      } catch (error) {
        console.log(error)
      }
    },
    // 列表导出函数
    exportList: dailyBankListExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return this.operationBtn({
        edit: {
          click: ({ row, index, column }) => {
            this.close()
            dailyBankDetail({ id: row.id }).then(res => {
              this.formData = res.data
              this.$refs.tabPage.addTab({
                // 对应显示的模块
                activeKey: 'edit',
                // 唯一标识
                key: 'edit',
                // 页签名称
                label: '编辑',
                // 打开时事件
                triggerEvent: () => {},
                // 关闭时事件
                closeEvent: () => {}
              })
            })
          }
        },
        del: {
          prompt: '确定删除吗？',
          click: ({ row, index, column }) => {
            dailyBankDelete({ id: row.id }).then(res => {
              this.$message({
                type: 'success',
                message: '删除成功！'
              })
              this.$refs?.pageRef?.refreshList()
            })
          }
        }
      })
    },
    formConfirm(removeTab) {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          ;(this.formData.id ? dailyBankUpdate : dailyBankAdd)({
            ...this.formData
          }).then(res => {
            this.$message({ type: 'success', message: `设置成功!` })
            this.$refs?.pageRef?.refreshList()
            removeTab()
          })
        }
      })
    },
    close() {
      this.$data.formData = this.$options.data().formData
    }
  }
}
</script>

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