<template>
  <div>
    <el-dialog
      :title="exData.id ? '编辑': '新增'"
      :visible.sync="dialog"
      width="900px"
      :close-on-click-modal="false"
      top="50px">
      <el-form
        ref="form"
        :model="form"
        :rules="formRules"
        label-width="120px">
        <el-form-item
          prop="user_id"
          label="农户编号：">
          <el-input
            v-model="form.user_id"
            placeholder=""
            disabled
            clearable></el-input>
        </el-form-item>
        <el-form-item
          prop="user_name"
          label="农户名字：">
          <el-input
            v-model="form.user_name"
            placeholder=""
            disabled
            clearable></el-input>
        </el-form-item>
        <el-form-item
          prop="track_content"
          label="发布内容：">
          <el-input
            type="textarea"
            :rows="4"
            placeholder=""
            disabled
            v-model="form.track_content">
          </el-input>
        </el-form-item>
        <el-form-item
          class="track-img"
          prop="track_img_url"
          label="发布图片：">
          <el-image
            style="display:block;width: 100px; height: 100px;font-size: 0;"
            :src="item"
            :preview-src-list="[item]"
            v-for="(item, index) in form.track_img_url"
            :key="index">
          </el-image>
          <template v-if="form.track_img_url.length < 1">无</template>
        </el-form-item>
        <el-form-item
          label="互动消息：">
          <el-table
            :data="tableData"
            stripe
            v-loading="tableLoading"
            fit
            class="order-table"
            border
            :max-height="400">
            <el-table-column label="用户昵称" prop="user_nickname" min-width="100"
                             show-overflow-tooltip></el-table-column>
            <el-table-column label="评论内容" prop="track_comment" min-width="200"
                             show-overflow-tooltip></el-table-column>
            <el-table-column label="创建时间" prop="created_at" min-width="140"></el-table-column>
          </el-table>
          <el-pagination
            class="marginT-20"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :hide-on-single-page="true"
            :current-page="page"
            :page-size="page_size"
            :page-sizes="[10, 20, 100, 200, 300, 400]"
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"/>
        </el-form-item>
        <el-form-item
          prop="track_status"
          :rules="formRules.select"
          label="状态：">
          <el-select
            style="width: 100%;"
            v-model="form.track_status"
            filterable
            clearable
            placeholder="请选择是否显示">
            <el-option
              :label="item.name"
              :value="item.value"
              v-for="item in arrHideAndShow"
              :key="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button
          type="primary"
          :disabled="booLock"
          @click="handleSubmit">确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import page from '@/mixin/page'

export default {
  mixins: [page],
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    },
    arrHideAndShow: {
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data() {
    return {
      dialog: !!this.value,
      form: {
        'user_id': '',
        'user_name': '', // todo 发布人姓名
        'track_content': '',
        'track_img_url': [],
        'track_status': 1 // 状态(0隐藏1显示)
      },
      booLock: false,
      searchForm: {},
      tableData: [],
      tableUrl: '/api/admin/track/comment/list'
    }
  },
  methods: {
    handleSubmit() {
      const url = this.exData.id ? '/api/admin/track/set/status' : ''
      this.$refs.form.validate(async valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))
          const postData = {
            ...formData
          }
          this.booLock = true
          const data = await this.$fetch(url, postData)
          this.booLock = false
          if (data.code === 200) {
            this.$message.success('提交成功')
            this.$emit('success')
            this.dialog = false
          }
        }
      })
    }
  },
  mounted() {
    if (this.exData.id) {
      this.$set(this.form, 'id', this.exData.id)
      this.$set(this.searchForm, 'id', this.exData.id)
      for (const key in this.exData) {
        if (this.form.hasOwnProperty(key)) {
          let value = this.exData[key]
          if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
            if (key === 'track_img_url') {
              value = JSON.parse(value)
            }
            if (key === 'track_status') {
              value = value + ''
            }
            this.$set(this.form, key, value)
          }
        }
      }
    }
    this.init()
  },
  watch: {
    dialog(val) {
      if (!val) this.$emit('input', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.track-img {
  ::v-deep .el-form-item__content {
    display: flex;
    flex-flow: row wrap;

    .el-image {
      margin-left: 28px;
      margin-bottom: 10px;

      &:nth-of-type(6n + 1) {
        margin-left: 0;
      }
    }
  }
}
</style>
