<template>
  <!-- 表单渲染 -->
  <el-dialog append-to-body :close-on-click-modal="false" :before-close="cancelView" :visible="visible" :title="title" width="860px">
    <el-form ref="formViewRef" :model="formData" :rules="rules" :status-icon="true" label-width="240px">
      <el-form-item label="产品类型名称：" class="form-cell" prop="xxx">
        <div class="cell-box">
          <el-input v-model="formData.productType" disabled placeholder="暂无" class="cell-input" />
        </div>
      </el-form-item>
      <el-form-item label="产品ID：" class="form-cell" prop="xxx">
        <div v-for="(item, index) in formData.productList" :key="index" class="cell-box" style="margin-bottom: 2px">
          <el-input v-model="item.productId" placeholder="产品id" class="cell-input" style="width: 150px" />
          <el-input v-model.number="item.index" type="number" placeholder="顺序" class="cell-input" style="width: 150px" />
          <el-button circle icon="el-icon-delete" @click="delDataRow(index)" />
          <el-button v-if="index+1>=formData.productList.length" icon="el-icon-plus" @click="addDataRow">添加行</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center">
      <el-button @click="cancelView">关闭</el-button>
      <el-button type="primary" @click="submitForm()">保存</el-button>
      <el-button type="success" @click="submitForm(true)">发布</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getToken } from '@/utils/auth'
import { HttpReq } from '@/api/common'

export default {
  dicts: ['product_type'],
  data() {
    return {
      visible: false,
      title: '内容管理供应商设置',
      formData: {
        productType: '',
        productList: [],
        productListPageId: null
      },
      rules: {}
    }
  },
  mounted() {
  },
  methods: {
    delDataRow(index) {
      if (this.formData.productList.length > 1) {
        this.formData.productList.splice(index, 1)
      }
    },
    addDataRow() {
      this.formData.productList.push({ index: '', productId: '' })
    },
    showView() {
      this.visible = true
    },
    hideView() {
      this.$refs.formViewRef.resetFields()
      this.visible = false
      this.formData = {
        productType: '',
        productList: [],
        productListPageId: null
      }
    },
    cancelView() {
      this.hideView()
    },
    release() {
      HttpReq.backstageApi.releaseProductListPage({ ids: [this.formData.productListPageId] }).then((res) => {
        this.$notify({
          title: res.msg,
          type: res.code === 200 ? 'success' : 'error',
          duration: 2500
        })
        if (res.code === 200) {
          this.cancelView()
          this.$parent.loadData()
        }
      })
    },
    submitForm(isRelease) {
      this.$refs.formViewRef.validate((valid, obj) => {
        if (valid) {
          HttpReq.backstageApi.updateProductListPage(this.formData).then((res) => {
            this.$notify({
              title: res.msg,
              type: res.code === 200 ? 'success' : 'error',
              duration: 2500
            })
            if (res.code === 200) {
              if (isRelease) {
                this.release()
              } else {
                this.cancelView()
                this.$parent.loadData()
              }
            }
          })
        } else {
          this.$message({
            message: '表单信息有误，请核对无误后提交！',
            type: 'error'
          })
        }
      })
    },
    loadData(productListPageId) {
      if (productListPageId) {
        HttpReq.backstageApi.queryProductListPageById({ productListPageId }).then((res) => {
          if (res.code === 200) {
            this.formData = Object.assign(this.formData, res.data)
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            })
          }
        })
      }
      this.showView()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.cell-box {
  min-width: 120px;
  .cell-input {
    width: 420px;
  }
  .cell-select {
    width: 220px;
  }
  >>>.el-input__inner {
    border: 1px solid rgba(100, 100, 100, 0.1);
    border-bottom: 1px solid rgba(100, 100, 100, 0.2);
    border-radius: 5px;
  }
  >>>.el-input.is-disabled .el-input__inner {
    border-radius: 0;
    border: 0;
    border-bottom: 1px solid rgba(100, 100, 100, 0.4);
    background: white;
    cursor: text;
  }
  >>>.el-input.is-disabled .el-input__icon {
    cursor: text;
  }
  >>>.el-icon-circle-check {
    color: #13ce66;
  }
  >>>.el-icon-arrow-up:before {
    content: '';
  }
}
</style>
