<template>
  <el-dialog :title="dialogTitle"
             :visible="dialogVisible"
             width="75vw"
             center
             :destroy-on-close="true"
             :show-close="false"
  >
    <div class="dialogForm">
      <el-col>
        <el-card class="box-card">
          <div class="demo-input-suffix">
            <div class="title">
              <div class="dot"></div>
              采集基本配置
              <div class="dot"></div>
            </div>
            <el-col class="card-item" :span="2">
              <label class="text_label">规则名称：</label>
            </el-col>
            <el-col class="card-item" :span="4">
              <el-input v-model="form.parserName" size="mini" autocomplete="off"></el-input>
            </el-col>
            <el-col class="card-item" :span="2"><label
                class="text_label">拓展域名：</label>
            </el-col>
            <el-col class="card-item" :span="4">
              <el-input v-model="form.allowDomain" size="mini" autocomplete="off"></el-input>
            </el-col>
            <el-col class="card-item" :span="2"><label
                class="text_label">所在时区：</label></el-col>
            <el-col class="card-item" :span="4">
              <el-input v-model="form.timeZone" size="mini" autocomplete="off"></el-input>
            </el-col>
            <el-col class="card-item" :span="2"><label
                class="text_label">采集深度：</label>
            </el-col>
            <el-col class="card-item" :span="4">
              <el-select
                  v-model="form.depth"
                  placeholder="请选择"
                  collapse-tags
                  style="width:100%"
                  size="mini"
              >
                <el-option
                    v-for="item in depthList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                ></el-option>
              </el-select>
            </el-col>
            <el-col class="card-item" :span="2"><label class="text_label">间隔时间：</label></el-col>
            <el-col class="card-item" :span="4">
              <el-select
                  v-model="form.cron" size="mini"
                  placeholder="请选择"
                  style="width:100%"
              >
                <el-option
                    v-for="item in cronList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                ></el-option>
              </el-select>
            </el-col>
            <el-col class="card-item" :span="2"><label class="text_label">采集范围：</label>
            </el-col>
            <el-col class="card-item" :span="4">
              <el-radio-group v-model="form.crawlMethod" size="mini">
                <el-radio-button :label="0">普通</el-radio-button>
                <el-radio-button :label="1">列表</el-radio-button>
                <el-radio-button :label="2">翻页</el-radio-button>
              </el-radio-group>
            </el-col>
            <el-col class="card-item" :span="2"><label class="text_label">渲染模式：</label></el-col>
            <el-col class="card-item" :span="4">
              <el-radio-group v-model="form.useSelenium" size="mini">
                <el-radio-button :label="1">动态</el-radio-button>
                <el-radio-button :label="0">静态</el-radio-button>
              </el-radio-group>
            </el-col>
          </div>
        </el-card>
        <el-card class="box-card">
          <div class="demo-input-suffix">
            <div class="title">
              <div class="dot"></div>
              解析模板
              <div class="dot"></div>
            </div>
            <el-col class="card-item" :span="4"><label class="text_label">内容页正则：</label>
            </el-col>
            <el-col class="card-item" :span="7">
              <el-input v-model="form.detailPageReg" autocomplete="off" size="mini"></el-input>
            </el-col>
            <el-col class="card-item parse-button-icon" :span="1">
              <i class="el-icon-circle-plus" @click="addFieldRuleItem()"></i>
            </el-col>
            <div v-for="(propItem, propIndex) in form.itemPropMap" :key="propIndex">
              <el-col class="card-item" :span="4" style="padding:0 12px 0 12px;">
                <el-select size="mini" :disabled="propIndex<4" v-model="propItem.field" @input="forceUpdate"
                           placeholder="选择字段" style="float:right">
                  <el-option
                      v-for="item in esFields"
                      :key="item.columnProp"
                      :label="item.columnLabel"
                      :value="item.columnProp">
                  </el-option>
                </el-select>
              </el-col>
              <el-col class="card-item" :span="7">
                <el-input v-model="propItem.value" @input="forceUpdate" autocomplete="off" size="mini"></el-input>
              </el-col>
              <el-col class="card-item parse-button-icon" :span="1">
                <i v-if="propIndex>3" class="el-icon-remove"
                   @click.prevent="deleteFieldRuleItem(propIndex)"></i>
              </el-col>
            </div>
          </div>
        </el-card>
      </el-col>
    </div>
    <span slot="footer" class="dialog-footer">
          <el-button size="small" type="success" @click="submit" :loading="saveLoading">保存</el-button>
          <el-button style="margin-left: 10px;" size="small" type="primary" @click="closeDialog">关闭</el-button>
    </span>
  </el-dialog>

</template>

<script>
import {
  Button,
  Card,
  Checkbox,
  Col,
  Input,
  InputNumber,
  Option,
  Pagination,
  Radio,
  Select
} from 'element-ui'
import Vue from "vue";

Vue.use(Card);
Vue.use(Button);
Vue.use(Input);
Vue.use(InputNumber);
Vue.use(Checkbox);
Vue.use(Col);
Vue.use(Radio);
Vue.use(Select);
Vue.use(Option);
Vue.use(Pagination);

export default {
  name: "RulesParserDialog",
  props: ["form", "metaData", "dialogTitle", "dialogVisible", "cronList", "depthList", "closeDialog", "submit"],
  data() {
    return {
      saveLoading: false,
    }
  },
  methods: {
    forceUpdate() {
      this.$forceUpdate();
    },
    addFieldRule() {
      this.form.itemPropMap.push({value: '', field: 'es_urltitle'});
      this.form.itemPropMap.push({value: '', field: 'es_urlcontent'});
      this.form.itemPropMap.push({value: '', field: 'es_urltime'});
      this.form.itemPropMap.push({value: '', field: 'es_lasttime'});
    },
    addFieldRuleItem() {
      this.form.itemPropMap.push({value: '', field: ''});
    },
    deleteFieldRuleItem(ruleItemIndex) {
      this.form.itemPropMap.splice(ruleItemIndex, 1);
    },
  },
  computed: {
    esFields: function () {
      let temp = [];
      this.$store.state.metadata.metadata.forEach(element => {
        if (element.tableName === 'es_public_info') {
          temp.push(element);
        }
      });
      return temp;
    },
  }
}
</script>

<style scoped>
.dialogForm {
  display: flex;
  justify-content: center;
}

.demo-form {
  width: 400px;
}

.box-card .text_label {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  padding: 0 12px 0 0;
  box-sizing: border-box;
  width: 100%;
  line-height: 28px;
}

.box-card {
  background-color: #f8f8f8;
  margin-bottom: 10px;
  padding-bottom: 15px;
}

.box-card .title {
  text-align: center;
  font-size: 18px;
  padding: 10px;
  color: #666;
}

.box-card .dot {
  display: inline-block;
  vertical-align: middle;
  width: 6px;
  height: 6px;
  margin: 0 15px;
  background-color: #0080e5;
}

.el-input__inner {
  height: 28px !important;
  line-height: 28px !important;
}

.card-item {
  height: 32px;
  margin-top: 3px;
}

.parse-button-icon {
  text-align: center;
  color: #409EFF;
}

</style>