<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="基础信息" :form-data="formData" :form-items="items" />
              <zj-form-module title="网点信息" :form-data="formData" :form-items="items2" />
            </zj-form-container>
          </zj-page-fill>
          <!-- 操作按钮 -->
          <div style="box-sizing: border-box; padding: 10px; text-align: right">
            <el-button size="mini" @click="handleClose">取 消</el-button>
            <el-button v-if="~[0, 1].indexOf(formType)" size="mini" @click="formConfirm" type="primary"
              >发 送</el-button
            >
          </div>
        </zj-page-container>
      </el-dialog>
    </div>
    <div class="cartographer_big">
      <el-dialog
        title="网点通知详情"
        width="100%"
        :modal="false"
        :visible.sync="formBoolXQ"
        :before-close="handleClose"
      >
        <zj-page-container v-if="formBoolXQ">
          <zj-page-fill class="neibuview">
            <div class="nlnlknmlkmkl" style="width: 100%" v-if="formBoolXQ">
              <template-page
                ref="pageRef2"
                :getList="getList2"
                :exportList="exportList2"
                :operation="operation2()"
              ></template-page>
            </div>
            <zj-form-container
              ref="formRef"
              :form-data="formData"
              :form-rules="formRules"
              :form-attributes="{ size: 'mini' }"
            >
              <zj-form-module title="基础信息" :form-data="formData" :form-items="items" />
            </zj-form-container>
          </zj-page-fill>
        </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 pagingTransfer from '@/components/paging-transfer.vue'
