<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="5vh"
    width="1040px"
    @close="closeModel"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-row :gutter="10" style="height: 500px">
            <el-col :span="18">
              <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                :size="'mini'"
                label-width="85px"
                class="demo-ruleForm"
                :label-position="'top'"
              >
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="报表名称" prop="name">
                      <el-input v-model="ruleForm.name" placeholder="请输入报表名称"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="报表数据时间区间" required>
                      <period-date-picker
                        :start="ruleForm.start"
                        :end="ruleForm.end"
                        :type="1"
                        :size="'mini'"
                        :width="'165px'"
                        @timeReturned="timeReturned($event)"
                      ></period-date-picker>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-col :span="24">
                  <el-form-item label="报表预览" required> </el-form-item
                ></el-col>
                <el-col :span="24">
                  <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')"
                      >立即创建</el-button
                    >
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                  </el-form-item></el-col
                >
              </el-form>
            </el-col>
            <el-col :span="6">
              <div>报表参数</div>
              <!-- "ontouchstart" in window -->
              <div></div>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button type="primary" @click="submitForm" v-if="!isDetail"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
   <script>
import asyncRequest from "@/apis/service/report";
import PeriodDatePicker from "@/components/PeriodDatePicker";
import resToken from "@/mixins/resToken";
export default {
  name: "report",
  props: ["showModel", "id", "isDetail"],
  components: {
    PeriodDatePicker,
  },
  mixins:[resToken],
  data() {
    return {
      ruleForm: {
        name: "账龄",
        start: "",
        end: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入报表名称", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur",
          },
        ],
      },
      activeName: "first",
      tabsList: [
        {
          tabs: "账龄",
          key: 1,
        },
        {
          tabs: "部门",
          key: 2,
        },
        {
          tabs: "业务员",
          key: 3,
        },
        {
          tabs: "合计",
          key: 4,
        },
        {
          tabs: "总公司",
          key: 5,
        },
         {
          tabs: "分公司",
          key: 6,
        },
        {
          tabs: "确认单的数量",
          key: 7,
        },
        {
          tabs: "平台",
          key: 8,
        },
        {
          tabs: "开票类型",
          key: 9,
        },

        {
          tabs: "产品品类",
          key: 10,
        },
         {
          tabs: "月",
          key: 11,
        },
         {
          tabs: "日",
          key: 12,
        },
        
      ],
      loading: false,
      title: "设置报表格式",
      showModelThis: this.showModel,
    };
  },
  methods: {
    async timeReturned(e) {
      if (e.startTime !== "") {
        this.ruleForm.start = e.startTime;
      } else {
        this.ruleForm.start = "";
      }

      if (e.endTime !== "") {
        this.ruleForm.end = e.endTime;
      } else {
        this.ruleForm.end = "";
      }
      if (this.ruleForm.start !== "" && this.ruleForm.end !== "") {
        // this.parmValue.page = 1;
        // await this.searchList();
      }
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      if (this.id === "add") {
        this.title = "设置报表格式";
        // this.ruleForm.isAdmin = 0;
        this.loading = false;
      } else {
        if (this.isDetail) {
          this.title = "设置报表格式";
        } else {
          this.title = "设置报表格式";
        }

        await this.initData();
      }
    },
    async initData() {
      this.loading = true;
      let res = await asyncRequest.detail({ id: this.id });
      this.loading = false;
      if (res&&res.code === 0) {
        // this.ruleForm = res.data;
      }else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      }else{
        this.$message.warning(res.message);
      }
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let obj = JSON.parse(JSON.stringify(this.ruleForm));
          let res = {};
          if (this.id === "add") {
            obj.passport = obj.password;
            res = await asyncRequest.add(obj);
          } else {
            res = await asyncRequest.update(obj);
          }
          this.loading = false;
          if (res&&res.code === 0) {
            let title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          }else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      }else{
        this.$message.warning(res.message);
      }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
};
</script>

   <style lang="scss" scoped>
.report {
}
</style>
   