<template>
  <Collapse v-model="value1" class="property">
    <Panel name="1">
      列表设置
      <div slot="content">
        <div class="property-item" v-for="(config, index) in configList" :key="index">
          <Form :model="config" :label-width="80">
            <FormItem label="标题">
              <Input v-model="config.title"></Input>
            </FormItem>
            <FormItem label="描述">
              <Input v-model="config.description"></Input>
            </FormItem>
            <FormItem label="跳转页面">
              <Select v-model="config.function" filterable>
                <Option v-for="item in functionList" :value="item.id" :key="item.id">
                  {{item.title}}
                </Option>
              </Select>
            </FormItem>
            <FormItem label="查询SQL">
              <Select v-model="config.SQL" filterable>
                <Option v-for="item in querysourceList" :key="item.name" :value="item.name">
                  {{item.title}}
                </Option>
              </Select>
            </FormItem>
            
            <FormItem label="颜色">
              <Select v-model="config.color">
                <Option v-for="item in colorList" :value="item.value" :key="item.value">
                  <div style="width: 15px; height:15px;" :style="{background: item.value}"></div>
                </Option>
              </Select>
            </FormItem>
          </Form>
          <Button size="small" type="error" @click="removeItem(index)">删除</Button>
          <Divider />
        </div>
        <Button size="small" type="primary" @click="addItem">添加</Button>
      </div>
    </Panel>
  </Collapse>
</template>

<script>
export default {
  watch: {
    configList: {
      handler(newVal) {
        const properties = [...newVal]
        window.GMS.vbus.$emit('widgetPluginPropertiesChange', { id: this.type + this.id, properties })
        this.itemSetting.properties = properties
      },
      deep: true,
    },
  },
  props: {
    itemSetting: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      value1: null,
			configList: [],
      functionList: [],
      querysourceList: [],
      colorList: [
        { value: '#52D2DA' },
        { value: '#75DB5E' },
        { value: '#FAD83C' },
        { value: '#FF7A66' },
      ],
    }
  },
  computed: {
    id() {
      return this.itemSetting.id
    },
    type() {
      return this.itemSetting.type
    },
  },
  methods: {
    addFunction(l = []) {
      for (let o of l) {
        if (o.type == 'ROUTE') {
          this.functionList.push(o)
        }
        else if (o.type == 'GROUP') {
          this.addFunction(o.children)
        }
      }
    },
    addItem() {
      this.configList.push({
        title: '',
        query: '',
        SQL: '',
        description: '',
      })
    },
    removeItem(index) {
      this.configList.splice(index, 1)
    },
  },
  created() {
    window.GMS.util.nvwa.getRoutes().then((res) => {
      this.addFunction(res.data.children)
    })
    window.GMS.$http.get(`/bpm/bill/queryTitle/querySourceList`).then((res) => {
      this.querysourceList = res?.data || []
    })
    let prop = this.itemSetting.properties
		if (prop) {
			this.configList = prop
		}
  },
}
</script>
<style scoped lang="scss">
</style>
