<template>
  <div class="app-container" style="width: 800px;margin: 0 auto;">
    <el-form ref="bannerForm" :model="form" label-width="100px">
      <el-form-item label="姓名:" prop="name" :rules="rules.name">
        <el-input v-model="form.name" placeholder="请填写姓名" maxlength="6" style="width: 200px;" />
      </el-form-item>
      <el-form-item label="手机号:" prop="mobile" :rules="rules.mobile">
        <el-input v-model="form.mobile" placeholder="请填写手机号" style="width: 200px" />
      </el-form-item>
      <el-form-item label="状态:" prop="status" :rules="rules.status">
        <el-select v-model="form.status" placeholder="请选择">
          <el-option label="上线" :value="1" />
          <el-option label="下线" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="关联机构：" prop="clubs" :rules="rules.clubs">
        <template v-if="form.clubs && form.clubs.length === 0">
          <el-button type="primary" autofocussize="mini" size="mini" icon="el-icon-plus" @click="handleDialogVisible">选择机构</el-button>
          <el-checkbox-group v-show="false" v-model="form.clubs" />
        </template>
        <template v-else>
          <el-table :data="form.clubs" border width="1000">
            <el-table-column prop="userName" label="机构名称" align="center" />
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="{row}">
                <template v-if="row.clubStatus*1===1">
                  <el-tag type="warning" size="small">待审核</el-tag>
                </template>
                <template v-else-if="row.clubStatus*1===92">
                  <el-tag type="danger" size="small">审核未通过</el-tag>
                </template>
                <template v-else>
                  <el-tag v-if="row.clubStatus*1===91" type="danger" size="small">已下线</el-tag>
                  <el-tag v-else type="success" size="small">已上线</el-tag>
                </template>
              </template>
            </el-table-column>
            <el-table-column label="联系人" align="center" prop="linkMan" />
            <el-table-column label="手机号" align="center" prop="bindMobile" />
            <el-table-column label="操作" align="center">
              <el-button type="text" @click="deleteShop">删除</el-button>
            </el-table-column>
          </el-table>
        </template>
      </el-form-item>
    </el-form>
    <div class="el-dialog__footer" style="text-align: center;">
      <el-button type="primary" @click="onSubmit('bannerForm')">保存</el-button>
      <el-button plain @click="backToList">返回</el-button>
    </div>
    <!-- 商品弹框 -->
    <club-dialog v-if="dialogVisible" ref="goodDialog" @cancel="handleGoodCancel" @confirm="handleGoodConfirm" />
  </div>
</template>

<script>
import { savePageBanner, formPageBanner } from '@/api/campaign'
import clubDialog from './components/club-multi-dialog'
export default {
  name: 'ProcureEdit',
  components: { clubDialog },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'warning'
      }
      return statusMap[status]
    }
  },
  data() {
    const defaultForm = () => {
      return {
        organizeId: this.$store.getters.organizeId,
        name: '', // 采购员名称
        mobile: '', // 采购员手机号
        status: 1, // 轮播上线状态
        clubs: []
      }
    }
    return {
      form: defaultForm(),
      activityList: [],
      editType: 'add',
      // 商品列表
      dialogVisible: false,
      loadImgLoading: false,
      rules: {
        bannerName: [{ required: true, message: '请填写轮播主题', trigger: 'blur' }],
        bannerImage: [{ required: true, message: '请上传轮播图', trigger: 'blur' }],
        clubs: [{ required: true, type: 'array', message: '请添加一个商品', trigger: ['change'] }],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      },
      listQuery: {
        index: 1,
        pageSize: 100,
        enabledStatus: 1,
        organizeId: this.$store.getters.organizeId
      }
    }
  },
  computed: {

  },
  created() {
    this.editType = this.$route.query.type || 'edit'
    if (this.editType === 'edit') {
      console.log('edit')
      // this.formPageBanner(this.$route.query.id)
    }
  },
  methods: {
    async formPageBanner(id) {
      // 获取轮播图详情
      try {
        const res = await formPageBanner(id)
        this.form = { ...this.form, ...res.data }
        if (res.data.jumpType === 2) {
          this.form.products = []
        }
      } catch (error) {
        console.log('error', error)
      }
    },
    changeGrop(value) {
      console.log('value', value)
    },
    // 显示选择商品弹窗
    handleDialogVisible() {
      this.dialogVisible = true
    },
    handleGoodDialogVisible(index) {
      // 显示选择商品弹窗
      this.addIndex = index
      this.dialogGoodVisible = true
    },
    deleteShop() {
      // 清除商品
      this.form.jumpProductId = ''
      this.form.clubs = []
    },
    handleGoodConfirm(data) {
      // 确认选择商品
      this.form.clubs = data
      console.log('clubs', this.form.clubs)
      this.handleGoodCancel()
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$confirm('是否提交数据', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            console.log('form', this.form)
            this.savePageBanner(this.form)
          })
        } else {
          return false
        }
      })
    },
    async savePageBanner(params) {
      // 保存
      await savePageBanner(params)
      this.$message.success('保存成功')
      setTimeout(() => {
        this.$router.push({ path: '/campaign/list', query: { type: 1 }})
      }, 1000)
    },
    // 取消选择商品
    handleGoodCancel() {
      this.dialogVisible = false
      this.$refs.goodDialog.visible = false
    },
    backToList() {
      this.$store.dispatch('tagsView/delView', this.$route).then(() => {
        this.$nextTick(() => {
          this.$router.replace({ path: '/procure/list', query: { type: 1 }})
        })
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    float: left;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon{
    font-size: 30px;
    color: #999999;
  }
  .el-form-item__label{
      text-align: right !important;
  }
  .el-upload__tip{
    line-height: 20px;
    color: red;
    text-align: left;
    position: absolute;
    right: -50%;
    bottom: 0;
  }
  .span_tip{
    font-size: 12px;
    color: red;
    margin-left: 5px;
  }
  .filter-item-temp{
    width: 100px;
  }
</style>
