<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'店铺编号'">
              <el-input clearable v-model="queryParams.storeCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'店铺名称'">
              <el-input clearable v-model="queryParams.storeName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="dizhi"  :label="'详细地址'" prop="">
              <el-cascader
                style="width: 100%"
                clearable
                v-model="queryParams.shoppingAddress"
                :options="areaData"
                :props="regionParams"
                @change="handleChange1"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="" label-width="40px">
              <el-input clearable v-model="queryParams.address"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'店长编号'">
              <el-input clearable v-model="queryParams.memberCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'店长姓名'">
              <el-input clearable v-model="queryParams.shopowner"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'店铺等级'">
              <el-select clearable v-model="queryParams.pkStoreLevel">
                <el-option
                  v-for="item in noPageList"
                  :key="item.pkId"
                  :label="item.storeLevelName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'创建日期'">
              <el-date-picker
                @change="checkTime"
                type="daterange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search" @click="getSearch"> {{ '搜索' }}</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="addData"
            >{{'添加'}}</el-button
          >
          <el-button size="mini" class="thebtn2" @click="handleExport"
            > {{ '导出' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="680px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            align="center"
            prop="storeCode"
            :label="'图片'"
            width="180"
          >
          <template slot-scope="scope">
            <img style="width:48px;height:48px;" :src="scope.row.picture" alt="">
          </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="storeCode"
            :label="'店铺编号'"
            width="180"
          >
          </el-table-column>
          <el-table-column align="center" prop="storeName" :label="'店铺名称'">
          </el-table-column>
          <el-table-column align="center" prop="provinceVal" :label="'省'">
          </el-table-column>
          <el-table-column align="center" prop="cityVal" :label="'市'">
          </el-table-column>
          <el-table-column align="center" prop="countyVal" :label="'区/县'">
          </el-table-column>
          <el-table-column
            align="center"
            min-width="150px"
            prop="address"
             :label="'详细地址'"
          >
          </el-table-column>
          <el-table-column align="center" prop="memberCode" :label="'店长编号'">
          </el-table-column>
          <el-table-column align="center" prop="shopowner" :label="'店长姓名'">
          </el-table-column>
          <el-table-column
            align="center"
            prop="pkTransactionVal"
            :label="'店铺等级'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            min-width="100px"
            prop="creationTime"
            :label="'创建日期'"
          >
          </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="'添加'"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'店铺编号'" prop="storeCode">
              <el-input clearable disabled v-model="form.storeCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'店铺名称'" prop="storeName">
              <el-input clearable v-model="form.storeName"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="'店长编号'" prop="memberCode">
              <el-input
                clearable
                @blur="checkName"
                v-model="form.memberCode"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="店长名称" prop="shopowner">
              <el-input clearable disabled v-model="form.shopowner"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="'店铺等级'" prop="pkStoreLevel">
              <el-select
                clearable
                :placeholder="'请选择'"
                v-model="form.pkStoreLevel"
              >
                <el-option
                  v-for="item in noPageList"
                  :key="item.pkId"
                  :label="item.storeLevelName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item  :label="'详细地址'" class="dizhi" prop="shoppingAddress">
              <el-cascader
                v-model="form.shoppingAddress"
                :options="areaData"
                :props="regionParams"
                @change="handleChange2"
              ></el-cascader>
              <el-input
                clearable
                type="textarea"
                v-model="form.address"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'上传图片'" prop="picture">
              <imageUpload v-model="form.picture" />
            </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 api from "@/api/site.js";
import * as con from "@/api/configManage";
import ImageUpload from '@/components/ImageUpload'
export default {
  name: "StoreConfiguration",
  components: {
    topBar,ImageUpload
  },
  data() {
    return {
      moren: "storeConfiguration",
      topList: [
        {
          name: '店铺配置',
          path: "storeConfiguration",
        },
      ],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: ["", "", ""],
      },
      total: 0,
      multipleSelection: [],
      areaData: [],
      regionParams: {
        label: "name",
        value: "id",
        children: "children",
        expandTrigger: "hover",
      },
      dialogVisible: false,
      rules: {
        storeName: [
          { required: true, message: '请输入店铺名称', trigger: "blur" },
        ],
        memberCode: [
          { required: true, message: '请输入店长编号', trigger: "blur" },
        ],
        pkStoreLevel: [
          { required: true, message: '请选择店铺等级', trigger: "change" },
        ],

        shoppingAddress: [
          { required: true, message: '请选择收货地址', trigger: "change" },
        ],
        picture:[{
          required: true, message: '请上传图片', trigger: "change"
        }]
      },
      form: {
        storeCode: "",
        storeName: "",
        province: "",
        city: "",
        county: "",
        address: "",
        memberCode: "",
        shopowner: "",
        pkStoreLevel: "",
        picture:"",
        shoppingAddress: ["", "", ""],
      },
      noPageList: [], //店铺等级下拉列表
    };
  },
  created() {
    this.getSearch();
    this.getData();
  },
  methods: {
    //导出
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/member/manage/store/export",
          { ...this.queryParams },
          `店铺配置${new Date().getTime()}.xlsx`
        );
      });

    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          con.saveManageStore(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: '添加成功',
                type: "success",
              });
              this.dialogVisible = false;
              this.getSearch();
            }
          });
        }
      });
    },
    checkName() {
      con.getMemberName({ memberCode: this.form.memberCode }).then((res) => {
        this.$set(this.form, "shopowner", res.memberName);
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    checkTime(val) {
      this.queryParams.startDate = val[0];
      this.queryParams.endDate = val[1];
    },
    handleChange1(val) {
      this.queryParams.province = val[0];
      this.queryParams.city = val[1];
      this.queryParams.county = val[2] || "";
    },
    handleChange2(val) {
      this.form.province = val[0];
      this.form.city = val[1];
      this.form.county = val[2] || "";
    },
    getData() {
      api.areaList().then((res) => {
        this.areaData = res.data;
      });
      con.getListNoPage().then((res) => {
        this.noPageList = res.data;
      });
    },
    addData() {
      this.form = {
        shoppingAddress: ["", "", ""],
      };
      con.getStoreCode({}).then((res) => {
        if (res.code == 200) {
          this.form.storeCode = res.msg;
          this.dialogVisible = true;
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: ["", "", ""],
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true
      con.getStoreList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false
      });
    },
  },
};
</script>

  <style lang="scss" scoped>
::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: #48b2fd;
          // width: 68px;
          // height: 32px;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
          // width: 68px;
          // height: 32px;
        }
      }
    }
  }
}
</style>
