<!--
 * @Descripttion:
 * @version:
 * @Author: kBank
 * @Date: 2023-02-23 15:59:57
-->
<template>
  <div class="page">
    <div class="main">
      <el-form :model="ruleForm"
               :rules="rules"
               ref="ruleForm"
               label-width="100px"
               class="demo-ruleForm">
        <el-row>
          <el-col :span="22">
            <el-form-item :label="'标题'"
                          prop="titleName">
              <el-input clearable
                        v-model="ruleForm.titleName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="22">
            <el-form-item :label="'内容'"
                          prop="content">
              <editor ref="sptwEditor"
                      v-model="ruleForm.content"
                      :min-height="196"
                      :uploadUrl="uploadImgUrl" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item :label="'排序'"
                          prop="sort">
              <el-input clearable
                        v-model="ruleForm.sort"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item :label="'国家'"
                          prop="languageType">
              <el-select v-model="ruleForm.languageType"
                         size="medium"
                         filterable
                         :placeholder="'请选择'">
                <el-option v-for="item in countryList"
                           :key="item.pkCountry"
                           :label="item.name"
                           :value="item.pkCountry">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item :label="'生效日期'">
              <el-date-picker v-model="ruleForm.effectiveDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              :placeholder="'请选择'"
                              default-time="00:00:00">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'上传封面'"
                          prop="picture">
              <imageUpload v-model="ruleForm.picture" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="footer">
      <el-button size="small"
                 class="thebtn2"> {{ '取消' }}</el-button>
      <el-button size="small"
                 @click="submit"
                 class="thebtn1"> {{ '确认' }}</el-button>
    </div>
  </div>
</template>

<script>
import Editor from '@/components/Editor'
import * as api from '@/api/site.js'
import ImageUpload from '@/components/ImageUpload'

export default {
  components: {
    Editor,
    ImageUpload,
  },
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + '/system/upload',
      ruleForm: {
        isType: '',
      },
      rules: {
        picture: [{ required: true, message: '请上传图片', trigger: 'change' }],
        picture: [
          { required: true, message: '请上传封面图片', trigger: 'change' },
        ],
        pkAreaClassify: [
          { required: true, message: '请选择商品分类', trigger: 'change' },
        ],
        titleName: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        twoLevelTitle: [
          { required: true, message: '请输入副标题', trigger: 'blur' },
        ],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        languageType: [
          { required: true, message: '请选择国家', trigger: 'change' },
        ],
      },
      localList: [],
      popList: [],
      webList: [],
      gradeList: [],
      pkId: '',
      funcList: [],
      jsList: [],
      news: false,
      lookOver: false,
      classifyList: [],
      countryList: [],
    }
  },
  mounted() {
    this.getCoutry()
    this.pkId = this.$route.query.pkId || ''
    if (this.pkId != '') {
      this.getDetails()
    }
  },
  methods: {
    getCoutry() {
      api.userCountryList().then((res) => {
        this.countryList = res.data
      })
    },
    getDetails() {
      api.getOneMission(this.pkId).then((res) => {
        this.ruleForm = res.data
        this.$forceUpdate()
      })
    },
    submit() {
      let obj = Object.assign({}, this.ruleForm, { pkId: this.pkId })
      let urlRequest = ''
      if (this.pkId == '') {
        urlRequest = api.missionSave
      } else {
        urlRequest = api.missionUpdate
      }
      urlRequest(obj).then((res) => {
        if (res.code == 200) {
          if (this.pkId == '') {
            this.$message({
              message: '添加成功',
              type: 'success',
            })
          } else {
            this.$message({
              message: '修改成功',
              type: 'success',
            })
          }
          this.$router.replace({
            path: 'mission',
          })
        }

        // console.log(
        //   '%c [ res ]-238',
        //   'font-size:13px; background:#5f4299; color:#a386dd;',
        //   res
        // )
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    padding: 10px;
  }
  ::v-deep .el-select {
    width: 100%;
  }
}
.footer {
  height: 68px;
  background: #ffffff;
  box-shadow: 0px -3px 20px 0px rgba(204, 204, 204, 0.5);
  // margin: 0 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  .thebtn1 {
    background: #3181e5;
    color: #ffffff;
    padding: 9px 45px;
  }
  .thebtn2 {
    background: #cccccc;
    color: #ffffff;
    padding: 9px 45px;
  }
}
</style>
