<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="thetopbox">
      <el-form ref="queryParams" :model="queryParams" label-width="auto">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'国家全称'" prop="name" class="el-inputs">
              <el-input v-model="queryParams.name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'国家简称'" prop="shortName" class="el-inputs">
              <el-input v-model="queryParams.shortName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'语种'" prop="languages" class="el-inputs">
              <el-select v-model="queryParams.languages" clearable>
                <el-option
                  v-for="item in languagesList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'国家编码'" prop="code" class="el-inputs">
              <el-input v-model="queryParams.code" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'会员前缀'" prop="code" class="el-inputs">
              <el-input v-model="queryParams.code" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'启用状态'" prop="enableState" class="el-inputs">
              <el-select v-model="queryParams.enableState" clearable>
                <el-option
                  v-for="item in stateList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item :label="'生效日期'" prop="enableState">
              <el-date-picker
                v-model="sxsj"
                type="datetimerange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd HH:mm:ss"
                :unlink-panels="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'创建日期'" prop="enableState">
              <el-date-picker
                v-model="cjsj"
                type="datetimerange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd HH:mm:ss"
                :unlink-panels="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4" style="margin-left: 0px">
            <div class="searchbox">
              <el-button
                class="my_search"
                @click="getNationalList"
              > {{ '搜索' }}</el-button>
              <el-button class="my_reset" @click="reChongzhi"> {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>

      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            v-has-buttons="['NationalAdd']"
            size="mini"
            class="thebtn1"
            @click="adddate()"
          >{{ '添加' }}</el-button>
          <el-button v-has-buttons="['NationalExport']" size="small" class="thebtn2" @click="handleExport"> {{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          height="680px"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column
            align="center"
            prop="nationalFlag1"
            :label="'国旗（长方形）'"
          >
            <template slot-scope="scope">
              <div class="tem">
                <img :src="scope.row.nationalFlag2" alt="">
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="gradeName" :label="'国旗（圆形）'">
            <template slot-scope="scope">
              <div class="tem2">
                <img :src="scope.row.nationalFlag1" alt="">
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="name" :label="'国家全称'" />
          <el-table-column align="center" prop="shortName" :label="'国家简称'" />
          <el-table-column
            align="center"
            prop="languages"
            :label="'语种'"
            :formatter="checkLanguages"
          />
          <el-table-column align="center" prop="codeThree" :label="'三位字母'" />
          <el-table-column align="center" prop="code" :label="'国家编码'" />
          <el-table-column align="center" prop="code" :label="'会员前缀'" />
          <el-table-column align="center" prop="numberPlaces" :label="'小数位数'" />
          <el-table-column
            align="center"
            prop="enableState"
            :label="'启用状态'"
            :formatter="checkQiyong"
          />
          <el-table-column align="center" prop="effectiveDate" :label="'生效日期'" width="150" />
          <el-table-column align="center" prop="creationTime" :label="'创建日期'" width="150" />
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-has-buttons="['NationalUpdate']"
                class="button-s"
                type="text"
                size="small"
                @click.native.prevent="update(scope.row, tableData)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                v-if="scope.row.enableState==0"
                v-has-buttons="['NationalDisable']"
                class="button-s redcolor"
                type="text"
                size="small"
                @click.native.prevent="updateState(scope.row, tableData)"
              >
                {{ '禁用' }}
              </el-button>
              <el-button
                v-else
                v-has-buttons="['NationalEnable']"
                class="button-s"
                type="text"
                size="small"
                @click.native.prevent="updateState(scope.row, tableData)"
              >
                {{ '启用' }}
              </el-button>
              <el-button
                v-has-buttons="['NationalDelete']"
                class="button-s"
                type="text"
                size="small"
                style="color: #c73030"
                @click.native.prevent="deleteRow(scope.row, tableData)"
              >
                {{ '删除' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getNationalList"
    />

    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'国家编号'" prop="code">
              <el-input
                v-model="form.code"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'国家全称'" prop="name">
              <el-input
                v-model="form.name"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'国家简称'" prop="shortName">
              <el-input
                v-model="form.shortName"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'三位字母'" prop="codeThree">
              <el-input
                v-model="form.codeThree"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'语种'" prop="languages">
              <el-select v-model="form.languages" clearable>
                <el-option
                  v-for="item in languagesList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'会员前缀'" prop="code">
              <el-input
                v-model="form.code"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'小数位数'" prop="numberPlaces">
              <el-input
                v-model="form.numberPlaces"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'生效日期'" prop="effectiveDate">
              <el-date-picker
                v-model="form.effectiveDate"
                type="datetime"
                :placeholder="'请选择'"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item :label="'上传国旗（圆形）'" prop="nationalFlag1">
              <el-upload
                :headers="headers"
                :class="{ uploadIconOne: showImg, uploadIcon: noneUploadImg }"
                :action="uploadImgUrl"
                :on-success="handlePictureSuccess"
                :on-change="uploadImgChange"
                list-type="picture-card"
                :limit="1"
                :on-remove="handleRemove"
                :file-list="afileList"
              >
                <i class="el-icon-plus" />
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'上传国旗（长方形）'" prop="nationalFlag2">
              <el-upload
                :headers="headers"
                :class="{ uploadIconOne: showImg2, uploadIcon: noneUploadImg2 }"
                :action="uploadImgUrl2"
                :on-success="handlePictureSuccess2"
                :on-change="uploadImgChange2"
                list-type="picture-card"
                :limit="1"
                :on-remove="handleRemove2"
                :file-list="bfileList"
              >
                <i class="el-icon-plus" />
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ '取消' }}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{ '确认' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import { getToken } from '@/utils/auth'
import {
  getNationalList,
  getlanguagesList,
  deleteCountry,
  updateEnable,
  addNational,
  updateNational,
  getOnecountrylist
} from '@/api/national'
export default {
  name: 'Gjpz',
  components: {
    topBar
  },
  data() {
    return {
      moren: 'gjpz',
      topList: [
        {
          name: '国家配置',
          path: 'gjpz'
        }
      ],
      multipleSelection: [], // 多选列表
      languagesList: [], // 语种列表
      sxsj: '',
      cjsj: '',
      tableData: [],
      loading: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        name: '', // 国家全称
        shortName: '', // 国家简称
        code: '', // 国家编码/会员前缀
        enableState: '', // 启用状态
        languages: '', // 语种
        createStartDate: '', // 创建时间起始
        createEndDate: '', // 创建时间结束
        effectiveStartDate: '', // 生效时间起始
        effectiveEndDate: '' // 生效时间结束
      },
      stateList: [
        // 启用状态列表
        {
          value: '0',
          label: '已启用'
        },
        {
          value: '1',
          label: '已禁用'
        }
      ],
      form: {
        code: '', // 国家编号
        name: '', // 国家全称
        shortName: '', // 国家简称
        codeThree: '', // 三位字母
        numberPlaces: '', // 小数位数
        languages: '', // 语种
        effectiveDate: null, // 生效时间
        nationalFlag1: '', // 上传国旗（圆形）
        nationalFlag2: '' // 上传国旗（长方形）
      },
      rules: {
        code: [{ required: true, message: '请输入', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        shortName: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        codeThree: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        numberPlaces: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        effectiveDate: [
          { required: true, message: '请选择生效时间', trigger: 'change' }
        ],
        languages: [
          { required: true, message: '请选择语种', trigger: 'change' }
        ],
        nationalFlag1: [
          { required: true, message: '请上传国旗', trigger: 'change' }
        ],

        nationalFlag2: [
          { required: true, message: '请上传国旗', trigger: 'change' }
        ]
      },
      showImg: true, // 默认展示
      noneUploadImg: false, //
      showImg2: true, // 默认展示
      noneUploadImg2: false, //
      uploadImgUrl: process.env.VUE_APP_BASE_API + '/system/upload',
      uploadImgUrl2: process.env.VUE_APP_BASE_API + '/system/upload',
      updateShow: false,
      updateTitle: '',
      headers: {
        Authorization: getToken()
      },
      addOrEdit: '', // 区分是添加还是修改 true添加 false修改
      afileList: [],
      bfileList: []
    }
  },
  created() {
    this.getNationalList()
    this.getlanguagesList()
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.addOrEdit == true) {
            url = addNational
          } else {
            url = updateNational
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.updateShow = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.getNationalList()
            } else {
              this.$message({
                message: res.msg,
                type: 'warning'
              })
            }
          })
        }
      })
    },
    // 图片上传成功后的回调
    handlePictureSuccess(res, file) {
      this.form.nationalFlag1 = res.data.url
    },
    uploadImgChange(file, fileList) {
      this.noneUploadImg = fileList.length > 0
    },
    // 图片上传删除
    handleRemove(file, fileList) {
      this.form.nationalFlag1 = ''
      this.noneUploadImg = fileList.length > 0
    },

    // 图片上传成功后的回调
    handlePictureSuccess2(res, file) {
      this.form.nationalFlag2 = res.data.url
    },
    uploadImgChange2(file, fileList) {
      this.noneUploadImg2 = fileList.length > 0
    },
    // 图片上传删除
    handleRemove2(file, fileList) {
      this.form.nationalFlag2 = ''
      this.noneUploadImg2 = fileList.length > 0
    },
    // 添加
    adddate() {
      this.updateShow = true
      this.updateTitle = '添加'
      this.addOrEdit = true
      // 上传初始化
      this.afileList = []
      this.bfileList = []
      this.noneUploadImg = false
      this.noneUploadImg2 = false
    },
    // 重置
    reChongzhi() {
      this.queryParams.name = ''
      this.queryParams.shortName = ''
      this.queryParams.code = ''
      this.queryParams.enableState = ''
      this.queryParams.languages = ''
      this.queryParams.createStartDate = ''
      this.queryParams.createEndDate = ''
      this.queryParams.effectiveStartDate = ''
      this.queryParams.effectiveEndDate = ''
      this.cjsj = ''
      this.sxsj = ''
    },

    checkQiyong(row) {
      if (row.enableState == 0) {
        return '启用'
      } else {
        return '禁用'
      }
    },
    // 获取语种枚举列表
    getlanguagesList() {
      getlanguagesList().then((res) => {
        this.languagesList = res.data
      })
    },
    // 展示列表语种替换
    checkLanguages(row) {
      let state = ''
      if (this.languagesList) {
        this.languagesList.map((element) => {
          if (row.languages == element.value) {
            state = element.label
          }
        })
      }
      return state
    },

    handleClose() {
      this.updateShow = false
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
      // 上传初始化
      this.afileList = []
      this.bfileList = []
      this.noneUploadImg = false
      this.noneUploadImg2 = false
    },
    // 修改
    update(row) {
      this.updateTitle = '修改'
      this.updateShow = true
      this.addOrEdit = false
      getOnecountrylist({ pkId: row.pkId }).then((res) => {
        this.form = res.data
        this.noneUploadImg = true
        this.noneUploadImg2 = true
        this.afileList = [{ url: res.data.nationalFlag1 }]
        this.bfileList = [{ url: res.data.nationalFlag2 }]
      })
    },
    // 删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCountry(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.getNationalList()
          } else {
            this.$message({
              message: res.msg,
              type: 'warning'
            })
          }
        })
      })
    },
    // 修改国家状态禁用/启用
    updateState(row) {
      this.$confirm('确认更改状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var thestate = ''
        if (row.enableState == 0) {
          thestate = 1
        } else if (row.enableState == 1) {
          thestate = 0
        }
        updateEnable(row.pkId, thestate).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            // 获取列表
          } else {
            this.$message({
              message: res.msg,
              type: 'warning'
            })
          }
          this.getNationalList()
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((_) => {
        this.download(
          '/system/manage/country/export',
          Object.assign({}, this.queryParams),
          `国家配置${new Date().getTime()}.xlsx`
        )
      })
    },
    // 获取国家配置列表
    getNationalList() {
      this.loading = true
      if (this.sxsj) {
        this.queryParams.effectiveStartDate = this.sxsj[0]
        this.queryParams.effectiveEndDate = this.sxsj[1]
      }
      if (this.cjsj) {
        this.queryParams.createStartDate = this.cjsj[0]
        this.queryParams.createEndDate = this.cjsj[1]
      }
      getNationalList(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>

<style lang="scss" scoped>
.button-s{
  padding: 3px 0;
}
::v-deep .el-check_s  .el-input__inner{
  width: 100%;
}
::v-deep .el-form-item{
  margin-bottom: 10px;
}
::v-deep .div1 .el-form-item__content{
  margin-left: 10px!important;
}
::v-deep .divs .el-input{
  //width: 300px;
}
::v-deep .el-date-editor .el-range__close-icon{
  margin-top: -10px;
}
::v-deep .el-date-editor .el-range-separator{
  margin-top: -10px;
}
::v-deep .el-date-editor .el-range__icon{
  margin-top: -10px;
}
::v-deep .el-input__inner{
  //height:28px;
  //line-height: 28px;
}
::v-deep .el-form-item__label{
  color: #333;
  font-weight: inherit;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-select {
  width: 100%;
}

.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-row {
  //margin-top: 20px;
}
.tem {
  img {
    width: 68px;
    height: 44px;
  }
}
.tem2 {
  img {
    width: 44px;
    height: 44px;
  }
}
.redcolor {
  color: #ed1d25;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding:  20px 20px 20px;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #c8161d;
        color: #ffffff;
      }
    }
  }
  .main {
    //margin-top: 20px;
    //background: #ffffff;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    background: #f9f9f9;
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;

        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;

        }
      }
    }
  }
}
::v-deep .el-date-editor--datetimerange{
  width: 100%;
}
::v-deep .el-col{
  padding-right: 10px;
}
</style>
