<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>

    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="small" class="thebtn1" @click="addForm"
            >{{'添加'}}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          height="600px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="colsele" type="selection" width="55">
          </el-table-column>

          <el-table-column align="center" prop="name" :label="'名称'">
          </el-table-column>
          <el-table-column align="center" prop="sourceVal" :label="'平台'">
          </el-table-column>
          <el-table-column align="center" prop="enableStateVal" :label="'显示'">
            <template slot-scope="scope">
              <span style="color: #009b58" v-if="scope.row.enableState == 0">{{
                scope.row.enableStateVal
              }}</span>
              <span style="color: #c8161d" v-if="scope.row.enableState == 1">{{
                scope.row.enableStateVal
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            min-width="150px"
            prop="creationTime"
            :label="'创建日期'"
          >
          </el-table-column>
          <el-table-column align="center" prop="time" :label="'操作'">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="update(scope.row)"
                type="text"
                size="small"
                class="button-s"
                style="color: #ffad41"
              >
                {{'修改'}}
              </el-button>
              <el-button
                style="color: #009b58"
                v-if="scope.row.enableState == 1"
                type="text"
                size="small"
                class="button-s"
                @click="updateStatus(scope.row, 0)"
              >
                {{ '允许' }}
              </el-button>
              <el-button
                style="color: #c8161d"
                v-if="scope.row.enableState == 0"
                type="text"
                size="small"
                class="button-s"
                @click="updateStatus(scope.row, 1)"
              >
                {{ '禁止' }}
              </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="getSearch"
    />
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="30%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="'名称'" prop="name">
              <el-input v-model="form.name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="'渠道'" prop="source">
              <el-select
                :disabled="ifupdate == 2 ? true : false"
                v-model="form.source"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="item in sourceList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="'显示'" prop="enableState">
              <el-select
                v-model="form.enableState"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="item in enableStateList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </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 * as three from "@/api/threegoods";
export default {
  name: "SourceChannel",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "sourceChannel",
      topList: [
        {
          name: '渠道维护',
          path: "sourceChannel",
        },
      ],
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        source: 1,
        creationTime: [],
      },
      sourceList: [],
      total: 0,
      multipleSelection: [],
      dialogVisible: false,
      dialogTitle: "",
      form: {
        name: "",
        source: "",
        enableState: "",
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: "blur" }],
        source: [{ required: true, message: '请选择', trigger: "change" }],
        enableState: [{ required: true, message: '请选择', trigger: "change" }],
      },
      enableStateList: [
        {
          value: 0,
          label: '允许',
        },
        {
          value: 1,
          label: '禁止',
        },
      ],
      ifupdate: "", //1添加 2修改
    };
  },
  created() {
    this.getSearch();
    this.getOthers();
  },
  methods: {
    addForm() {
      this.ifupdate = 1;
      this.dialogTitle = '添加';
      this.dialogVisible = true;
      this.form = {
        name: "",
        source: "",
        enableState: "",
      };
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          three.saveTsource(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.dialogVisible = false;

              this.getSearch();
            }
          });
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    //修改状态
    updateStatus(row, index) {
      this.$confirm('是否修改状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        three
          .enableTsource({ source: row.source, enableState: index })
          .then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getSearch();
            }
          });
      });
    },

    update(row) {
      this.ifupdate = 2;
      this.dialogTitle = '修改';
      three.getMangeTsouceDetailList({ source: row.source }).then((res) => {
        if (res.code == 200) {
          this.form = res.data;
          this.dialogVisible = true;
        }
      });
    },

    getOthers() {
      three.getJxhhSource().then((res) => {
        this.sourceList = res.data;
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        source: 1,
        creationTime: [],
      };
      this.getSearch();
    },
    getSearch() {
      three.getMangeTsouceList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.queryParams.pageNum = 1
      });
    },

  },
};
</script>

    <style lang="scss" scoped>
::v-deep .dizhi .el-cascader {
  width: 100%;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 15px 20px 15px 0;
    background: #ffffff;
    border-radius: 8px;
    .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 {
          color: #ffffff;
          background: #4391fd;
          width: 68px;
          height: 32px;
        }
        .thebtn2 {
          color: #ffffff;
          background: #009b58;
          width: 68px;
          height: 32px;
        }
        .thebtn3 {
          color: #ffffff;
          background: #c8161d;
          width: 68px;
          height: 32px;
        }
      }
    }
  }
}
</style>