import {
  noticeWebsitList,
  noticeWebsitListExport,
  noticeWebsitAdd,
  noticeWebsitUpdate,
  noticeWebsitDetail,
  noticeWebsitOfOrSend,
  noticeWebsitListWebsit,
  noticeWebsitListWebsitExport,
  noticeWebsitMarkRead,
  noticeWebsitMarkReadOne
} from '@/api/networkNotification'
import operation_mixin from '@/components/template/operation_mixin.js'
import { required } from '@/components/template/rules_verify.js'
import { getWebsit } from '@/api/customerManagement.js'
import { EventBus } from '@/utils/eventBus'
import ImageUpload from '@/components/file-upload'
export default {
  components: { TemplatePage, ImageUpload },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      formBool: false,
      formBoolXQ: false,
      formType: 0,
      formData: {
        adminCompanyId: '',
        adminCompanyName: '',
        code: '',
        content: '',
        deptName: '',
        issueNickName: '',
        issueTime: '',
        issueUserId: '',
        noticeWebsitId: '',
        num: '',
        status: 'SEND',
        title: '',
        type: '',
        websitIds: [],
        websits: [],
        file: []
      },
      formRules: {},
      formInline: {
        websitId: '',
        websitName: ''
      }
    }
  },
  computed: {
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', {
              click: () => {
                this.openWindow(() => {
                  this.formType = 0
                  this.formBool = true
                })
              }
            })
          ]
        ]
      ]
    },

    items() {
      return [
        {
          isShow: this.formType == 0,
          name: 'el-select',
          md: 12,
          name: 'el-radio',
          options: [
            { label: '管理', value: 'GL' },
            { label: '技术', value: 'JS' },
            { label: '其他', value: 'QT' },
            { label: '政策', value: 'ZC' },
            { label: '资讯', value: 'ZX' }
          ],
          attributes: {
            disabled: this.formType == 2,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '通知类型',
            prop: 'type',
            rules: [...required]
          }
        },
        {
          isShow: this.formType == 0,
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: this.formType == 2,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '来源部门',
            prop: 'deptName',
            rules: [...required]
          }
        },
        {
          isShow: this.formType == 0,
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: this.formType == 2,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '文件编号',
            prop: 'code',
            rules: [...required]
          }
        },
        {
          isShow: this.formType == 0,
          name: 'el-input',
          md: 12,
          attributes: {
            disabled: this.formType == 2,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '文件标题',
            prop: 'title',
            rules: [...required]
          }
        },
        {
          name: 'slot-component',
          md: 24,
          attributes: { disabled: this.disabled },
          formItemAttributes: { label: '通知内容', prop: 'content', rules: [...required] },
          render: (h, { props }) => {
            var { formData } = props
            return (
              <v-quill-editor
                value={formData.content}
                onInput={val => {
                  formData.content = val
                }}
              ></v-quill-editor>
            )
          }
        },
        {
          md: 24,
          isShow: true,
          name: 'slot-component',
          formItemAttributes: {
            label: '附件',
            prop: 'file',
            rules: []
          },
          render: (h, { props, onInput }) => {
            var { value } = props
            return (
              <ImageUpload
                fileList={this.formData.file}
                uid="idcardUpUrl_drawg_FUJIAN"
                limit={100}
                isEdit={this.formType == 0}
              />
            )
          }
        }
      ]
    },
    items2() {
      return [
        {
          name: 'slot-component',
          md: 24,
          attributes: {},
          formItemAttributes: { label: '', 'label-width': '0px', prop: 'websits', rules: [] },
          render: (h, { props, onInput }) => {
            var { formData } = props
            return (
              <div>
                <div>
                  <el-form
                    size="mini"
                    inline={true}
                    value={this.formInline}
                    onInput={v => {
                      this.formInline = v
                    }}
                    label-width="0px"
                  >
                    <el-form-item label="">
                      <el-input
                        value={this.formInline.websitId}
                        onInput={v => {
                          this.formInline.websitId = v
                        }}
                        placeholder="网点编号"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="">
                      <el-input
                        value={this.formInline.websitName}
                        onInput={v => {
                          this.formInline.websitName = v
                        }}
                        placeholder="网点名称"
                      ></el-input>
                    </el-form-item>
                    <el-form-item>
                      <el-button
                        type="primary"
                        onClick={() => {
                          EventBus.$emit('handlePaginationCallBack22')
                        }}
                      >
                        查询
                      </el-button>
                      <el-button
                        onClick={() => {
                          this.formInline.websitId = ''
                          this.formInline.websitName = ''
                          this.$nextTick(() => {
                            EventBus.$emit('handlePaginationCallBack22')
                          })
                        }}
                      >
                        重置
                      </el-button>
                    </el-form-item>
                  </el-form>
                </div>
                <pagingTransfer
                  handlePaginationCallBackKey="handlePaginationCallBack22"
                  value={this.formData.websits}
                  onInput={v => {
                    this.formData.websits = v
                  }}
                  left-columns={[
                    { id: 'belongCompanyCode', label: '所属公司编号', width: '160' },
                    { id: 'websitId', label: '网点编号', width: '120' },
                    { id: 'websitName', label: '网点名称', width: '' }
                  ]}
                  right-columns={[
                    { id: 'belongCompanyCode', label: '所属公司编号', width: '160' },
                    { id: 'websitId', label: '网点编号', width: '120' },
                    { id: 'websitName', label: '网点名称', width: '' }
                  ]}
                  show-pagination={false}
                  pagination-call-back={this.paginationCallback}
                  title-texts={['待选项', '已选项']}
                  min-height="300px"
                  table-row-key={row => row.websitId}
                />
              </div>
            )
          }
        }
      ]
    }
  },
  created() {
    this.initFun()
  },
  methods: {
    initFun() {
      if (this.pageType == 'detail' && this.pageCode) {
        noticeWebsitDetail({
          id: this.pageCode
        }).then(res => {
          this.formData = {
            ...res.data,
            file: res.data.file ? res.data.file.split(',').map(url => ({ url })) : []
          }
          this.formType = 2
          this.openWindow(() => {
            this.formBoolXQ = true
          })
        })
      }
    },
    // 列表请求函数
    getList: noticeWebsitList,
    // 导出
    exportList: noticeWebsitListExport,
    // 列表请求函数
    getList2(p, cb) {
      var pam = JSON.parse(JSON.stringify(p))
      try {
        pam.params.push({ param: 'a.notice_websit_id', compare: '=', value: this.formData.noticeWebsitId })
        cb && cb(pam)
        return noticeWebsitListWebsit(pam)
      } catch (err) {}
    },
    // 导出
    exportList2: noticeWebsitListWebsitExport,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 获取勾选框数据
    selectionChange(data) {
      this.recordSelected = data
    },
    handleClose() {
      this.$refs?.pageRef?.refreshList()
      this.$data.formData = this.$options.data().formData
      this.$data.formInline = this.$options.data().formInline
      this.formType = 0
      this.formBool = false
      this.formBoolXQ = false
    },
    formConfirm() {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          if (this.formData.websits.length == 0) {
            this.$message.warning('网点必须至少选择一个！')
            return
          }
          ;(this.formType ? noticeWebsitUpdate : noticeWebsitAdd)({
            ...this.formData,
            websitIds: this.formData.websits.map(item => item.websitId),
            file: this.formData.file.map(item => item.url).join(',')
          }).then(res => {
            this.$message({ type: 'success', message: '成功!' })
            this.$refs.pageRef.refreshList()
            this.handleClose()
          })
        }
      })
    },
    operation() {
      return this.operationBtn({
        details: {
          click: ({ row, index, column }) => {
            this.openWindow(() => {
              noticeWebsitDetail({
                id: row.noticeWebsitId
              }).then(res => {
                this.formData = {
                  ...res.data,
                  file: res.data.file ? res.data.file.split(',').map(url => ({ url })) : []
                }
                this.formType = 2
                this.openWindow(() => {
                  this.formBoolXQ = true
                })
              })
            })
          }
        },
        publish: {
          prompt: '确定发布吗？',
          conditions: ({ row, index, column }) => {
            return row.status !== 'SEND'
          },
          click: ({ row, index, column }) => {
            noticeWebsitOfOrSend({
              id: row.noticeWebsitId,
              status: 'SEND'
            }).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.$refs.pageRef.refreshList()
            })
          }
        },
        close: {
          prompt: '确定关闭吗？',
          conditions: ({ row, index, column }) => {
            return row.status !== 'OFF'
          },
          click: ({ row, index, column }) => {
            noticeWebsitOfOrSend({
              id: row.noticeWebsitId,
              status: 'OFF'
            }).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.$refs.pageRef.refreshList()
            })
          }
        }
      })
    },
    operation2() {
      if (JSON.parse(localStorage.getItem('greemall_user')).type != 0) {
        return undefined
      }
      return this.operationBtn({
        details: {
          name: '接收',
          conditions: ({ row, index, column }) => {
            return row.readFlag == 'NO' && JSON.parse(localStorage.getItem('greemall_user')).type == 0
          },
          click: ({ row, index, column }) => {
            noticeWebsitMarkReadOne({
              id: row.id
            }).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.$refs.pageRef2.refreshList()
            })
          }
        }
      })
    },
    openWindow(cb) {
      this.$nextTick(() => {
        cb?.()
      })
    },
    paginationCallback({ pageIndex, pageSize }) {
      return new Promise(resolve => {
        getWebsit({ ...this.formInline }).then(res => {
          resolve({
            total: res.data.filter(item => item.type == 'C').length,
            data: res.data
              .filter(item => item.type == 'C')
              .map(item => {
                return {
                  ...item,
                  websitName: item.name
                }
              })
          })
        })
      })
    }
  }
}
</script>

<style lang="scss">
.nlnlknmlkmkl {
  .zj-page-template {
    padding: 0px !important;
  }
  .zj-page-fill {
    min-height: auto !important;
    .zj-page-fill-scroll {
      height: auto !important;
      .page-content {
        height: auto !important;
        .el-form {
          height: auto !important;
          .zj-table-class {
            height: auto !important;
            .dropUseKey {
              height: auto !important;
            }
          }
        }
      }
    }
  }
}
</style>

<style lang="scss" scoped>
.nlnlknmlkmkl {
  ::v-deep .zj-page-template {
    padding: none !important;
  }
}
.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>
