<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form ref="form1" :model="queryParams" label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'标签名称'" prop="label">
              <el-input v-model="queryParams.label"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'标签类型'" prop="labelType">
              <el-select
                clearable
                v-model="queryParams.labelType"
                placeholder=""
              >
                <el-option
                  v-for="item in labelTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'创建日期'" prop="teamCode">
              <el-date-picker
                v-model="cjsj"
                value-format="yyyy-MM-dd"
                type="daterange"
                :range-separator="'至'"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search" @click="research()"> {{ '搜索' }}</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 size="mini" class="thebtn1" @click="adddate"
            >{{'添加'}}</el-button
          >
          <el-button size="mini" class="thebtn2" @click="exportTable"
            > {{ '导出' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="720px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>

          <el-table-column align="center" prop="label" :label="'标签名称'">
          </el-table-column>
          <el-table-column align="center" prop="registerValue" :label="'标签图片'">
            <template slot-scope="scope">
              <img
                style="width: 45px; height: 45px"
                :src="scope.row.labelImage"
                alt=""
              />
            </template>
          </el-table-column>
          <el-table-column align="center" prop="labelTypeVal" :label="'标签类型'">
          </el-table-column>
          <el-table-column align="center" prop="creationTime" :label="'创建日期'">
          </el-table-column>
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                style="color: #6962F6"
                @click.native.prevent="update(scope.row)"
                type="text"
                size="small"
                class="button-s"
              >
                {{'修改'}}
              </el-button>
              <el-button
                @click.native.prevent="deleteRow(scope.row)"
                type="text"
                size="small"
                class="button-s"
                style="color: #c73030"
              >
                {{'删除'}}
              </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="getShopList"
    />
    <el-dialog
      :title="'修改'"
      :visible.sync="updateShow"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form2" :rules="rules" :model="form" label-width="90px">
        <el-row>
          <el-col :span="10">
            <el-form-item :label="'标签名称'" prop="label">
              <el-input
                clearable
                v-model="form.label"
                :placeholder="'请输入'"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="10">
            <el-form-item :label="'标签类型'" prop="labelType">
              <el-select
                clearable
                v-model="form.labelType"
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="item in labelTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item :label="'标签图片'" prop="labelImage">
              <imageUpload v-model="form.labelImage" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{'取消'}}</el-button>
        <el-button class="submit" @click="onSubmit('form2')">{{'确认'}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import ImageUpload from '@/components/ImageUpload'
import {
  getLabelType,
  getLabelList,
  deletLabel,
  saveLabel,
  updateLabel,
  getLabelDetail,
} from "@/api/system";
export default {
  name: "Label",
  components: {
    topBar,
    ImageUpload,
  },
  data() {
    return {
      moren: "label",
      topList: [
        {
          name: '标签配置',
          path: "label",
        },
      ],
      labelTypeList: [],
      tableData: [],
      loading:false,
      cjsj: "",
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        startDate: "",
        endDate: "",
        label: "",
        labelType: "",
      },
      total: 0,
      rules: {
        label: [{ required: true, message: '请输入', trigger: "blur" }],
        labelType: [{ required: true, message: '请选择', trigger: "change" }],
        labelImage: [
          { required: true, message: "请选择图片", trigger: "change" },
        ],
      },
      form: {
        labelImage: "",
        labelType: "",
        label: "",
      },
      updateShow: false,
      addEidt: "",
    };
  },
  created() {
    this.research();
    this.getLabelType();
  },
  methods: {
    //导出
    exportTable() {
      this.download(
        "/system/manage/label/export",
        { ...this.queryParams },
        `${'标签配置'}${new Date().getTime()}.xlsx`
      );
    },
    //点击保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "";
          if (this.addEidt == 1) {
            //添加
            url = saveLabel;
          } else {
            url = updateLabel;
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
            }
            this.updateShow = false;
            this.$refs[formName].resetFields();
            this.delImg();
            this.research();
          });
        }
      });
    },
    //删除
    deleteRow(row) {
      this.$confirm('确定删除?').then((_) => {
        deletLabel({ pkId: row.pkId }).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.research();
          }
        });
      });
    },
    getImg(data) {
      this.form.labelImage = data;
    },
    delImg(data) {
      this.form.labelImage = data;
    },
    getLabelType() {
      getLabelType().then((res) => {
        this.labelTypeList = res.data;
      });
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        startDate: "",
        endDate: "",
        label: "",
        labelType: "",
      };
      this.$refs.form1.resetFields();
      this.cjsj = "";
      this.research();
    },
    adddate() {
      this.updateShow = true;
      this.addEidt = 1;
    },
    update(row) {
      this.updateShow = true;
      this.addEidt = 2;
      getLabelDetail({ labelId: row.pkId }).then((res) => {
        this.form = res.data;
        if (res.data.labelImage) {
          this.$refs.upImg.noneUploadImg = true
          this.$refs.upImg.coverList = [{ url: res.data.labelImage }]
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleClose() {
      this.updateShow = false;
      this.$refs["form2"].clearValidate();
      this.$refs["form2"].resetFields();
        // 上传初始化
        this.$refs.upImg.coverList = []
      this.$refs.upImg.noneUploadImg = false
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
    research() {
      this.loading = true

      if (this.cjsj) {
        this.queryParams.startDate = this.cjsj[0];
        this.queryParams.endDate = this.cjsj[1];
      }
      getLabelList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-select{
  width: 100%;
}
.el-range-editor.el-input__inner{
  width: 100%;
}
.button-s{
  padding: 0 3px;
}
.submit{
  width: 209px;
  height: 48px;
  background: #c8161d;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #c8161d;
  margin-left: 20px;
}
.cancels{
  width: 209px;
  height: 48px;
  background: #CCCCCC;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #ccc;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    background: #ffffff;
    border-radius: 8px;
    padding-top: 20px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #08143f;
        color: #ffffff;
      }
    }
  }
  .main {
    //margin-top: 20px;
    background: #f9f9f9;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          background: #3181E5;
          color: #ffffff;
          border-color: #FFAD41;
          border: none;
        }
        .thebtn2 {
          background: #FFAD41;
          border-color: #FFAD41;
          color: #ffffff;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }
}
</style>
